1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * VMware vSockets Driver 4 * 5 * Copyright (C) 2007-2013 VMware, Inc. All rights reserved. 6 */ 7 8 /* Implementation notes: 9 * 10 * - There are two kinds of sockets: those created by user action (such as 11 * calling socket(2)) and those created by incoming connection request packets. 12 * 13 * - There are two "global" tables, one for bound sockets (sockets that have 14 * specified an address that they are responsible for) and one for connected 15 * sockets (sockets that have established a connection with another socket). 16 * These tables are "global" in that all sockets on the system are placed 17 * within them. - Note, though, that the bound table contains an extra entry 18 * for a list of unbound sockets and SOCK_DGRAM sockets will always remain in 19 * that list. The bound table is used solely for lookup of sockets when packets 20 * are received and that's not necessary for SOCK_DGRAM sockets since we create 21 * a datagram handle for each and need not perform a lookup. Keeping SOCK_DGRAM 22 * sockets out of the bound hash buckets will reduce the chance of collisions 23 * when looking for SOCK_STREAM sockets and prevents us from having to check the 24 * socket type in the hash table lookups. 25 * 26 * - Sockets created by user action will either be "client" sockets that 27 * initiate a connection or "server" sockets that listen for connections; we do 28 * not support simultaneous connects (two "client" sockets connecting). 29 * 30 * - "Server" sockets are referred to as listener sockets throughout this 31 * implementation because they are in the TCP_LISTEN state. When a 32 * connection request is received (the second kind of socket mentioned above), 33 * we create a new socket and refer to it as a pending socket. These pending 34 * sockets are placed on the pending connection list of the listener socket. 35 * When future packets are received for the address the listener socket is 36 * bound to, we check if the source of the packet is from one that has an 37 * existing pending connection. If it does, we process the packet for the 38 * pending socket. When that socket reaches the connected state, it is removed 39 * from the listener socket's pending list and enqueued in the listener 40 * socket's accept queue. Callers of accept(2) will accept connected sockets 41 * from the listener socket's accept queue. Once the connection is accepted, 42 * it is owned by the user process and the responsibility for cleanup falls 43 * with that user process. 44 * 45 * - It is possible that these pending sockets will never reach the connected 46 * state; in fact, we may never receive another packet after the connection 47 * request. Because of this, we must schedule a cleanup function to run in the 48 * future, after some amount of time passes where a connection should have been 49 * established. This function ensures that the socket is off all lists so it 50 * cannot be retrieved, then drops all references to the socket so it is cleaned 51 * up (sock_put() -> sk_free() -> our sk_destruct implementation). 52 * 53 * - Lock ordering for pending or accept queue sockets is: 54 * 55 * lock_sock(listener); 56 * lock_sock_nested(pending, SINGLE_DEPTH_NESTING); 57 * 58 * Using explicit nested locking keeps lockdep happy since normally only one 59 * lock of a given class may be taken at a time. 60 * 61 * - Sockets created by user action will be cleaned up when the user process 62 * calls close(2), causing our release implementation to be called. Our release 63 * implementation will perform some cleanup then drop the last reference so our 64 * sk_destruct implementation is invoked. Our sk_destruct implementation will 65 * perform additional cleanup that's common for both types of sockets. 66 * 67 * - A socket's reference count is what ensures that the structure won't be 68 * freed. Each entry in a list (such as the "global" bound and connected tables 69 * and the listener socket's pending list and connected queue) ensures a 70 * reference. When we defer work until process context and pass a socket as our 71 * argument, we must ensure the reference count is increased to ensure the 72 * socket isn't freed before the function is run; the deferred function will 73 * then drop the reference. 74 * 75 * - sk->sk_state uses the TCP state constants because they are widely used by 76 * other address families and exposed to userspace tools like ss(8): 77 * 78 * TCP_CLOSE - unconnected 79 * TCP_SYN_SENT - connecting 80 * TCP_ESTABLISHED - connected 81 * TCP_CLOSING - disconnecting 82 * TCP_LISTEN - listening 83 * 84 * - Namespaces in vsock support two different modes: "local" and "global". 85 * Each mode defines how the namespace interacts with CIDs. 86 * Each namespace exposes two sysctl files: 87 * 88 * - /proc/sys/net/vsock/ns_mode (read-only) reports the current namespace's 89 * mode, which is set at namespace creation and immutable thereafter. 90 * - /proc/sys/net/vsock/child_ns_mode (write-once) controls what mode future 91 * child namespaces will inherit when created. The initial value matches 92 * the namespace's own ns_mode. 93 * 94 * Changing child_ns_mode only affects newly created namespaces, not the 95 * current namespace or existing children. A "local" namespace cannot set 96 * child_ns_mode to "global". child_ns_mode is write-once, so that it may be 97 * configured and locked down by a namespace manager. Writing a different 98 * value after the first write returns -EBUSY. At namespace creation, ns_mode 99 * is inherited from the parent's child_ns_mode. 100 * 101 * The init_net mode is "global" and cannot be modified. The init_net 102 * child_ns_mode is also write-once, so an init process (e.g. systemd) can 103 * set it to "local" to ensure all new namespaces inherit local mode. 104 * 105 * The modes affect the allocation and accessibility of CIDs as follows: 106 * 107 * - global - access and allocation are all system-wide 108 * - all CID allocation from global namespaces draw from the same 109 * system-wide pool. 110 * - if one global namespace has already allocated some CID, another 111 * global namespace will not be able to allocate the same CID. 112 * - global mode AF_VSOCK sockets can reach any VM or socket in any global 113 * namespace, they are not contained to only their own namespace. 114 * - AF_VSOCK sockets in a global mode namespace cannot reach VMs or 115 * sockets in any local mode namespace. 116 * - local - access and allocation are contained within the namespace 117 * - CID allocation draws only from a private pool local only to the 118 * namespace, and does not affect the CIDs available for allocation in any 119 * other namespace (global or local). 120 * - VMs in a local namespace do not collide with CIDs in any other local 121 * namespace or any global namespace. For example, if a VM in a local mode 122 * namespace is given CID 10, then CID 10 is still available for 123 * allocation in any other namespace, but not in the same namespace. 124 * - AF_VSOCK sockets in a local mode namespace can connect only to VMs or 125 * other sockets within their own namespace. 126 * - sockets bound to VMADDR_CID_ANY in local namespaces will never resolve 127 * to any transport that is not compatible with local mode. There is no 128 * error that propagates to the user (as there is for connection attempts) 129 * because it is possible for some packet to reach this socket from 130 * a different transport that *does* support local mode. For 131 * example, virtio-vsock may not support local mode, but the socket 132 * may still accept a connection from vhost-vsock which does. 133 */ 134 135 #include <linux/compat.h> 136 #include <linux/types.h> 137 #include <linux/bitops.h> 138 #include <linux/cred.h> 139 #include <linux/errqueue.h> 140 #include <linux/init.h> 141 #include <linux/io.h> 142 #include <linux/kernel.h> 143 #include <linux/sched/signal.h> 144 #include <linux/kmod.h> 145 #include <linux/list.h> 146 #include <linux/miscdevice.h> 147 #include <linux/module.h> 148 #include <linux/mutex.h> 149 #include <linux/net.h> 150 #include <linux/proc_fs.h> 151 #include <linux/poll.h> 152 #include <linux/random.h> 153 #include <linux/skbuff.h> 154 #include <linux/smp.h> 155 #include <linux/uio.h> 156 #include <linux/socket.h> 157 #include <linux/stddef.h> 158 #include <linux/sysctl.h> 159 #include <linux/unistd.h> 160 #include <linux/wait.h> 161 #include <linux/workqueue.h> 162 #include <net/sock.h> 163 #include <net/af_vsock.h> 164 #include <net/netns/vsock.h> 165 #include <uapi/linux/vm_sockets.h> 166 #include <uapi/asm-generic/ioctls.h> 167 168 #define VSOCK_NET_MODE_STR_GLOBAL "global" 169 #define VSOCK_NET_MODE_STR_LOCAL "local" 170 171 /* 6 chars for "global", 1 for null-terminator, and 1 more for '\n'. 172 * The newline is added by proc_dostring() for read operations. 173 */ 174 #define VSOCK_NET_MODE_STR_MAX 8 175 176 static int __vsock_bind(struct sock *sk, struct sockaddr_vm *addr); 177 static void vsock_sk_destruct(struct sock *sk); 178 static int vsock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb); 179 static void vsock_close(struct sock *sk, long timeout); 180 181 /* Protocol family. */ 182 struct proto vsock_proto = { 183 .name = "AF_VSOCK", 184 .owner = THIS_MODULE, 185 .obj_size = sizeof(struct vsock_sock), 186 .close = vsock_close, 187 #ifdef CONFIG_BPF_SYSCALL 188 .psock_update_sk_prot = vsock_bpf_update_proto, 189 #endif 190 }; 191 192 /* The default peer timeout indicates how long we will wait for a peer response 193 * to a control message. 194 */ 195 #define VSOCK_DEFAULT_CONNECT_TIMEOUT (2 * HZ) 196 197 #define VSOCK_DEFAULT_BUFFER_SIZE (1024 * 256) 198 #define VSOCK_DEFAULT_BUFFER_MAX_SIZE (1024 * 256) 199 #define VSOCK_DEFAULT_BUFFER_MIN_SIZE 128 200 201 /* Transport used for host->guest communication */ 202 static const struct vsock_transport *transport_h2g; 203 /* Transport used for guest->host communication */ 204 static const struct vsock_transport *transport_g2h; 205 /* Transport used for DGRAM communication */ 206 static const struct vsock_transport *transport_dgram; 207 /* Transport used for local communication */ 208 static const struct vsock_transport *transport_local; 209 static DEFINE_MUTEX(vsock_register_mutex); 210 211 /**** UTILS ****/ 212 213 /* Each bound VSocket is stored in the bind hash table and each connected 214 * VSocket is stored in the connected hash table. 215 * 216 * Unbound sockets are all put on the same list attached to the end of the hash 217 * table (vsock_unbound_sockets). Bound sockets are added to the hash table in 218 * the bucket that their local address hashes to (vsock_bound_sockets(addr) 219 * represents the list that addr hashes to). 220 * 221 * Specifically, we initialize the vsock_bind_table array to a size of 222 * VSOCK_HASH_SIZE + 1 so that vsock_bind_table[0] through 223 * vsock_bind_table[VSOCK_HASH_SIZE - 1] are for bound sockets and 224 * vsock_bind_table[VSOCK_HASH_SIZE] is for unbound sockets. The hash function 225 * mods with VSOCK_HASH_SIZE to ensure this. 226 */ 227 #define MAX_PORT_RETRIES 24 228 229 #define VSOCK_HASH(addr) ((addr)->svm_port % VSOCK_HASH_SIZE) 230 #define vsock_bound_sockets(addr) (&vsock_bind_table[VSOCK_HASH(addr)]) 231 #define vsock_unbound_sockets (&vsock_bind_table[VSOCK_HASH_SIZE]) 232 233 /* XXX This can probably be implemented in a better way. */ 234 #define VSOCK_CONN_HASH(src, dst) \ 235 (((src)->svm_cid ^ (dst)->svm_port) % VSOCK_HASH_SIZE) 236 #define vsock_connected_sockets(src, dst) \ 237 (&vsock_connected_table[VSOCK_CONN_HASH(src, dst)]) 238 #define vsock_connected_sockets_vsk(vsk) \ 239 vsock_connected_sockets(&(vsk)->remote_addr, &(vsk)->local_addr) 240 241 struct list_head vsock_bind_table[VSOCK_HASH_SIZE + 1]; 242 EXPORT_SYMBOL_GPL(vsock_bind_table); 243 struct list_head vsock_connected_table[VSOCK_HASH_SIZE]; 244 EXPORT_SYMBOL_GPL(vsock_connected_table); 245 DEFINE_SPINLOCK(vsock_table_lock); 246 EXPORT_SYMBOL_GPL(vsock_table_lock); 247 248 /* Autobind this socket to the local address if necessary. */ 249 static int vsock_auto_bind(struct vsock_sock *vsk) 250 { 251 struct sock *sk = sk_vsock(vsk); 252 struct sockaddr_vm local_addr; 253 254 if (vsock_addr_bound(&vsk->local_addr)) 255 return 0; 256 vsock_addr_init(&local_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY); 257 return __vsock_bind(sk, &local_addr); 258 } 259 260 static void vsock_init_tables(void) 261 { 262 int i; 263 264 for (i = 0; i < ARRAY_SIZE(vsock_bind_table); i++) 265 INIT_LIST_HEAD(&vsock_bind_table[i]); 266 267 for (i = 0; i < ARRAY_SIZE(vsock_connected_table); i++) 268 INIT_LIST_HEAD(&vsock_connected_table[i]); 269 } 270 271 static void __vsock_insert_bound(struct list_head *list, 272 struct vsock_sock *vsk) 273 { 274 sock_hold(&vsk->sk); 275 list_add(&vsk->bound_table, list); 276 } 277 278 static void __vsock_insert_connected(struct list_head *list, 279 struct vsock_sock *vsk) 280 { 281 sock_hold(&vsk->sk); 282 list_add(&vsk->connected_table, list); 283 } 284 285 static void __vsock_remove_bound(struct vsock_sock *vsk) 286 { 287 list_del_init(&vsk->bound_table); 288 sock_put(&vsk->sk); 289 } 290 291 static void __vsock_remove_connected(struct vsock_sock *vsk) 292 { 293 list_del_init(&vsk->connected_table); 294 sock_put(&vsk->sk); 295 } 296 297 static struct sock *__vsock_find_bound_socket_net(struct sockaddr_vm *addr, 298 struct net *net) 299 { 300 struct vsock_sock *vsk; 301 302 list_for_each_entry(vsk, vsock_bound_sockets(addr), bound_table) { 303 struct sock *sk = sk_vsock(vsk); 304 305 if (vsock_addr_equals_addr(addr, &vsk->local_addr) && 306 vsock_net_check_mode(sock_net(sk), net)) 307 return sk; 308 309 if (addr->svm_port == vsk->local_addr.svm_port && 310 (vsk->local_addr.svm_cid == VMADDR_CID_ANY || 311 addr->svm_cid == VMADDR_CID_ANY) && 312 vsock_net_check_mode(sock_net(sk), net)) 313 return sk; 314 } 315 316 return NULL; 317 } 318 319 static struct sock * 320 __vsock_find_connected_socket_net(struct sockaddr_vm *src, 321 struct sockaddr_vm *dst, struct net *net) 322 { 323 struct vsock_sock *vsk; 324 325 list_for_each_entry(vsk, vsock_connected_sockets(src, dst), 326 connected_table) { 327 struct sock *sk = sk_vsock(vsk); 328 329 if (vsock_addr_equals_addr(src, &vsk->remote_addr) && 330 dst->svm_port == vsk->local_addr.svm_port && 331 vsock_net_check_mode(sock_net(sk), net)) { 332 return sk; 333 } 334 } 335 336 return NULL; 337 } 338 339 static void vsock_insert_unbound(struct vsock_sock *vsk) 340 { 341 spin_lock_bh(&vsock_table_lock); 342 __vsock_insert_bound(vsock_unbound_sockets, vsk); 343 spin_unlock_bh(&vsock_table_lock); 344 } 345 346 void vsock_insert_connected(struct vsock_sock *vsk) 347 { 348 struct list_head *list = vsock_connected_sockets( 349 &vsk->remote_addr, &vsk->local_addr); 350 351 spin_lock_bh(&vsock_table_lock); 352 __vsock_insert_connected(list, vsk); 353 spin_unlock_bh(&vsock_table_lock); 354 } 355 EXPORT_SYMBOL_GPL(vsock_insert_connected); 356 357 void vsock_remove_bound(struct vsock_sock *vsk) 358 { 359 spin_lock_bh(&vsock_table_lock); 360 if (__vsock_in_bound_table(vsk)) 361 __vsock_remove_bound(vsk); 362 spin_unlock_bh(&vsock_table_lock); 363 } 364 EXPORT_SYMBOL_GPL(vsock_remove_bound); 365 366 void vsock_remove_connected(struct vsock_sock *vsk) 367 { 368 spin_lock_bh(&vsock_table_lock); 369 if (__vsock_in_connected_table(vsk)) 370 __vsock_remove_connected(vsk); 371 spin_unlock_bh(&vsock_table_lock); 372 } 373 EXPORT_SYMBOL_GPL(vsock_remove_connected); 374 375 /* Find a bound socket, filtering by namespace and namespace mode. 376 * 377 * Use this in transports that are namespace-aware and can provide the 378 * network namespace context. 379 */ 380 struct sock *vsock_find_bound_socket_net(struct sockaddr_vm *addr, 381 struct net *net) 382 { 383 struct sock *sk; 384 385 spin_lock_bh(&vsock_table_lock); 386 sk = __vsock_find_bound_socket_net(addr, net); 387 if (sk) 388 sock_hold(sk); 389 390 spin_unlock_bh(&vsock_table_lock); 391 392 return sk; 393 } 394 EXPORT_SYMBOL_GPL(vsock_find_bound_socket_net); 395 396 /* Find a bound socket without namespace filtering. 397 * 398 * Use this in transports that lack namespace context. All sockets are 399 * treated as if in global mode. 400 */ 401 struct sock *vsock_find_bound_socket(struct sockaddr_vm *addr) 402 { 403 return vsock_find_bound_socket_net(addr, NULL); 404 } 405 EXPORT_SYMBOL_GPL(vsock_find_bound_socket); 406 407 /* Find a connected socket, filtering by namespace and namespace mode. 408 * 409 * Use this in transports that are namespace-aware and can provide the 410 * network namespace context. 411 */ 412 struct sock *vsock_find_connected_socket_net(struct sockaddr_vm *src, 413 struct sockaddr_vm *dst, 414 struct net *net) 415 { 416 struct sock *sk; 417 418 spin_lock_bh(&vsock_table_lock); 419 sk = __vsock_find_connected_socket_net(src, dst, net); 420 if (sk) 421 sock_hold(sk); 422 423 spin_unlock_bh(&vsock_table_lock); 424 425 return sk; 426 } 427 EXPORT_SYMBOL_GPL(vsock_find_connected_socket_net); 428 429 /* Find a connected socket without namespace filtering. 430 * 431 * Use this in transports that lack namespace context. All sockets are 432 * treated as if in global mode. 433 */ 434 struct sock *vsock_find_connected_socket(struct sockaddr_vm *src, 435 struct sockaddr_vm *dst) 436 { 437 return vsock_find_connected_socket_net(src, dst, NULL); 438 } 439 EXPORT_SYMBOL_GPL(vsock_find_connected_socket); 440 441 /** 442 * vsock_check_source - validate a packet source against a socket peer 443 * @vsk: socket receiving the packet 444 * @transport: transport receiving the packet 445 * @src: source address from the packet 446 * 447 * Return: true if the packet arrived on the socket's assigned transport and 448 * its source matches the stored peer. Loopback packets are generated 449 * internally and always use the local CID as their source, including 450 * connections using a valid CID alias. 451 * 452 * The caller must hold the socket lock and must not call this for listening 453 * sockets, which accept packets from any source and have no assigned 454 * transport. 455 */ 456 bool vsock_check_source(const struct vsock_sock *vsk, 457 const struct vsock_transport *transport, 458 const struct sockaddr_vm *src) 459 { 460 if (vsk->transport != transport) 461 return false; 462 463 if (src->svm_port != vsk->remote_addr.svm_port) 464 return false; 465 466 if (src->svm_cid == vsk->remote_addr.svm_cid) 467 return true; 468 469 return transport->get_local_cid() == VMADDR_CID_LOCAL; 470 } 471 EXPORT_SYMBOL_GPL(vsock_check_source); 472 473 void vsock_remove_sock(struct vsock_sock *vsk) 474 { 475 /* Transport reassignment must not remove the binding. */ 476 if (sock_flag(sk_vsock(vsk), SOCK_DEAD)) 477 vsock_remove_bound(vsk); 478 479 vsock_remove_connected(vsk); 480 } 481 EXPORT_SYMBOL_GPL(vsock_remove_sock); 482 483 void vsock_for_each_connected_socket(struct vsock_transport *transport, 484 void (*fn)(struct sock *sk)) 485 { 486 int i; 487 488 spin_lock_bh(&vsock_table_lock); 489 490 for (i = 0; i < ARRAY_SIZE(vsock_connected_table); i++) { 491 struct vsock_sock *vsk; 492 list_for_each_entry(vsk, &vsock_connected_table[i], 493 connected_table) { 494 if (vsk->transport != transport) 495 continue; 496 497 fn(sk_vsock(vsk)); 498 } 499 } 500 501 spin_unlock_bh(&vsock_table_lock); 502 } 503 EXPORT_SYMBOL_GPL(vsock_for_each_connected_socket); 504 505 void vsock_add_pending(struct sock *listener, struct sock *pending) 506 { 507 struct vsock_sock *vlistener; 508 struct vsock_sock *vpending; 509 510 vlistener = vsock_sk(listener); 511 vpending = vsock_sk(pending); 512 513 sock_hold(pending); 514 sock_hold(listener); 515 list_add_tail(&vpending->pending_links, &vlistener->pending_links); 516 sk_acceptq_added(listener); 517 } 518 EXPORT_SYMBOL_GPL(vsock_add_pending); 519 520 void vsock_remove_pending(struct sock *listener, struct sock *pending) 521 { 522 struct vsock_sock *vpending = vsock_sk(pending); 523 524 list_del_init(&vpending->pending_links); 525 sock_put(listener); 526 sock_put(pending); 527 sk_acceptq_removed(listener); 528 } 529 EXPORT_SYMBOL_GPL(vsock_remove_pending); 530 531 void vsock_pending_to_accept(struct sock *listener, struct sock *pending) 532 { 533 struct vsock_sock *vpending = vsock_sk(pending); 534 struct vsock_sock *vlistener = vsock_sk(listener); 535 536 list_del_init(&vpending->pending_links); 537 list_add_tail(&vpending->accept_queue, &vlistener->accept_queue); 538 } 539 EXPORT_SYMBOL_GPL(vsock_pending_to_accept); 540 541 void vsock_enqueue_accept(struct sock *listener, struct sock *connected) 542 { 543 struct vsock_sock *vlistener; 544 struct vsock_sock *vconnected; 545 546 vlistener = vsock_sk(listener); 547 vconnected = vsock_sk(connected); 548 549 sock_hold(connected); 550 sock_hold(listener); 551 list_add_tail(&vconnected->accept_queue, &vlistener->accept_queue); 552 sk_acceptq_added(listener); 553 } 554 EXPORT_SYMBOL_GPL(vsock_enqueue_accept); 555 556 static bool vsock_use_local_transport(unsigned int remote_cid) 557 { 558 lockdep_assert_held(&vsock_register_mutex); 559 560 if (!transport_local) 561 return false; 562 563 if (remote_cid == VMADDR_CID_LOCAL) 564 return true; 565 566 if (transport_g2h) { 567 return remote_cid == transport_g2h->get_local_cid(); 568 } else { 569 return remote_cid == VMADDR_CID_HOST; 570 } 571 } 572 573 static void vsock_deassign_transport(struct vsock_sock *vsk) 574 { 575 if (!vsk->transport) 576 return; 577 578 vsk->transport->destruct(vsk); 579 module_put(vsk->transport->module); 580 vsk->transport = NULL; 581 } 582 583 /* Assign a transport to a socket and call the .init transport callback. 584 * 585 * Note: for connection oriented socket this must be called when vsk->remote_addr 586 * is set (e.g. during the connect() or when a connection request on a listener 587 * socket is received). 588 * The vsk->remote_addr is used to decide which transport to use: 589 * - remote CID == VMADDR_CID_LOCAL or g2h->local_cid or VMADDR_CID_HOST if 590 * g2h is not loaded, will use local transport; 591 * - remote CID <= VMADDR_CID_HOST or remote flags field includes 592 * VMADDR_FLAG_TO_HOST, will use guest->host transport; 593 * - remote CID > VMADDR_CID_HOST and h2g is loaded and h2g claims that CID, 594 * will use host->guest transport; 595 * - h2g not loaded or h2g does not claim that CID and g2h claims the CID via 596 * has_remote_cid, will use guest->host transport (when g2h_fallback=1) 597 * - anything else goes to h2g or returns -ENODEV if no h2g is available 598 */ 599 int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk) 600 { 601 const struct vsock_transport *new_transport; 602 struct sock *sk = sk_vsock(vsk); 603 unsigned int remote_cid = vsk->remote_addr.svm_cid; 604 __u8 remote_flags; 605 int ret; 606 607 /* If the packet is coming with the source and destination CIDs higher 608 * than VMADDR_CID_HOST, then a vsock channel where all the packets are 609 * forwarded to the host should be established. Then the host will 610 * need to forward the packets to the guest. 611 * 612 * The flag is set on the (listen) receive path (psk is not NULL). On 613 * the connect path the flag can be set by the user space application. 614 */ 615 if (psk && vsk->local_addr.svm_cid > VMADDR_CID_HOST && 616 vsk->remote_addr.svm_cid > VMADDR_CID_HOST) 617 vsk->remote_addr.svm_flags |= VMADDR_FLAG_TO_HOST; 618 619 remote_flags = vsk->remote_addr.svm_flags; 620 621 mutex_lock(&vsock_register_mutex); 622 623 switch (sk->sk_type) { 624 case SOCK_DGRAM: 625 new_transport = transport_dgram; 626 break; 627 case SOCK_STREAM: 628 case SOCK_SEQPACKET: 629 if (vsock_use_local_transport(remote_cid)) 630 new_transport = transport_local; 631 else if (remote_cid <= VMADDR_CID_HOST || 632 (remote_flags & VMADDR_FLAG_TO_HOST)) 633 new_transport = transport_g2h; 634 else if (transport_h2g && 635 (!transport_h2g->has_remote_cid || 636 transport_h2g->has_remote_cid(vsk, remote_cid))) 637 new_transport = transport_h2g; 638 else if (sock_net(sk)->vsock.g2h_fallback && 639 transport_g2h && transport_g2h->has_remote_cid && 640 transport_g2h->has_remote_cid(vsk, remote_cid)) { 641 vsk->remote_addr.svm_flags |= VMADDR_FLAG_TO_HOST; 642 new_transport = transport_g2h; 643 } else { 644 new_transport = transport_h2g; 645 } 646 break; 647 default: 648 ret = -ESOCKTNOSUPPORT; 649 goto err; 650 } 651 652 if (vsk->transport && vsk->transport == new_transport) { 653 ret = 0; 654 goto err; 655 } 656 657 /* We increase the module refcnt to prevent the transport unloading 658 * while there are open sockets assigned to it. 659 */ 660 if (!new_transport || !try_module_get(new_transport->module)) { 661 ret = -ENODEV; 662 goto err; 663 } 664 665 /* It's safe to release the mutex after a successful try_module_get(). 666 * Whichever transport `new_transport` points at, it won't go away until 667 * the last module_put() below or in vsock_deassign_transport(). 668 */ 669 mutex_unlock(&vsock_register_mutex); 670 671 if (vsk->transport) { 672 /* transport->release() must be called with sock lock acquired. 673 * This path can only be taken during vsock_connect(), where we 674 * have already held the sock lock. In the other cases, this 675 * function is called on a new socket which is not assigned to 676 * any transport. 677 */ 678 vsk->transport->release(vsk); 679 vsock_deassign_transport(vsk); 680 681 /* transport's release() and destruct() can touch some socket 682 * state, since we are reassigning the socket to a new transport 683 * during vsock_connect(), let's reset these fields to have a 684 * clean state. 685 */ 686 sock_reset_flag(sk, SOCK_DONE); 687 sk->sk_state = TCP_CLOSE; 688 WRITE_ONCE(vsk->peer_shutdown, 0); 689 } 690 691 if (sk->sk_type == SOCK_SEQPACKET) { 692 if (!new_transport->seqpacket_allow || 693 !new_transport->seqpacket_allow(vsk, remote_cid)) { 694 module_put(new_transport->module); 695 return -ESOCKTNOSUPPORT; 696 } 697 } 698 699 ret = new_transport->init(vsk, psk); 700 if (ret) { 701 module_put(new_transport->module); 702 return ret; 703 } 704 705 vsk->transport = new_transport; 706 707 return 0; 708 err: 709 mutex_unlock(&vsock_register_mutex); 710 return ret; 711 } 712 EXPORT_SYMBOL_GPL(vsock_assign_transport); 713 714 /* 715 * Provide safe access to static transport_{h2g,g2h,dgram,local} callbacks. 716 * Otherwise we may race with module removal. Do not use on `vsk->transport`. 717 */ 718 static u32 vsock_registered_transport_cid(const struct vsock_transport **transport) 719 { 720 u32 cid = VMADDR_CID_ANY; 721 722 mutex_lock(&vsock_register_mutex); 723 if (*transport) 724 cid = (*transport)->get_local_cid(); 725 mutex_unlock(&vsock_register_mutex); 726 727 return cid; 728 } 729 730 bool vsock_find_cid(unsigned int cid) 731 { 732 if (cid == vsock_registered_transport_cid(&transport_g2h)) 733 return true; 734 735 if (transport_h2g && cid == VMADDR_CID_HOST) 736 return true; 737 738 if (transport_local && cid == VMADDR_CID_LOCAL) 739 return true; 740 741 return false; 742 } 743 EXPORT_SYMBOL_GPL(vsock_find_cid); 744 745 static struct sock *vsock_dequeue_accept(struct sock *listener) 746 { 747 struct vsock_sock *vlistener; 748 struct vsock_sock *vconnected; 749 750 vlistener = vsock_sk(listener); 751 752 if (list_empty(&vlistener->accept_queue)) 753 return NULL; 754 755 vconnected = list_entry(vlistener->accept_queue.next, 756 struct vsock_sock, accept_queue); 757 758 list_del_init(&vconnected->accept_queue); 759 sock_put(listener); 760 /* The caller will need a reference on the connected socket so we let 761 * it call sock_put(). 762 */ 763 764 return sk_vsock(vconnected); 765 } 766 767 static bool vsock_is_accept_queue_empty(struct sock *sk) 768 { 769 struct vsock_sock *vsk = vsock_sk(sk); 770 return list_empty(&vsk->accept_queue); 771 } 772 773 static bool vsock_is_pending(struct sock *sk) 774 { 775 struct vsock_sock *vsk = vsock_sk(sk); 776 return !list_empty(&vsk->pending_links); 777 } 778 779 static int vsock_send_shutdown(struct sock *sk, int mode) 780 { 781 struct vsock_sock *vsk = vsock_sk(sk); 782 783 if (!vsk->transport) 784 return -ENODEV; 785 786 return vsk->transport->shutdown(vsk, mode); 787 } 788 789 static void vsock_pending_work(struct work_struct *work) 790 { 791 struct sock *sk; 792 struct sock *listener; 793 struct vsock_sock *vsk; 794 bool cleanup; 795 796 vsk = container_of(work, struct vsock_sock, pending_work.work); 797 sk = sk_vsock(vsk); 798 listener = vsk->listener; 799 cleanup = true; 800 801 lock_sock(listener); 802 lock_sock_nested(sk, SINGLE_DEPTH_NESTING); 803 804 if (vsock_is_pending(sk)) { 805 vsock_remove_pending(listener, sk); 806 } else { 807 /* We are not on the pending list so we must have been accepted 808 * by our user process. We just need to drop our references to 809 * the sockets and be on our way. 810 */ 811 cleanup = false; 812 goto out; 813 } 814 815 /* We need to remove ourself from the global connected sockets list so 816 * incoming packets can't find this socket, and to reduce the reference 817 * count. 818 */ 819 vsock_remove_connected(vsk); 820 821 sk->sk_state = TCP_CLOSE; 822 823 out: 824 release_sock(sk); 825 release_sock(listener); 826 if (cleanup) 827 sock_put(sk); 828 829 sock_put(sk); 830 sock_put(listener); 831 } 832 833 /**** SOCKET OPERATIONS ****/ 834 835 static int __vsock_bind_connectible(struct vsock_sock *vsk, 836 struct sockaddr_vm *addr) 837 { 838 struct net *net = sock_net(sk_vsock(vsk)); 839 struct sockaddr_vm new_addr; 840 841 if (!net->vsock.port) 842 net->vsock.port = get_random_u32_above(LAST_RESERVED_PORT); 843 844 vsock_addr_init(&new_addr, addr->svm_cid, addr->svm_port); 845 846 if (addr->svm_port == VMADDR_PORT_ANY) { 847 bool found = false; 848 unsigned int i; 849 850 for (i = 0; i < MAX_PORT_RETRIES; i++) { 851 if (net->vsock.port == VMADDR_PORT_ANY || 852 net->vsock.port <= LAST_RESERVED_PORT) 853 net->vsock.port = LAST_RESERVED_PORT + 1; 854 855 new_addr.svm_port = net->vsock.port++; 856 857 if (!__vsock_find_bound_socket_net(&new_addr, net)) { 858 found = true; 859 break; 860 } 861 } 862 863 if (!found) 864 return -EADDRNOTAVAIL; 865 } else { 866 /* If port is in reserved range, ensure caller 867 * has necessary privileges. 868 */ 869 if (addr->svm_port <= LAST_RESERVED_PORT && 870 !capable(CAP_NET_BIND_SERVICE)) { 871 return -EACCES; 872 } 873 874 if (__vsock_find_bound_socket_net(&new_addr, net)) 875 return -EADDRINUSE; 876 } 877 878 vsock_addr_init(&vsk->local_addr, new_addr.svm_cid, new_addr.svm_port); 879 880 /* Remove connection oriented sockets from the unbound list and add them 881 * to the hash table for easy lookup by its address. The unbound list 882 * is simply an extra entry at the end of the hash table, a trick used 883 * by AF_UNIX. 884 */ 885 __vsock_remove_bound(vsk); 886 __vsock_insert_bound(vsock_bound_sockets(&vsk->local_addr), vsk); 887 888 return 0; 889 } 890 891 static int __vsock_bind_dgram(struct vsock_sock *vsk, 892 struct sockaddr_vm *addr) 893 { 894 return vsk->transport->dgram_bind(vsk, addr); 895 } 896 897 static int __vsock_bind(struct sock *sk, struct sockaddr_vm *addr) 898 { 899 struct vsock_sock *vsk = vsock_sk(sk); 900 int retval; 901 902 /* First ensure this socket isn't already bound. */ 903 if (vsock_addr_bound(&vsk->local_addr)) 904 return -EINVAL; 905 906 /* Now bind to the provided address or select appropriate values if 907 * none are provided (VMADDR_CID_ANY and VMADDR_PORT_ANY). Note that 908 * like AF_INET prevents binding to a non-local IP address (in most 909 * cases), we only allow binding to a local CID. 910 */ 911 if (addr->svm_cid != VMADDR_CID_ANY && !vsock_find_cid(addr->svm_cid)) 912 return -EADDRNOTAVAIL; 913 914 switch (sk->sk_socket->type) { 915 case SOCK_STREAM: 916 case SOCK_SEQPACKET: 917 spin_lock_bh(&vsock_table_lock); 918 retval = __vsock_bind_connectible(vsk, addr); 919 spin_unlock_bh(&vsock_table_lock); 920 break; 921 922 case SOCK_DGRAM: 923 retval = __vsock_bind_dgram(vsk, addr); 924 break; 925 926 default: 927 retval = -EINVAL; 928 break; 929 } 930 931 return retval; 932 } 933 934 static void vsock_connect_timeout(struct work_struct *work); 935 936 static struct sock *__vsock_create(struct net *net, 937 struct socket *sock, 938 struct sock *parent, 939 gfp_t priority, 940 unsigned short type, 941 int kern) 942 { 943 struct sock *sk; 944 struct vsock_sock *psk; 945 struct vsock_sock *vsk; 946 947 sk = sk_alloc(net, AF_VSOCK, priority, &vsock_proto, kern); 948 if (!sk) 949 return NULL; 950 951 sock_init_data(sock, sk); 952 953 /* sk->sk_type is normally set in sock_init_data, but only if sock is 954 * non-NULL. We make sure that our sockets always have a type by 955 * setting it here if needed. 956 */ 957 if (!sock) 958 sk->sk_type = type; 959 960 vsk = vsock_sk(sk); 961 vsock_addr_init(&vsk->local_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY); 962 vsock_addr_init(&vsk->remote_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY); 963 964 sk->sk_destruct = vsock_sk_destruct; 965 sk->sk_backlog_rcv = vsock_queue_rcv_skb; 966 sock_reset_flag(sk, SOCK_DONE); 967 968 INIT_LIST_HEAD(&vsk->bound_table); 969 INIT_LIST_HEAD(&vsk->connected_table); 970 vsk->listener = NULL; 971 INIT_LIST_HEAD(&vsk->pending_links); 972 INIT_LIST_HEAD(&vsk->accept_queue); 973 vsk->sent_request = false; 974 vsk->ignore_connecting_rst = false; 975 WRITE_ONCE(vsk->peer_shutdown, 0); 976 INIT_DELAYED_WORK(&vsk->connect_work, vsock_connect_timeout); 977 INIT_DELAYED_WORK(&vsk->pending_work, vsock_pending_work); 978 979 psk = parent ? vsock_sk(parent) : NULL; 980 if (parent) { 981 vsk->trusted = psk->trusted; 982 vsk->owner = get_cred(psk->owner); 983 vsk->connect_timeout = psk->connect_timeout; 984 vsk->buffer_size = psk->buffer_size; 985 vsk->buffer_min_size = psk->buffer_min_size; 986 vsk->buffer_max_size = psk->buffer_max_size; 987 security_sk_clone(parent, sk); 988 } else { 989 vsk->trusted = ns_capable_noaudit(&init_user_ns, CAP_NET_ADMIN); 990 vsk->owner = get_current_cred(); 991 vsk->connect_timeout = VSOCK_DEFAULT_CONNECT_TIMEOUT; 992 vsk->buffer_size = VSOCK_DEFAULT_BUFFER_SIZE; 993 vsk->buffer_min_size = VSOCK_DEFAULT_BUFFER_MIN_SIZE; 994 vsk->buffer_max_size = VSOCK_DEFAULT_BUFFER_MAX_SIZE; 995 } 996 997 return sk; 998 } 999 1000 static bool sock_type_connectible(u16 type) 1001 { 1002 return (type == SOCK_STREAM) || (type == SOCK_SEQPACKET); 1003 } 1004 1005 static void __vsock_release(struct sock *sk, int level) 1006 { 1007 struct vsock_sock *vsk; 1008 struct sock *pending; 1009 1010 vsk = vsock_sk(sk); 1011 pending = NULL; /* Compiler warning. */ 1012 1013 /* When "level" is SINGLE_DEPTH_NESTING, use the nested 1014 * version to avoid the warning "possible recursive locking 1015 * detected". When "level" is 0, lock_sock_nested(sk, level) 1016 * is the same as lock_sock(sk). 1017 */ 1018 lock_sock_nested(sk, level); 1019 1020 /* Indicate to vsock_remove_sock() that the socket is being released and 1021 * can be removed from the bound_table. Unlike transport reassignment 1022 * case, where the socket must remain bound despite vsock_remove_sock() 1023 * being called from the transport release() callback. 1024 */ 1025 sock_set_flag(sk, SOCK_DEAD); 1026 1027 if (vsk->transport) 1028 vsk->transport->release(vsk); 1029 else if (sock_type_connectible(sk->sk_type)) 1030 vsock_remove_sock(vsk); 1031 1032 sock_orphan(sk); 1033 sk->sk_shutdown = SHUTDOWN_MASK; 1034 1035 skb_queue_purge(&sk->sk_receive_queue); 1036 1037 /* Clean up any sockets that never were accepted. */ 1038 while ((pending = vsock_dequeue_accept(sk)) != NULL) { 1039 __vsock_release(pending, SINGLE_DEPTH_NESTING); 1040 sock_put(pending); 1041 } 1042 1043 release_sock(sk); 1044 sock_put(sk); 1045 } 1046 1047 static void vsock_sk_destruct(struct sock *sk) 1048 { 1049 struct vsock_sock *vsk = vsock_sk(sk); 1050 1051 /* Flush MSG_ZEROCOPY leftovers. */ 1052 __skb_queue_purge(&sk->sk_error_queue); 1053 1054 vsock_deassign_transport(vsk); 1055 1056 /* When clearing these addresses, there's no need to set the family and 1057 * possibly register the address family with the kernel. 1058 */ 1059 vsock_addr_init(&vsk->local_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY); 1060 vsock_addr_init(&vsk->remote_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY); 1061 1062 put_cred(vsk->owner); 1063 } 1064 1065 static int vsock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 1066 { 1067 int err; 1068 1069 err = sock_queue_rcv_skb(sk, skb); 1070 if (err) 1071 kfree_skb(skb); 1072 1073 return err; 1074 } 1075 1076 struct sock *vsock_create_connected(struct sock *parent) 1077 { 1078 return __vsock_create(sock_net(parent), NULL, parent, GFP_KERNEL, 1079 parent->sk_type, 0); 1080 } 1081 EXPORT_SYMBOL_GPL(vsock_create_connected); 1082 1083 s64 vsock_stream_has_data(struct vsock_sock *vsk) 1084 { 1085 if (WARN_ON(!vsk->transport)) 1086 return 0; 1087 1088 return vsk->transport->stream_has_data(vsk); 1089 } 1090 EXPORT_SYMBOL_GPL(vsock_stream_has_data); 1091 1092 s64 vsock_connectible_has_data(struct vsock_sock *vsk) 1093 { 1094 struct sock *sk = sk_vsock(vsk); 1095 1096 if (WARN_ON(!vsk->transport)) 1097 return 0; 1098 1099 if (sk->sk_type == SOCK_SEQPACKET) 1100 return vsk->transport->seqpacket_has_data(vsk); 1101 else 1102 return vsock_stream_has_data(vsk); 1103 } 1104 EXPORT_SYMBOL_GPL(vsock_connectible_has_data); 1105 1106 s64 vsock_stream_has_space(struct vsock_sock *vsk) 1107 { 1108 if (WARN_ON(!vsk->transport)) 1109 return 0; 1110 1111 return vsk->transport->stream_has_space(vsk); 1112 } 1113 EXPORT_SYMBOL_GPL(vsock_stream_has_space); 1114 1115 void vsock_data_ready(struct sock *sk) 1116 { 1117 struct vsock_sock *vsk = vsock_sk(sk); 1118 1119 if (vsock_stream_has_data(vsk) >= sk->sk_rcvlowat || 1120 sock_flag(sk, SOCK_DONE)) 1121 sk->sk_data_ready(sk); 1122 } 1123 EXPORT_SYMBOL_GPL(vsock_data_ready); 1124 1125 /* Dummy callback required by sockmap. 1126 * See unconditional call of saved_close() in sock_map_close(). 1127 */ 1128 static void vsock_close(struct sock *sk, long timeout) 1129 { 1130 } 1131 1132 static int vsock_release(struct socket *sock) 1133 { 1134 struct sock *sk = sock->sk; 1135 1136 if (!sk) 1137 return 0; 1138 1139 sk->sk_prot->close(sk, 0); 1140 __vsock_release(sk, 0); 1141 sock->sk = NULL; 1142 sock->state = SS_FREE; 1143 1144 return 0; 1145 } 1146 1147 static int 1148 vsock_bind(struct socket *sock, struct sockaddr_unsized *addr, int addr_len) 1149 { 1150 int err; 1151 struct sock *sk; 1152 struct sockaddr_vm *vm_addr; 1153 1154 sk = sock->sk; 1155 1156 if (vsock_addr_cast(addr, addr_len, &vm_addr) != 0) 1157 return -EINVAL; 1158 1159 lock_sock(sk); 1160 err = __vsock_bind(sk, vm_addr); 1161 release_sock(sk); 1162 1163 return err; 1164 } 1165 1166 static int vsock_getname(struct socket *sock, 1167 struct sockaddr *addr, int peer) 1168 { 1169 int err; 1170 struct sock *sk; 1171 struct vsock_sock *vsk; 1172 struct sockaddr_vm *vm_addr; 1173 1174 sk = sock->sk; 1175 vsk = vsock_sk(sk); 1176 err = 0; 1177 1178 lock_sock(sk); 1179 1180 if (peer) { 1181 if (sock->state != SS_CONNECTED) { 1182 err = -ENOTCONN; 1183 goto out; 1184 } 1185 vm_addr = &vsk->remote_addr; 1186 } else { 1187 vm_addr = &vsk->local_addr; 1188 } 1189 1190 BUILD_BUG_ON(sizeof(*vm_addr) > sizeof(struct sockaddr_storage)); 1191 memcpy(addr, vm_addr, sizeof(*vm_addr)); 1192 err = sizeof(*vm_addr); 1193 1194 out: 1195 release_sock(sk); 1196 return err; 1197 } 1198 1199 void vsock_linger(struct sock *sk) 1200 { 1201 DEFINE_WAIT_FUNC(wait, woken_wake_function); 1202 ssize_t (*unsent)(struct vsock_sock *vsk); 1203 struct vsock_sock *vsk = vsock_sk(sk); 1204 long timeout; 1205 1206 if (!sock_flag(sk, SOCK_LINGER)) 1207 return; 1208 1209 timeout = sk->sk_lingertime; 1210 if (!timeout) 1211 return; 1212 1213 /* Transports must implement `unsent_bytes` if they want to support 1214 * SOCK_LINGER through `vsock_linger()` since we use it to check when 1215 * the socket can be closed. 1216 */ 1217 unsent = vsk->transport->unsent_bytes; 1218 if (!unsent) 1219 return; 1220 1221 add_wait_queue(sk_sleep(sk), &wait); 1222 1223 do { 1224 if (sk_wait_event(sk, &timeout, unsent(vsk) == 0, &wait)) 1225 break; 1226 } while (!signal_pending(current) && timeout); 1227 1228 remove_wait_queue(sk_sleep(sk), &wait); 1229 } 1230 EXPORT_SYMBOL_GPL(vsock_linger); 1231 1232 static int vsock_shutdown(struct socket *sock, int mode) 1233 { 1234 int err; 1235 struct sock *sk; 1236 1237 /* User level uses SHUT_RD (0) and SHUT_WR (1), but the kernel uses 1238 * RCV_SHUTDOWN (1) and SEND_SHUTDOWN (2), so we must increment mode 1239 * here like the other address families do. Note also that the 1240 * increment makes SHUT_RDWR (2) into RCV_SHUTDOWN | SEND_SHUTDOWN (3), 1241 * which is what we want. 1242 */ 1243 mode++; 1244 1245 if ((mode & ~SHUTDOWN_MASK) || !mode) 1246 return -EINVAL; 1247 1248 /* If this is a connection oriented socket and it is not connected then 1249 * bail out immediately. If it is a DGRAM socket then we must first 1250 * kick the socket so that it wakes up from any sleeping calls, for 1251 * example recv(), and then afterwards return the error. 1252 */ 1253 1254 sk = sock->sk; 1255 1256 lock_sock(sk); 1257 if (sock->state == SS_UNCONNECTED) { 1258 err = -ENOTCONN; 1259 if (sock_type_connectible(sk->sk_type)) 1260 goto out; 1261 } else { 1262 sock->state = SS_DISCONNECTING; 1263 err = 0; 1264 } 1265 1266 /* Receive and send shutdowns are treated alike. */ 1267 mode = mode & (RCV_SHUTDOWN | SEND_SHUTDOWN); 1268 if (mode) { 1269 sk->sk_shutdown |= mode; 1270 sk->sk_state_change(sk); 1271 1272 if (sock_type_connectible(sk->sk_type)) { 1273 sock_reset_flag(sk, SOCK_DONE); 1274 vsock_send_shutdown(sk, mode); 1275 } 1276 } 1277 1278 out: 1279 release_sock(sk); 1280 return err; 1281 } 1282 1283 static __poll_t vsock_poll_shutdown(struct sock *sk, u32 peer_shutdown) 1284 { 1285 __poll_t mask = 0; 1286 1287 /* INET sockets treat local write shutdown and peer write shutdown as a 1288 * case of EPOLLHUP set. 1289 */ 1290 if (sk->sk_shutdown == SHUTDOWN_MASK || 1291 ((sk->sk_shutdown & SEND_SHUTDOWN) && 1292 (peer_shutdown & SEND_SHUTDOWN))) 1293 mask |= EPOLLHUP; 1294 1295 if (sk->sk_shutdown & RCV_SHUTDOWN || 1296 peer_shutdown & SEND_SHUTDOWN) 1297 mask |= EPOLLRDHUP; 1298 1299 return mask; 1300 } 1301 1302 static __poll_t vsock_poll(struct file *file, struct socket *sock, 1303 poll_table *wait) 1304 { 1305 struct sock *sk; 1306 __poll_t mask; 1307 struct vsock_sock *vsk; 1308 1309 sk = sock->sk; 1310 vsk = vsock_sk(sk); 1311 1312 poll_wait(file, sk_sleep(sk), wait); 1313 mask = 0; 1314 1315 if (sk->sk_err || !skb_queue_empty_lockless(&sk->sk_error_queue)) 1316 /* Signify that there has been an error on this socket. */ 1317 mask |= EPOLLERR; 1318 1319 if (sk_is_readable(sk)) 1320 mask |= EPOLLIN | EPOLLRDNORM; 1321 1322 if (sock->type == SOCK_DGRAM) { 1323 u32 peer_shutdown = READ_ONCE(vsk->peer_shutdown); 1324 1325 /* DGRAM sockets do not take lock_sock() in poll(), so use one 1326 * lockless snapshot for all shutdown-derived mask bits. 1327 */ 1328 mask |= vsock_poll_shutdown(sk, peer_shutdown); 1329 1330 /* For datagram sockets we can read if there is something in 1331 * the queue and write as long as the socket isn't shutdown for 1332 * sending. 1333 */ 1334 if (!skb_queue_empty_lockless(&sk->sk_receive_queue) || 1335 (sk->sk_shutdown & RCV_SHUTDOWN)) { 1336 mask |= EPOLLIN | EPOLLRDNORM; 1337 } 1338 1339 if (!(sk->sk_shutdown & SEND_SHUTDOWN)) 1340 mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND; 1341 1342 } else if (sock_type_connectible(sk->sk_type)) { 1343 const struct vsock_transport *transport; 1344 u32 peer_shutdown; 1345 1346 lock_sock(sk); 1347 1348 transport = vsk->transport; 1349 1350 /* Listening sockets that have connections in their accept 1351 * queue can be read. 1352 */ 1353 if (sk->sk_state == TCP_LISTEN 1354 && !vsock_is_accept_queue_empty(sk)) 1355 mask |= EPOLLIN | EPOLLRDNORM; 1356 1357 /* If there is something in the queue then we can read. */ 1358 if (transport && transport->stream_is_active(vsk) && 1359 !(sk->sk_shutdown & RCV_SHUTDOWN)) { 1360 bool data_ready_now = false; 1361 int target = sock_rcvlowat(sk, 0, INT_MAX); 1362 int ret = transport->notify_poll_in( 1363 vsk, target, &data_ready_now); 1364 if (ret < 0) { 1365 mask |= EPOLLERR; 1366 } else { 1367 if (data_ready_now) 1368 mask |= EPOLLIN | EPOLLRDNORM; 1369 1370 } 1371 } 1372 1373 /* Sockets whose connections have been closed, reset, or 1374 * terminated should also be considered read, and we check the 1375 * shutdown flag for that. 1376 */ 1377 peer_shutdown = READ_ONCE(vsk->peer_shutdown); 1378 mask |= vsock_poll_shutdown(sk, peer_shutdown); 1379 if (sk->sk_shutdown & RCV_SHUTDOWN || 1380 peer_shutdown & SEND_SHUTDOWN) { 1381 mask |= EPOLLIN | EPOLLRDNORM; 1382 } 1383 1384 /* Connected sockets that can produce data can be written. */ 1385 if (transport && sk->sk_state == TCP_ESTABLISHED) { 1386 if (!(sk->sk_shutdown & SEND_SHUTDOWN)) { 1387 bool space_avail_now = false; 1388 int ret = transport->notify_poll_out( 1389 vsk, 1, &space_avail_now); 1390 if (ret < 0) { 1391 mask |= EPOLLERR; 1392 } else { 1393 if (space_avail_now) 1394 /* Remove EPOLLWRBAND since INET 1395 * sockets are not setting it. 1396 */ 1397 mask |= EPOLLOUT | EPOLLWRNORM; 1398 1399 } 1400 } 1401 } 1402 1403 /* Simulate INET socket poll behaviors, which sets 1404 * EPOLLOUT|EPOLLWRNORM when peer is closed and nothing to read, 1405 * but local send is not shutdown. 1406 */ 1407 if (sk->sk_state == TCP_CLOSE || sk->sk_state == TCP_CLOSING) { 1408 if (!(sk->sk_shutdown & SEND_SHUTDOWN)) 1409 mask |= EPOLLOUT | EPOLLWRNORM; 1410 1411 } 1412 1413 release_sock(sk); 1414 } 1415 1416 return mask; 1417 } 1418 1419 static int vsock_read_skb(struct sock *sk, skb_read_actor_t read_actor) 1420 { 1421 struct vsock_sock *vsk = vsock_sk(sk); 1422 1423 if (WARN_ON_ONCE(!vsk->transport)) 1424 return -ENODEV; 1425 1426 return vsk->transport->read_skb(vsk, read_actor); 1427 } 1428 1429 static int vsock_dgram_sendmsg(struct socket *sock, struct msghdr *msg, 1430 size_t len) 1431 { 1432 int err; 1433 struct sock *sk; 1434 struct vsock_sock *vsk; 1435 struct sockaddr_vm *remote_addr; 1436 const struct vsock_transport *transport; 1437 1438 if (msg->msg_flags & MSG_OOB) 1439 return -EOPNOTSUPP; 1440 1441 /* For now, MSG_DONTWAIT is always assumed... */ 1442 err = 0; 1443 sk = sock->sk; 1444 vsk = vsock_sk(sk); 1445 1446 lock_sock(sk); 1447 1448 transport = vsk->transport; 1449 1450 err = vsock_auto_bind(vsk); 1451 if (err) 1452 goto out; 1453 1454 1455 /* If the provided message contains an address, use that. Otherwise 1456 * fall back on the socket's remote handle (if it has been connected). 1457 */ 1458 if (msg->msg_name && 1459 vsock_addr_cast(msg->msg_name, msg->msg_namelen, 1460 &remote_addr) == 0) { 1461 /* Ensure this address is of the right type and is a valid 1462 * destination. 1463 */ 1464 1465 if (remote_addr->svm_cid == VMADDR_CID_ANY) 1466 remote_addr->svm_cid = transport->get_local_cid(); 1467 1468 if (!vsock_addr_bound(remote_addr)) { 1469 err = -EINVAL; 1470 goto out; 1471 } 1472 } else if (sock->state == SS_CONNECTED) { 1473 remote_addr = &vsk->remote_addr; 1474 1475 if (remote_addr->svm_cid == VMADDR_CID_ANY) 1476 remote_addr->svm_cid = transport->get_local_cid(); 1477 1478 /* XXX Should connect() or this function ensure remote_addr is 1479 * bound? 1480 */ 1481 if (!vsock_addr_bound(&vsk->remote_addr)) { 1482 err = -EINVAL; 1483 goto out; 1484 } 1485 } else { 1486 err = -EINVAL; 1487 goto out; 1488 } 1489 1490 if (!transport->dgram_allow(vsk, remote_addr->svm_cid, 1491 remote_addr->svm_port)) { 1492 err = -EINVAL; 1493 goto out; 1494 } 1495 1496 err = transport->dgram_enqueue(vsk, remote_addr, msg, len); 1497 1498 out: 1499 release_sock(sk); 1500 return err; 1501 } 1502 1503 static int vsock_dgram_connect(struct socket *sock, 1504 struct sockaddr_unsized *addr, int addr_len, int flags) 1505 { 1506 int err; 1507 struct sock *sk; 1508 struct vsock_sock *vsk; 1509 struct sockaddr_vm *remote_addr; 1510 1511 sk = sock->sk; 1512 vsk = vsock_sk(sk); 1513 1514 err = vsock_addr_cast(addr, addr_len, &remote_addr); 1515 if (err == -EAFNOSUPPORT && remote_addr->svm_family == AF_UNSPEC) { 1516 lock_sock(sk); 1517 vsock_addr_init(&vsk->remote_addr, VMADDR_CID_ANY, 1518 VMADDR_PORT_ANY); 1519 sock->state = SS_UNCONNECTED; 1520 release_sock(sk); 1521 return 0; 1522 } else if (err != 0) 1523 return -EINVAL; 1524 1525 lock_sock(sk); 1526 1527 err = vsock_auto_bind(vsk); 1528 if (err) 1529 goto out; 1530 1531 if (!vsk->transport->dgram_allow(vsk, remote_addr->svm_cid, 1532 remote_addr->svm_port)) { 1533 err = -EINVAL; 1534 goto out; 1535 } 1536 1537 memcpy(&vsk->remote_addr, remote_addr, sizeof(vsk->remote_addr)); 1538 sock->state = SS_CONNECTED; 1539 1540 /* sock map disallows redirection of non-TCP sockets with sk_state != 1541 * TCP_ESTABLISHED (see sock_map_redirect_allowed()), so we set 1542 * TCP_ESTABLISHED here to allow redirection of connected vsock dgrams. 1543 * 1544 * This doesn't seem to be abnormal state for datagram sockets, as the 1545 * same approach can be see in other datagram socket types as well 1546 * (such as unix sockets). 1547 */ 1548 sk->sk_state = TCP_ESTABLISHED; 1549 1550 out: 1551 release_sock(sk); 1552 return err; 1553 } 1554 1555 int __vsock_dgram_recvmsg(struct socket *sock, struct msghdr *msg, 1556 size_t len, int flags) 1557 { 1558 struct sock *sk = sock->sk; 1559 struct vsock_sock *vsk = vsock_sk(sk); 1560 1561 return vsk->transport->dgram_dequeue(vsk, msg, len, flags); 1562 } 1563 1564 int vsock_dgram_recvmsg(struct socket *sock, struct msghdr *msg, 1565 size_t len, int flags) 1566 { 1567 #ifdef CONFIG_BPF_SYSCALL 1568 struct sock *sk = sock->sk; 1569 const struct proto *prot; 1570 1571 prot = READ_ONCE(sk->sk_prot); 1572 if (prot != &vsock_proto) 1573 return prot->recvmsg(sk, msg, len, flags); 1574 #endif 1575 1576 return __vsock_dgram_recvmsg(sock, msg, len, flags); 1577 } 1578 EXPORT_SYMBOL_GPL(vsock_dgram_recvmsg); 1579 1580 static int vsock_do_ioctl(struct socket *sock, unsigned int cmd, 1581 int __user *arg) 1582 { 1583 struct sock *sk = sock->sk; 1584 struct vsock_sock *vsk; 1585 int ret; 1586 1587 vsk = vsock_sk(sk); 1588 1589 switch (cmd) { 1590 case SIOCINQ: { 1591 ssize_t n_bytes; 1592 1593 if (!vsk->transport) { 1594 ret = -EOPNOTSUPP; 1595 break; 1596 } 1597 1598 if (sock_type_connectible(sk->sk_type) && 1599 sk->sk_state == TCP_LISTEN) { 1600 ret = -EINVAL; 1601 break; 1602 } 1603 1604 n_bytes = vsock_stream_has_data(vsk); 1605 if (n_bytes < 0) { 1606 ret = n_bytes; 1607 break; 1608 } 1609 ret = put_user(n_bytes, arg); 1610 break; 1611 } 1612 case SIOCOUTQ: { 1613 ssize_t n_bytes; 1614 1615 if (!vsk->transport || !vsk->transport->unsent_bytes) { 1616 ret = -EOPNOTSUPP; 1617 break; 1618 } 1619 1620 if (sock_type_connectible(sk->sk_type) && sk->sk_state == TCP_LISTEN) { 1621 ret = -EINVAL; 1622 break; 1623 } 1624 1625 n_bytes = vsk->transport->unsent_bytes(vsk); 1626 if (n_bytes < 0) { 1627 ret = n_bytes; 1628 break; 1629 } 1630 1631 ret = put_user(n_bytes, arg); 1632 break; 1633 } 1634 default: 1635 ret = -ENOIOCTLCMD; 1636 } 1637 1638 return ret; 1639 } 1640 1641 static int vsock_ioctl(struct socket *sock, unsigned int cmd, 1642 unsigned long arg) 1643 { 1644 int ret; 1645 1646 lock_sock(sock->sk); 1647 ret = vsock_do_ioctl(sock, cmd, (int __user *)arg); 1648 release_sock(sock->sk); 1649 1650 return ret; 1651 } 1652 1653 static const struct proto_ops vsock_dgram_ops = { 1654 .family = PF_VSOCK, 1655 .owner = THIS_MODULE, 1656 .release = vsock_release, 1657 .bind = vsock_bind, 1658 .connect = vsock_dgram_connect, 1659 .socketpair = sock_no_socketpair, 1660 .accept = sock_no_accept, 1661 .getname = vsock_getname, 1662 .poll = vsock_poll, 1663 .ioctl = vsock_ioctl, 1664 .listen = sock_no_listen, 1665 .shutdown = vsock_shutdown, 1666 .sendmsg = vsock_dgram_sendmsg, 1667 .recvmsg = vsock_dgram_recvmsg, 1668 .mmap = sock_no_mmap, 1669 .read_skb = vsock_read_skb, 1670 }; 1671 1672 static int vsock_transport_cancel_pkt(struct vsock_sock *vsk) 1673 { 1674 const struct vsock_transport *transport = vsk->transport; 1675 1676 if (!transport || !transport->cancel_pkt) 1677 return -EOPNOTSUPP; 1678 1679 return transport->cancel_pkt(vsk); 1680 } 1681 1682 static void vsock_connect_timeout(struct work_struct *work) 1683 { 1684 struct sock *sk; 1685 struct vsock_sock *vsk; 1686 1687 vsk = container_of(work, struct vsock_sock, connect_work.work); 1688 sk = sk_vsock(vsk); 1689 1690 lock_sock(sk); 1691 if (sk->sk_state == TCP_SYN_SENT && 1692 (sk->sk_shutdown != SHUTDOWN_MASK)) { 1693 sk->sk_state = TCP_CLOSE; 1694 sk->sk_socket->state = SS_UNCONNECTED; 1695 sk->sk_err = ETIMEDOUT; 1696 sk_error_report(sk); 1697 vsock_transport_cancel_pkt(vsk); 1698 } 1699 release_sock(sk); 1700 1701 sock_put(sk); 1702 } 1703 1704 static int vsock_connect(struct socket *sock, struct sockaddr_unsized *addr, 1705 int addr_len, int flags) 1706 { 1707 int err; 1708 struct sock *sk; 1709 struct vsock_sock *vsk; 1710 const struct vsock_transport *transport; 1711 struct sockaddr_vm *remote_addr; 1712 long timeout; 1713 DEFINE_WAIT(wait); 1714 1715 err = 0; 1716 sk = sock->sk; 1717 vsk = vsock_sk(sk); 1718 1719 lock_sock(sk); 1720 1721 /* XXX AF_UNSPEC should make us disconnect like AF_INET. */ 1722 switch (sock->state) { 1723 case SS_CONNECTED: 1724 err = -EISCONN; 1725 goto out; 1726 case SS_DISCONNECTING: 1727 err = -EINVAL; 1728 goto out; 1729 case SS_CONNECTING: 1730 /* This continues on so we can move sock into the SS_CONNECTED 1731 * state once the connection has completed (at which point err 1732 * will be set to zero also). Otherwise, we will either wait 1733 * for the connection or return -EALREADY should this be a 1734 * non-blocking call. 1735 */ 1736 err = -EALREADY; 1737 if (flags & O_NONBLOCK) 1738 goto out; 1739 break; 1740 default: 1741 if ((sk->sk_state == TCP_LISTEN) || 1742 vsock_addr_cast(addr, addr_len, &remote_addr) != 0) { 1743 err = -EINVAL; 1744 goto out; 1745 } 1746 1747 /* Set the remote address that we are connecting to. */ 1748 memcpy(&vsk->remote_addr, remote_addr, 1749 sizeof(vsk->remote_addr)); 1750 1751 err = vsock_assign_transport(vsk, NULL); 1752 if (err) 1753 goto out; 1754 1755 transport = vsk->transport; 1756 1757 /* The hypervisor and well-known contexts do not have socket 1758 * endpoints. 1759 */ 1760 if (!transport || 1761 !transport->stream_allow(vsk, remote_addr->svm_cid, 1762 remote_addr->svm_port)) { 1763 err = -ENETUNREACH; 1764 goto out; 1765 } 1766 1767 if (vsock_msgzerocopy_allow(transport)) { 1768 set_bit(SOCK_SUPPORT_ZC, &sk->sk_socket->flags); 1769 } else if (sock_flag(sk, SOCK_ZEROCOPY)) { 1770 /* If this option was set before 'connect()', 1771 * when transport was unknown, check that this 1772 * feature is supported here. 1773 */ 1774 err = -EOPNOTSUPP; 1775 goto out; 1776 } 1777 1778 err = vsock_auto_bind(vsk); 1779 if (err) 1780 goto out; 1781 1782 sk->sk_state = TCP_SYN_SENT; 1783 1784 err = transport->connect(vsk); 1785 if (err < 0) 1786 goto out; 1787 1788 /* sk_err might have been set as a result of an earlier 1789 * (failed) connect attempt. 1790 */ 1791 sk->sk_err = 0; 1792 1793 /* Mark sock as connecting and set the error code to in 1794 * progress in case this is a non-blocking connect. 1795 */ 1796 sock->state = SS_CONNECTING; 1797 err = -EINPROGRESS; 1798 } 1799 1800 /* The receive path will handle all communication until we are able to 1801 * enter the connected state. Here we wait for the connection to be 1802 * completed or a notification of an error. 1803 */ 1804 timeout = vsk->connect_timeout; 1805 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); 1806 1807 /* If the socket is already closing or it is in an error state, there 1808 * is no point in waiting. 1809 */ 1810 while (sk->sk_state != TCP_ESTABLISHED && 1811 sk->sk_state != TCP_CLOSING && sk->sk_err == 0) { 1812 if (flags & O_NONBLOCK) { 1813 /* If we're not going to block, we schedule a timeout 1814 * function to generate a timeout on the connection 1815 * attempt, in case the peer doesn't respond in a 1816 * timely manner. We hold on to the socket until the 1817 * timeout fires. 1818 */ 1819 sock_hold(sk); 1820 1821 /* If the timeout function is already scheduled, 1822 * reschedule it, then ungrab the socket refcount to 1823 * keep it balanced. 1824 */ 1825 if (mod_delayed_work(system_percpu_wq, &vsk->connect_work, 1826 timeout)) 1827 sock_put(sk); 1828 1829 /* Skip ahead to preserve error code set above. */ 1830 goto out_wait; 1831 } 1832 1833 release_sock(sk); 1834 timeout = schedule_timeout(timeout); 1835 lock_sock(sk); 1836 1837 /* Connection established. Whatever happens to socket once we 1838 * release it, that's not connect()'s concern. No need to go 1839 * into signal and timeout handling. Call it a day. 1840 * 1841 * Note that allowing to "reset" an already established socket 1842 * here is racy and insecure. 1843 */ 1844 if (sk->sk_state == TCP_ESTABLISHED) 1845 break; 1846 1847 /* If connection was _not_ established and a signal/timeout came 1848 * to be, we want the socket's state reset. User space may want 1849 * to retry. 1850 * 1851 * sk_state != TCP_ESTABLISHED implies that socket is not on 1852 * vsock_connected_table. We keep the binding and the transport 1853 * assigned. 1854 */ 1855 if (signal_pending(current) || timeout == 0) { 1856 err = timeout == 0 ? -ETIMEDOUT : sock_intr_errno(timeout); 1857 1858 /* Listener might have already responded with 1859 * VIRTIO_VSOCK_OP_RESPONSE. Its handling expects our 1860 * sk_state == TCP_SYN_SENT, which hereby we break. 1861 * In such case VIRTIO_VSOCK_OP_RST will follow. 1862 */ 1863 sk->sk_state = TCP_CLOSE; 1864 sock->state = SS_UNCONNECTED; 1865 1866 /* Try to cancel VIRTIO_VSOCK_OP_REQUEST skb sent out by 1867 * transport->connect(). 1868 */ 1869 vsock_transport_cancel_pkt(vsk); 1870 1871 goto out_wait; 1872 } 1873 1874 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); 1875 } 1876 1877 err = sock_error(sk); 1878 if (err) { 1879 sk->sk_state = TCP_CLOSE; 1880 sock->state = SS_UNCONNECTED; 1881 } 1882 1883 out_wait: 1884 finish_wait(sk_sleep(sk), &wait); 1885 out: 1886 release_sock(sk); 1887 return err; 1888 } 1889 1890 static int vsock_accept(struct socket *sock, struct socket *newsock, 1891 struct proto_accept_arg *arg) 1892 { 1893 struct sock *listener; 1894 int err; 1895 struct sock *connected; 1896 struct vsock_sock *vconnected; 1897 long timeout; 1898 DEFINE_WAIT(wait); 1899 1900 err = 0; 1901 listener = sock->sk; 1902 1903 lock_sock(listener); 1904 1905 if (!sock_type_connectible(sock->type)) { 1906 err = -EOPNOTSUPP; 1907 goto out; 1908 } 1909 1910 if (listener->sk_state != TCP_LISTEN) { 1911 err = -EINVAL; 1912 goto out; 1913 } 1914 1915 /* Wait for children sockets to appear; these are the new sockets 1916 * created upon connection establishment. 1917 */ 1918 timeout = sock_rcvtimeo(listener, arg->flags & O_NONBLOCK); 1919 1920 while ((connected = vsock_dequeue_accept(listener)) == NULL && 1921 timeout != 0) { 1922 prepare_to_wait(sk_sleep(listener), &wait, TASK_INTERRUPTIBLE); 1923 release_sock(listener); 1924 timeout = schedule_timeout(timeout); 1925 finish_wait(sk_sleep(listener), &wait); 1926 lock_sock(listener); 1927 1928 if (signal_pending(current)) { 1929 err = sock_intr_errno(timeout); 1930 goto out; 1931 } 1932 } 1933 1934 if (!connected) { 1935 err = -EAGAIN; 1936 } else { 1937 sk_acceptq_removed(listener); 1938 1939 lock_sock_nested(connected, SINGLE_DEPTH_NESTING); 1940 vconnected = vsock_sk(connected); 1941 1942 newsock->state = SS_CONNECTED; 1943 sock_graft(connected, newsock); 1944 1945 set_bit(SOCK_CUSTOM_SOCKOPT, 1946 &connected->sk_socket->flags); 1947 1948 if (vsock_msgzerocopy_allow(vconnected->transport)) 1949 set_bit(SOCK_SUPPORT_ZC, 1950 &connected->sk_socket->flags); 1951 1952 release_sock(connected); 1953 sock_put(connected); 1954 } 1955 1956 out: 1957 release_sock(listener); 1958 return err; 1959 } 1960 1961 static int vsock_listen(struct socket *sock, int backlog) 1962 { 1963 int err; 1964 struct sock *sk; 1965 struct vsock_sock *vsk; 1966 1967 sk = sock->sk; 1968 1969 lock_sock(sk); 1970 1971 if (!sock_type_connectible(sk->sk_type)) { 1972 err = -EOPNOTSUPP; 1973 goto out; 1974 } 1975 1976 if (sock->state != SS_UNCONNECTED) { 1977 err = -EINVAL; 1978 goto out; 1979 } 1980 1981 vsk = vsock_sk(sk); 1982 1983 if (!vsock_addr_bound(&vsk->local_addr)) { 1984 err = -EINVAL; 1985 goto out; 1986 } 1987 1988 sk->sk_max_ack_backlog = backlog; 1989 sk->sk_state = TCP_LISTEN; 1990 1991 err = 0; 1992 1993 out: 1994 release_sock(sk); 1995 return err; 1996 } 1997 1998 static void vsock_update_buffer_size(struct vsock_sock *vsk, 1999 const struct vsock_transport *transport, 2000 u64 val) 2001 { 2002 if (val < vsk->buffer_min_size) 2003 val = vsk->buffer_min_size; 2004 2005 if (val > vsk->buffer_max_size) 2006 val = vsk->buffer_max_size; 2007 2008 if (val != vsk->buffer_size && 2009 transport && transport->notify_buffer_size) 2010 transport->notify_buffer_size(vsk, &val); 2011 2012 vsk->buffer_size = val; 2013 } 2014 2015 static int vsock_connectible_setsockopt(struct socket *sock, 2016 int level, 2017 int optname, 2018 sockptr_t optval, 2019 unsigned int optlen) 2020 { 2021 int err; 2022 struct sock *sk; 2023 struct vsock_sock *vsk; 2024 const struct vsock_transport *transport; 2025 u64 val; 2026 2027 if (level != AF_VSOCK && level != SOL_SOCKET) 2028 return -ENOPROTOOPT; 2029 2030 #define COPY_IN(_v) \ 2031 do { \ 2032 if (optlen < sizeof(_v)) { \ 2033 err = -EINVAL; \ 2034 goto exit; \ 2035 } \ 2036 if (copy_from_sockptr(&_v, optval, sizeof(_v)) != 0) { \ 2037 err = -EFAULT; \ 2038 goto exit; \ 2039 } \ 2040 } while (0) 2041 2042 err = 0; 2043 sk = sock->sk; 2044 vsk = vsock_sk(sk); 2045 2046 lock_sock(sk); 2047 2048 transport = vsk->transport; 2049 2050 if (level == SOL_SOCKET) { 2051 int zerocopy; 2052 2053 if (optname != SO_ZEROCOPY) { 2054 release_sock(sk); 2055 return sock_setsockopt(sock, level, optname, optval, optlen); 2056 } 2057 2058 /* Use 'int' type here, because variable to 2059 * set this option usually has this type. 2060 */ 2061 COPY_IN(zerocopy); 2062 2063 if (zerocopy < 0 || zerocopy > 1) { 2064 err = -EINVAL; 2065 goto exit; 2066 } 2067 2068 if (transport && !vsock_msgzerocopy_allow(transport)) { 2069 err = -EOPNOTSUPP; 2070 goto exit; 2071 } 2072 2073 sock_valbool_flag(sk, SOCK_ZEROCOPY, zerocopy); 2074 goto exit; 2075 } 2076 2077 switch (optname) { 2078 case SO_VM_SOCKETS_BUFFER_SIZE: 2079 COPY_IN(val); 2080 vsock_update_buffer_size(vsk, transport, val); 2081 break; 2082 2083 case SO_VM_SOCKETS_BUFFER_MAX_SIZE: 2084 COPY_IN(val); 2085 vsk->buffer_max_size = val; 2086 vsock_update_buffer_size(vsk, transport, vsk->buffer_size); 2087 break; 2088 2089 case SO_VM_SOCKETS_BUFFER_MIN_SIZE: 2090 COPY_IN(val); 2091 vsk->buffer_min_size = val; 2092 vsock_update_buffer_size(vsk, transport, vsk->buffer_size); 2093 break; 2094 2095 case SO_VM_SOCKETS_CONNECT_TIMEOUT_NEW: 2096 case SO_VM_SOCKETS_CONNECT_TIMEOUT_OLD: { 2097 struct __kernel_sock_timeval tv; 2098 2099 err = sock_copy_user_timeval(&tv, optval, optlen, 2100 optname == SO_VM_SOCKETS_CONNECT_TIMEOUT_OLD); 2101 if (err) 2102 break; 2103 if (tv.tv_sec >= 0 && tv.tv_usec < USEC_PER_SEC && 2104 tv.tv_sec < (MAX_SCHEDULE_TIMEOUT / HZ - 1)) { 2105 vsk->connect_timeout = tv.tv_sec * HZ + 2106 DIV_ROUND_UP((unsigned long)tv.tv_usec, (USEC_PER_SEC / HZ)); 2107 if (vsk->connect_timeout == 0) 2108 vsk->connect_timeout = 2109 VSOCK_DEFAULT_CONNECT_TIMEOUT; 2110 2111 } else { 2112 err = -ERANGE; 2113 } 2114 break; 2115 } 2116 2117 default: 2118 err = -ENOPROTOOPT; 2119 break; 2120 } 2121 2122 #undef COPY_IN 2123 2124 exit: 2125 release_sock(sk); 2126 return err; 2127 } 2128 2129 static int vsock_connectible_getsockopt(struct socket *sock, 2130 int level, int optname, 2131 sockopt_t *opt) 2132 { 2133 struct sock *sk = sock->sk; 2134 struct vsock_sock *vsk = vsock_sk(sk); 2135 2136 union { 2137 u64 val64; 2138 struct old_timeval32 tm32; 2139 struct __kernel_old_timeval tm; 2140 struct __kernel_sock_timeval stm; 2141 } v; 2142 2143 int lv = sizeof(v.val64); 2144 int len; 2145 2146 if (level != AF_VSOCK) 2147 return -ENOPROTOOPT; 2148 2149 len = opt->optlen; 2150 2151 memset(&v, 0, sizeof(v)); 2152 2153 switch (optname) { 2154 case SO_VM_SOCKETS_BUFFER_SIZE: 2155 v.val64 = vsk->buffer_size; 2156 break; 2157 2158 case SO_VM_SOCKETS_BUFFER_MAX_SIZE: 2159 v.val64 = vsk->buffer_max_size; 2160 break; 2161 2162 case SO_VM_SOCKETS_BUFFER_MIN_SIZE: 2163 v.val64 = vsk->buffer_min_size; 2164 break; 2165 2166 case SO_VM_SOCKETS_CONNECT_TIMEOUT_NEW: 2167 case SO_VM_SOCKETS_CONNECT_TIMEOUT_OLD: 2168 lv = sock_get_timeout(vsk->connect_timeout, &v, 2169 optname == SO_VM_SOCKETS_CONNECT_TIMEOUT_OLD); 2170 break; 2171 2172 default: 2173 return -ENOPROTOOPT; 2174 } 2175 2176 if (len < lv) 2177 return -EINVAL; 2178 if (len > lv) 2179 len = lv; 2180 if (copy_to_iter(&v, len, &opt->iter_out) != len) 2181 return -EFAULT; 2182 2183 opt->optlen = len; 2184 2185 return 0; 2186 } 2187 2188 static int vsock_connectible_sendmsg(struct socket *sock, struct msghdr *msg, 2189 size_t len) 2190 { 2191 struct sock *sk; 2192 struct vsock_sock *vsk; 2193 const struct vsock_transport *transport; 2194 ssize_t total_written; 2195 long timeout; 2196 int err; 2197 struct vsock_transport_send_notify_data send_data; 2198 DEFINE_WAIT_FUNC(wait, woken_wake_function); 2199 2200 sk = sock->sk; 2201 vsk = vsock_sk(sk); 2202 total_written = 0; 2203 err = 0; 2204 2205 if (msg->msg_flags & MSG_OOB) 2206 return -EOPNOTSUPP; 2207 2208 lock_sock(sk); 2209 2210 transport = vsk->transport; 2211 2212 /* Callers should not provide a destination with connection oriented 2213 * sockets. 2214 */ 2215 if (msg->msg_namelen) { 2216 err = sk->sk_state == TCP_ESTABLISHED ? -EISCONN : -EOPNOTSUPP; 2217 goto out; 2218 } 2219 2220 /* Send data only if both sides are not shutdown in the direction. */ 2221 if (sk->sk_shutdown & SEND_SHUTDOWN || 2222 vsk->peer_shutdown & RCV_SHUTDOWN) { 2223 err = -EPIPE; 2224 goto out; 2225 } 2226 2227 if (!transport || sk->sk_state != TCP_ESTABLISHED || 2228 !vsock_addr_bound(&vsk->local_addr)) { 2229 err = -ENOTCONN; 2230 goto out; 2231 } 2232 2233 if (!vsock_addr_bound(&vsk->remote_addr)) { 2234 err = -EDESTADDRREQ; 2235 goto out; 2236 } 2237 2238 if (msg->msg_flags & MSG_ZEROCOPY && 2239 !vsock_msgzerocopy_allow(transport)) { 2240 err = -EOPNOTSUPP; 2241 goto out; 2242 } 2243 2244 /* Wait for room in the produce queue to enqueue our user's data. */ 2245 timeout = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); 2246 2247 err = transport->notify_send_init(vsk, &send_data); 2248 if (err < 0) 2249 goto out; 2250 2251 while (total_written < len) { 2252 ssize_t written; 2253 2254 add_wait_queue(sk_sleep(sk), &wait); 2255 while (vsock_stream_has_space(vsk) == 0 && 2256 sk->sk_err == 0 && 2257 !(sk->sk_shutdown & SEND_SHUTDOWN) && 2258 !(vsk->peer_shutdown & RCV_SHUTDOWN)) { 2259 2260 /* Don't wait for non-blocking sockets. */ 2261 if (timeout == 0) { 2262 err = -EAGAIN; 2263 remove_wait_queue(sk_sleep(sk), &wait); 2264 goto out_err; 2265 } 2266 2267 err = transport->notify_send_pre_block(vsk, &send_data); 2268 if (err < 0) { 2269 remove_wait_queue(sk_sleep(sk), &wait); 2270 goto out_err; 2271 } 2272 2273 release_sock(sk); 2274 timeout = wait_woken(&wait, TASK_INTERRUPTIBLE, timeout); 2275 lock_sock(sk); 2276 if (signal_pending(current)) { 2277 err = sock_intr_errno(timeout); 2278 remove_wait_queue(sk_sleep(sk), &wait); 2279 goto out_err; 2280 } else if (timeout == 0) { 2281 err = -EAGAIN; 2282 remove_wait_queue(sk_sleep(sk), &wait); 2283 goto out_err; 2284 } 2285 } 2286 remove_wait_queue(sk_sleep(sk), &wait); 2287 2288 /* These checks occur both as part of and after the loop 2289 * conditional since we need to check before and after 2290 * sleeping. 2291 */ 2292 if (sk->sk_err) { 2293 err = -sk->sk_err; 2294 goto out_err; 2295 } else if ((sk->sk_shutdown & SEND_SHUTDOWN) || 2296 (vsk->peer_shutdown & RCV_SHUTDOWN)) { 2297 err = -EPIPE; 2298 goto out_err; 2299 } 2300 2301 err = transport->notify_send_pre_enqueue(vsk, &send_data); 2302 if (err < 0) 2303 goto out_err; 2304 2305 /* Note that enqueue will only write as many bytes as are free 2306 * in the produce queue, so we don't need to ensure len is 2307 * smaller than the queue size. It is the caller's 2308 * responsibility to check how many bytes we were able to send. 2309 */ 2310 2311 if (sk->sk_type == SOCK_SEQPACKET) { 2312 written = transport->seqpacket_enqueue(vsk, 2313 msg, len - total_written); 2314 } else { 2315 written = transport->stream_enqueue(vsk, 2316 msg, len - total_written); 2317 } 2318 2319 if (written < 0) { 2320 err = written; 2321 goto out_err; 2322 } 2323 2324 total_written += written; 2325 2326 err = transport->notify_send_post_enqueue( 2327 vsk, written, &send_data); 2328 if (err < 0) 2329 goto out_err; 2330 2331 } 2332 2333 out_err: 2334 if (total_written > 0) { 2335 /* Return number of written bytes only if: 2336 * 1) SOCK_STREAM socket. 2337 * 2) SOCK_SEQPACKET socket when whole buffer is sent. 2338 */ 2339 if (sk->sk_type == SOCK_STREAM || total_written == len) 2340 err = total_written; 2341 } 2342 out: 2343 if (sk->sk_type == SOCK_STREAM) 2344 err = sk_stream_error(sk, msg->msg_flags, err); 2345 2346 release_sock(sk); 2347 return err; 2348 } 2349 2350 static int vsock_connectible_wait_data(struct sock *sk, 2351 struct wait_queue_entry *wait, 2352 long timeout, 2353 struct vsock_transport_recv_notify_data *recv_data, 2354 size_t target) 2355 { 2356 const struct vsock_transport *transport; 2357 struct vsock_sock *vsk; 2358 s64 data; 2359 int err; 2360 2361 vsk = vsock_sk(sk); 2362 err = 0; 2363 transport = vsk->transport; 2364 2365 while (1) { 2366 prepare_to_wait(sk_sleep(sk), wait, TASK_INTERRUPTIBLE); 2367 data = vsock_connectible_has_data(vsk); 2368 if (data != 0) 2369 break; 2370 2371 if (sk->sk_err != 0 || 2372 (sk->sk_shutdown & RCV_SHUTDOWN) || 2373 (vsk->peer_shutdown & SEND_SHUTDOWN)) { 2374 break; 2375 } 2376 2377 /* Don't wait for non-blocking sockets. */ 2378 if (timeout == 0) { 2379 err = -EAGAIN; 2380 break; 2381 } 2382 2383 if (recv_data) { 2384 err = transport->notify_recv_pre_block(vsk, target, recv_data); 2385 if (err < 0) 2386 break; 2387 } 2388 2389 release_sock(sk); 2390 timeout = schedule_timeout(timeout); 2391 lock_sock(sk); 2392 2393 if (signal_pending(current)) { 2394 err = sock_intr_errno(timeout); 2395 break; 2396 } else if (timeout == 0) { 2397 err = -EAGAIN; 2398 break; 2399 } 2400 } 2401 2402 finish_wait(sk_sleep(sk), wait); 2403 2404 if (err) 2405 return err; 2406 2407 /* Internal transport error when checking for available 2408 * data. XXX This should be changed to a connection 2409 * reset in a later change. 2410 */ 2411 if (data < 0) 2412 return -ENOMEM; 2413 2414 return data; 2415 } 2416 2417 static int __vsock_stream_recvmsg(struct sock *sk, struct msghdr *msg, 2418 size_t len, int flags) 2419 { 2420 struct vsock_transport_recv_notify_data recv_data; 2421 const struct vsock_transport *transport; 2422 struct vsock_sock *vsk; 2423 ssize_t copied; 2424 size_t target; 2425 long timeout; 2426 int err; 2427 2428 DEFINE_WAIT(wait); 2429 2430 vsk = vsock_sk(sk); 2431 transport = vsk->transport; 2432 2433 /* We must not copy less than target bytes into the user's buffer 2434 * before returning successfully, so we wait for the consume queue to 2435 * have that much data to consume before dequeueing. Note that this 2436 * makes it impossible to handle cases where target is greater than the 2437 * queue size. 2438 */ 2439 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); 2440 if (target >= transport->stream_rcvhiwat(vsk)) { 2441 err = -ENOMEM; 2442 goto out; 2443 } 2444 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 2445 copied = 0; 2446 2447 err = transport->notify_recv_init(vsk, target, &recv_data); 2448 if (err < 0) 2449 goto out; 2450 2451 2452 while (1) { 2453 ssize_t read; 2454 2455 err = vsock_connectible_wait_data(sk, &wait, timeout, 2456 &recv_data, target); 2457 if (err <= 0) 2458 break; 2459 2460 err = transport->notify_recv_pre_dequeue(vsk, target, 2461 &recv_data); 2462 if (err < 0) 2463 break; 2464 2465 read = transport->stream_dequeue(vsk, msg, len - copied, flags); 2466 if (read < 0) { 2467 err = read; 2468 break; 2469 } 2470 2471 copied += read; 2472 2473 err = transport->notify_recv_post_dequeue(vsk, target, read, 2474 !(flags & MSG_PEEK), &recv_data); 2475 if (err < 0) 2476 goto out; 2477 2478 if (read >= target || flags & MSG_PEEK) 2479 break; 2480 2481 target -= read; 2482 } 2483 2484 if (sk->sk_err) 2485 err = -sk->sk_err; 2486 else if (sk->sk_shutdown & RCV_SHUTDOWN) 2487 err = 0; 2488 2489 if (copied > 0) 2490 err = copied; 2491 2492 out: 2493 return err; 2494 } 2495 2496 static int __vsock_seqpacket_recvmsg(struct sock *sk, struct msghdr *msg, 2497 size_t len, int flags) 2498 { 2499 const struct vsock_transport *transport; 2500 struct vsock_sock *vsk; 2501 ssize_t msg_len; 2502 long timeout; 2503 int err = 0; 2504 DEFINE_WAIT(wait); 2505 2506 vsk = vsock_sk(sk); 2507 transport = vsk->transport; 2508 2509 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 2510 2511 err = vsock_connectible_wait_data(sk, &wait, timeout, NULL, 0); 2512 if (err <= 0) 2513 goto out; 2514 2515 msg_len = transport->seqpacket_dequeue(vsk, msg, flags); 2516 2517 if (msg_len < 0) { 2518 err = msg_len; 2519 goto out; 2520 } 2521 2522 if (sk->sk_err) { 2523 err = -sk->sk_err; 2524 } else if (sk->sk_shutdown & RCV_SHUTDOWN) { 2525 err = 0; 2526 } else { 2527 /* User sets MSG_TRUNC, so return real length of 2528 * packet. 2529 */ 2530 if (flags & MSG_TRUNC) 2531 err = msg_len; 2532 else 2533 err = len - msg_data_left(msg); 2534 2535 /* Always set MSG_TRUNC if real length of packet is 2536 * bigger than user's buffer. 2537 */ 2538 if (msg_len > len) 2539 msg->msg_flags |= MSG_TRUNC; 2540 } 2541 2542 out: 2543 return err; 2544 } 2545 2546 int 2547 __vsock_connectible_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, 2548 int flags) 2549 { 2550 struct sock *sk; 2551 struct vsock_sock *vsk; 2552 const struct vsock_transport *transport; 2553 int err; 2554 2555 sk = sock->sk; 2556 2557 if (unlikely(flags & MSG_ERRQUEUE)) 2558 return sock_recv_errqueue(sk, msg, len, SOL_VSOCK, VSOCK_RECVERR); 2559 2560 vsk = vsock_sk(sk); 2561 err = 0; 2562 2563 lock_sock(sk); 2564 2565 transport = vsk->transport; 2566 2567 if (!transport || sk->sk_state != TCP_ESTABLISHED) { 2568 /* Recvmsg is supposed to return 0 if a peer performs an 2569 * orderly shutdown. Differentiate between that case and when a 2570 * peer has not connected or a local shutdown occurred with the 2571 * SOCK_DONE flag. 2572 */ 2573 if (sock_flag(sk, SOCK_DONE)) 2574 err = 0; 2575 else 2576 err = -ENOTCONN; 2577 2578 goto out; 2579 } 2580 2581 if (flags & MSG_OOB) { 2582 err = -EOPNOTSUPP; 2583 goto out; 2584 } 2585 2586 /* We don't check peer_shutdown flag here since peer may actually shut 2587 * down, but there can be data in the queue that a local socket can 2588 * receive. 2589 */ 2590 if (sk->sk_shutdown & RCV_SHUTDOWN) { 2591 err = 0; 2592 goto out; 2593 } 2594 2595 /* It is valid on Linux to pass in a zero-length receive buffer. This 2596 * is not an error. We may as well bail out now. 2597 */ 2598 if (!len) { 2599 err = 0; 2600 goto out; 2601 } 2602 2603 if (sk->sk_type == SOCK_STREAM) 2604 err = __vsock_stream_recvmsg(sk, msg, len, flags); 2605 else 2606 err = __vsock_seqpacket_recvmsg(sk, msg, len, flags); 2607 2608 out: 2609 release_sock(sk); 2610 return err; 2611 } 2612 2613 int 2614 vsock_connectible_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, 2615 int flags) 2616 { 2617 #ifdef CONFIG_BPF_SYSCALL 2618 struct sock *sk = sock->sk; 2619 const struct proto *prot; 2620 2621 prot = READ_ONCE(sk->sk_prot); 2622 if (prot != &vsock_proto) 2623 return prot->recvmsg(sk, msg, len, flags); 2624 #endif 2625 2626 return __vsock_connectible_recvmsg(sock, msg, len, flags); 2627 } 2628 EXPORT_SYMBOL_GPL(vsock_connectible_recvmsg); 2629 2630 static int vsock_set_rcvlowat(struct sock *sk, int val) 2631 { 2632 const struct vsock_transport *transport; 2633 struct vsock_sock *vsk; 2634 2635 vsk = vsock_sk(sk); 2636 2637 if (val > vsk->buffer_size) 2638 return -EINVAL; 2639 2640 transport = vsk->transport; 2641 2642 if (transport && transport->notify_set_rcvlowat) { 2643 int err; 2644 2645 err = transport->notify_set_rcvlowat(vsk, val); 2646 if (err) 2647 return err; 2648 } 2649 2650 WRITE_ONCE(sk->sk_rcvlowat, val ? : 1); 2651 return 0; 2652 } 2653 2654 static const struct proto_ops vsock_stream_ops = { 2655 .family = PF_VSOCK, 2656 .owner = THIS_MODULE, 2657 .release = vsock_release, 2658 .bind = vsock_bind, 2659 .connect = vsock_connect, 2660 .socketpair = sock_no_socketpair, 2661 .accept = vsock_accept, 2662 .getname = vsock_getname, 2663 .poll = vsock_poll, 2664 .ioctl = vsock_ioctl, 2665 .listen = vsock_listen, 2666 .shutdown = vsock_shutdown, 2667 .setsockopt = vsock_connectible_setsockopt, 2668 .getsockopt_iter = vsock_connectible_getsockopt, 2669 .sendmsg = vsock_connectible_sendmsg, 2670 .recvmsg = vsock_connectible_recvmsg, 2671 .mmap = sock_no_mmap, 2672 .set_rcvlowat = vsock_set_rcvlowat, 2673 .read_skb = vsock_read_skb, 2674 }; 2675 2676 static const struct proto_ops vsock_seqpacket_ops = { 2677 .family = PF_VSOCK, 2678 .owner = THIS_MODULE, 2679 .release = vsock_release, 2680 .bind = vsock_bind, 2681 .connect = vsock_connect, 2682 .socketpair = sock_no_socketpair, 2683 .accept = vsock_accept, 2684 .getname = vsock_getname, 2685 .poll = vsock_poll, 2686 .ioctl = vsock_ioctl, 2687 .listen = vsock_listen, 2688 .shutdown = vsock_shutdown, 2689 .setsockopt = vsock_connectible_setsockopt, 2690 .getsockopt_iter = vsock_connectible_getsockopt, 2691 .sendmsg = vsock_connectible_sendmsg, 2692 .recvmsg = vsock_connectible_recvmsg, 2693 .mmap = sock_no_mmap, 2694 .read_skb = vsock_read_skb, 2695 }; 2696 2697 static int vsock_create(struct net *net, struct socket *sock, 2698 int protocol, int kern) 2699 { 2700 struct vsock_sock *vsk; 2701 struct sock *sk; 2702 int ret; 2703 2704 if (!sock) 2705 return -EINVAL; 2706 2707 if (protocol && protocol != PF_VSOCK) 2708 return -EPROTONOSUPPORT; 2709 2710 switch (sock->type) { 2711 case SOCK_DGRAM: 2712 sock->ops = &vsock_dgram_ops; 2713 break; 2714 case SOCK_STREAM: 2715 sock->ops = &vsock_stream_ops; 2716 break; 2717 case SOCK_SEQPACKET: 2718 sock->ops = &vsock_seqpacket_ops; 2719 break; 2720 default: 2721 return -ESOCKTNOSUPPORT; 2722 } 2723 2724 sock->state = SS_UNCONNECTED; 2725 2726 sk = __vsock_create(net, sock, NULL, GFP_KERNEL, 0, kern); 2727 if (!sk) 2728 return -ENOMEM; 2729 2730 vsk = vsock_sk(sk); 2731 2732 if (sock->type == SOCK_DGRAM) { 2733 ret = vsock_assign_transport(vsk, NULL); 2734 if (ret < 0) { 2735 sock->sk = NULL; 2736 sock_put(sk); 2737 return ret; 2738 } 2739 } 2740 2741 /* SOCK_DGRAM doesn't have 'setsockopt' callback set in its 2742 * proto_ops, so there is no handler for custom logic. 2743 */ 2744 if (sock_type_connectible(sock->type)) 2745 set_bit(SOCK_CUSTOM_SOCKOPT, &sk->sk_socket->flags); 2746 2747 vsock_insert_unbound(vsk); 2748 2749 return 0; 2750 } 2751 2752 static const struct net_proto_family vsock_family_ops = { 2753 .family = AF_VSOCK, 2754 .create = vsock_create, 2755 .owner = THIS_MODULE, 2756 }; 2757 2758 static long vsock_dev_do_ioctl(struct file *filp, 2759 unsigned int cmd, void __user *ptr) 2760 { 2761 u32 __user *p = ptr; 2762 int retval = 0; 2763 u32 cid; 2764 2765 switch (cmd) { 2766 case IOCTL_VM_SOCKETS_GET_LOCAL_CID: 2767 /* To be compatible with the VMCI behavior, we prioritize the 2768 * guest CID instead of well-know host CID (VMADDR_CID_HOST). 2769 */ 2770 cid = vsock_registered_transport_cid(&transport_g2h); 2771 if (cid == VMADDR_CID_ANY) 2772 cid = vsock_registered_transport_cid(&transport_h2g); 2773 if (cid == VMADDR_CID_ANY) 2774 cid = vsock_registered_transport_cid(&transport_local); 2775 2776 if (put_user(cid, p) != 0) 2777 retval = -EFAULT; 2778 break; 2779 2780 default: 2781 retval = -ENOIOCTLCMD; 2782 } 2783 2784 return retval; 2785 } 2786 2787 static long vsock_dev_ioctl(struct file *filp, 2788 unsigned int cmd, unsigned long arg) 2789 { 2790 return vsock_dev_do_ioctl(filp, cmd, (void __user *)arg); 2791 } 2792 2793 #ifdef CONFIG_COMPAT 2794 static long vsock_dev_compat_ioctl(struct file *filp, 2795 unsigned int cmd, unsigned long arg) 2796 { 2797 return vsock_dev_do_ioctl(filp, cmd, compat_ptr(arg)); 2798 } 2799 #endif 2800 2801 static const struct file_operations vsock_device_ops = { 2802 .owner = THIS_MODULE, 2803 .unlocked_ioctl = vsock_dev_ioctl, 2804 #ifdef CONFIG_COMPAT 2805 .compat_ioctl = vsock_dev_compat_ioctl, 2806 #endif 2807 .open = nonseekable_open, 2808 }; 2809 2810 static struct miscdevice vsock_device = { 2811 .name = "vsock", 2812 .fops = &vsock_device_ops, 2813 }; 2814 2815 static int __vsock_net_mode_string(const struct ctl_table *table, int write, 2816 void *buffer, size_t *lenp, loff_t *ppos, 2817 enum vsock_net_mode mode, 2818 enum vsock_net_mode *new_mode) 2819 { 2820 char data[VSOCK_NET_MODE_STR_MAX] = {0}; 2821 struct ctl_table tmp; 2822 int ret; 2823 2824 if (!table->data || !table->maxlen || !*lenp) { 2825 *lenp = 0; 2826 return 0; 2827 } 2828 2829 tmp = *table; 2830 tmp.data = data; 2831 2832 if (!write) { 2833 const char *p; 2834 2835 switch (mode) { 2836 case VSOCK_NET_MODE_GLOBAL: 2837 p = VSOCK_NET_MODE_STR_GLOBAL; 2838 break; 2839 case VSOCK_NET_MODE_LOCAL: 2840 p = VSOCK_NET_MODE_STR_LOCAL; 2841 break; 2842 default: 2843 WARN_ONCE(true, "netns has invalid vsock mode"); 2844 *lenp = 0; 2845 return 0; 2846 } 2847 2848 strscpy(data, p, sizeof(data)); 2849 tmp.maxlen = strlen(p); 2850 } 2851 2852 ret = proc_dostring(&tmp, write, buffer, lenp, ppos); 2853 if (ret || !write) 2854 return ret; 2855 2856 if (*lenp >= sizeof(data)) 2857 return -EINVAL; 2858 2859 if (!strncmp(data, VSOCK_NET_MODE_STR_GLOBAL, sizeof(data))) 2860 *new_mode = VSOCK_NET_MODE_GLOBAL; 2861 else if (!strncmp(data, VSOCK_NET_MODE_STR_LOCAL, sizeof(data))) 2862 *new_mode = VSOCK_NET_MODE_LOCAL; 2863 else 2864 return -EINVAL; 2865 2866 return 0; 2867 } 2868 2869 static int vsock_net_mode_string(const struct ctl_table *table, int write, 2870 void *buffer, size_t *lenp, loff_t *ppos) 2871 { 2872 struct net *net; 2873 2874 if (write) 2875 return -EPERM; 2876 2877 net = container_of(table->data, struct net, vsock.mode); 2878 2879 return __vsock_net_mode_string(table, write, buffer, lenp, ppos, 2880 vsock_net_mode(net), NULL); 2881 } 2882 2883 static int vsock_net_child_mode_string(const struct ctl_table *table, int write, 2884 void *buffer, size_t *lenp, loff_t *ppos) 2885 { 2886 enum vsock_net_mode new_mode; 2887 struct net *net; 2888 int ret; 2889 2890 net = container_of(table->data, struct net, vsock.child_ns_mode); 2891 2892 ret = __vsock_net_mode_string(table, write, buffer, lenp, ppos, 2893 vsock_net_child_mode(net), &new_mode); 2894 if (ret) 2895 return ret; 2896 2897 if (write) { 2898 /* Prevent a "local" namespace from escalating to "global", 2899 * which would give nested namespaces access to global CIDs. 2900 */ 2901 if (vsock_net_mode(net) == VSOCK_NET_MODE_LOCAL && 2902 new_mode == VSOCK_NET_MODE_GLOBAL) 2903 return -EPERM; 2904 2905 if (!vsock_net_set_child_mode(net, new_mode)) 2906 return -EBUSY; 2907 } 2908 2909 return 0; 2910 } 2911 2912 static const struct ctl_table vsock_table[] = { 2913 { 2914 .procname = "ns_mode", 2915 .data = &init_net.vsock.mode, 2916 .maxlen = VSOCK_NET_MODE_STR_MAX, 2917 .mode = 0444, 2918 .proc_handler = vsock_net_mode_string 2919 }, 2920 { 2921 .procname = "child_ns_mode", 2922 .data = &init_net.vsock.child_ns_mode, 2923 .maxlen = VSOCK_NET_MODE_STR_MAX, 2924 .mode = 0644, 2925 .proc_handler = vsock_net_child_mode_string 2926 }, 2927 { 2928 .procname = "g2h_fallback", 2929 .data = &init_net.vsock.g2h_fallback, 2930 .maxlen = sizeof(int), 2931 .mode = 0644, 2932 .proc_handler = proc_dointvec_minmax, 2933 .extra1 = SYSCTL_ZERO, 2934 .extra2 = SYSCTL_ONE, 2935 }, 2936 }; 2937 2938 static const struct ctl_table *vsock_table_dup(struct net *net) 2939 { 2940 struct ctl_table *table; 2941 2942 table = kmemdup(vsock_table, sizeof(vsock_table), GFP_KERNEL); 2943 if (!table) 2944 return NULL; 2945 2946 table[0].data = &net->vsock.mode; 2947 table[1].data = &net->vsock.child_ns_mode; 2948 table[2].data = &net->vsock.g2h_fallback; 2949 2950 return table; 2951 } 2952 2953 static int __net_init vsock_sysctl_register(struct net *net) 2954 { 2955 const struct ctl_table *table; 2956 2957 if (net_eq(net, &init_net)) { 2958 table = vsock_table; 2959 } else { 2960 table = vsock_table_dup(net); 2961 if (!table) 2962 goto err_alloc; 2963 } 2964 2965 net->vsock.sysctl_hdr = register_net_sysctl_sz(net, "net/vsock", table, 2966 ARRAY_SIZE(vsock_table)); 2967 if (!net->vsock.sysctl_hdr) 2968 goto err_reg; 2969 2970 return 0; 2971 2972 err_reg: 2973 if (!net_eq(net, &init_net)) 2974 kfree(table); 2975 err_alloc: 2976 return -ENOMEM; 2977 } 2978 2979 static void vsock_sysctl_unregister(struct net *net) 2980 { 2981 const struct ctl_table *table; 2982 2983 table = net->vsock.sysctl_hdr->ctl_table_arg; 2984 unregister_net_sysctl_table(net->vsock.sysctl_hdr); 2985 if (!net_eq(net, &init_net)) 2986 kfree(table); 2987 } 2988 2989 static void vsock_net_init(struct net *net) 2990 { 2991 if (net_eq(net, &init_net)) 2992 net->vsock.mode = VSOCK_NET_MODE_GLOBAL; 2993 else 2994 net->vsock.mode = vsock_net_child_mode(current->nsproxy->net_ns); 2995 2996 net->vsock.child_ns_mode = net->vsock.mode; 2997 net->vsock.child_ns_mode_locked = 0; 2998 net->vsock.g2h_fallback = 1; 2999 } 3000 3001 static __net_init int vsock_sysctl_init_net(struct net *net) 3002 { 3003 vsock_net_init(net); 3004 3005 if (vsock_sysctl_register(net)) 3006 return -ENOMEM; 3007 3008 return 0; 3009 } 3010 3011 static __net_exit void vsock_sysctl_exit_net(struct net *net) 3012 { 3013 vsock_sysctl_unregister(net); 3014 } 3015 3016 static struct pernet_operations vsock_sysctl_ops = { 3017 .init = vsock_sysctl_init_net, 3018 .exit = vsock_sysctl_exit_net, 3019 }; 3020 3021 static int __init vsock_init(void) 3022 { 3023 int err = 0; 3024 3025 vsock_init_tables(); 3026 3027 vsock_proto.owner = THIS_MODULE; 3028 vsock_device.minor = MISC_DYNAMIC_MINOR; 3029 err = misc_register(&vsock_device); 3030 if (err) { 3031 pr_err("Failed to register misc device\n"); 3032 goto err_reset_transport; 3033 } 3034 3035 err = proto_register(&vsock_proto, 1); /* we want our slab */ 3036 if (err) { 3037 pr_err("Cannot register vsock protocol\n"); 3038 goto err_deregister_misc; 3039 } 3040 3041 err = sock_register(&vsock_family_ops); 3042 if (err) { 3043 pr_err("could not register af_vsock (%d) address family: %d\n", 3044 AF_VSOCK, err); 3045 goto err_unregister_proto; 3046 } 3047 3048 if (register_pernet_subsys(&vsock_sysctl_ops)) { 3049 err = -ENOMEM; 3050 goto err_unregister_sock; 3051 } 3052 3053 vsock_bpf_build_proto(); 3054 3055 return 0; 3056 3057 err_unregister_sock: 3058 sock_unregister(AF_VSOCK); 3059 err_unregister_proto: 3060 proto_unregister(&vsock_proto); 3061 err_deregister_misc: 3062 misc_deregister(&vsock_device); 3063 err_reset_transport: 3064 return err; 3065 } 3066 3067 static void __exit vsock_exit(void) 3068 { 3069 misc_deregister(&vsock_device); 3070 sock_unregister(AF_VSOCK); 3071 proto_unregister(&vsock_proto); 3072 unregister_pernet_subsys(&vsock_sysctl_ops); 3073 } 3074 3075 const struct vsock_transport *vsock_core_get_transport(struct vsock_sock *vsk) 3076 { 3077 return vsk->transport; 3078 } 3079 EXPORT_SYMBOL_GPL(vsock_core_get_transport); 3080 3081 int vsock_core_register(const struct vsock_transport *t, int features) 3082 { 3083 const struct vsock_transport *t_h2g, *t_g2h, *t_dgram, *t_local; 3084 int err = mutex_lock_interruptible(&vsock_register_mutex); 3085 3086 if (err) 3087 return err; 3088 3089 t_h2g = transport_h2g; 3090 t_g2h = transport_g2h; 3091 t_dgram = transport_dgram; 3092 t_local = transport_local; 3093 3094 if (features & VSOCK_TRANSPORT_F_H2G) { 3095 if (t_h2g) { 3096 err = -EBUSY; 3097 goto err_busy; 3098 } 3099 t_h2g = t; 3100 } 3101 3102 if (features & VSOCK_TRANSPORT_F_G2H) { 3103 if (t_g2h) { 3104 err = -EBUSY; 3105 goto err_busy; 3106 } 3107 t_g2h = t; 3108 } 3109 3110 if (features & VSOCK_TRANSPORT_F_DGRAM) { 3111 if (t_dgram) { 3112 err = -EBUSY; 3113 goto err_busy; 3114 } 3115 t_dgram = t; 3116 } 3117 3118 if (features & VSOCK_TRANSPORT_F_LOCAL) { 3119 if (t_local) { 3120 err = -EBUSY; 3121 goto err_busy; 3122 } 3123 t_local = t; 3124 } 3125 3126 transport_h2g = t_h2g; 3127 transport_g2h = t_g2h; 3128 transport_dgram = t_dgram; 3129 transport_local = t_local; 3130 3131 err_busy: 3132 mutex_unlock(&vsock_register_mutex); 3133 return err; 3134 } 3135 EXPORT_SYMBOL_GPL(vsock_core_register); 3136 3137 void vsock_core_unregister(const struct vsock_transport *t) 3138 { 3139 mutex_lock(&vsock_register_mutex); 3140 3141 if (transport_h2g == t) 3142 transport_h2g = NULL; 3143 3144 if (transport_g2h == t) 3145 transport_g2h = NULL; 3146 3147 if (transport_dgram == t) 3148 transport_dgram = NULL; 3149 3150 if (transport_local == t) 3151 transport_local = NULL; 3152 3153 mutex_unlock(&vsock_register_mutex); 3154 } 3155 EXPORT_SYMBOL_GPL(vsock_core_unregister); 3156 3157 module_init(vsock_init); 3158 module_exit(vsock_exit); 3159 3160 MODULE_AUTHOR("VMware, Inc."); 3161 MODULE_DESCRIPTION("VMware Virtual Socket Family"); 3162 MODULE_VERSION("1.0.2.0-k"); 3163 MODULE_LICENSE("GPL v2"); 3164