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 /* Socket buffers may remain in the queue in VMCI datagram mode */ 1052 __skb_queue_purge(&sk->sk_receive_queue); 1053 /* Flush MSG_ZEROCOPY leftovers. */ 1054 __skb_queue_purge(&sk->sk_error_queue); 1055 1056 vsock_deassign_transport(vsk); 1057 1058 /* When clearing these addresses, there's no need to set the family and 1059 * possibly register the address family with the kernel. 1060 */ 1061 vsock_addr_init(&vsk->local_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY); 1062 vsock_addr_init(&vsk->remote_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY); 1063 1064 put_cred(vsk->owner); 1065 } 1066 1067 static int vsock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 1068 { 1069 int err; 1070 1071 err = sock_queue_rcv_skb(sk, skb); 1072 if (err) 1073 kfree_skb(skb); 1074 1075 return err; 1076 } 1077 1078 struct sock *vsock_create_connected(struct sock *parent) 1079 { 1080 return __vsock_create(sock_net(parent), NULL, parent, GFP_KERNEL, 1081 parent->sk_type, 0); 1082 } 1083 EXPORT_SYMBOL_GPL(vsock_create_connected); 1084 1085 s64 vsock_stream_has_data(struct vsock_sock *vsk) 1086 { 1087 if (WARN_ON(!vsk->transport)) 1088 return 0; 1089 1090 return vsk->transport->stream_has_data(vsk); 1091 } 1092 EXPORT_SYMBOL_GPL(vsock_stream_has_data); 1093 1094 s64 vsock_connectible_has_data(struct vsock_sock *vsk) 1095 { 1096 struct sock *sk = sk_vsock(vsk); 1097 1098 if (WARN_ON(!vsk->transport)) 1099 return 0; 1100 1101 if (sk->sk_type == SOCK_SEQPACKET) 1102 return vsk->transport->seqpacket_has_data(vsk); 1103 else 1104 return vsock_stream_has_data(vsk); 1105 } 1106 EXPORT_SYMBOL_GPL(vsock_connectible_has_data); 1107 1108 s64 vsock_stream_has_space(struct vsock_sock *vsk) 1109 { 1110 if (WARN_ON(!vsk->transport)) 1111 return 0; 1112 1113 return vsk->transport->stream_has_space(vsk); 1114 } 1115 EXPORT_SYMBOL_GPL(vsock_stream_has_space); 1116 1117 void vsock_data_ready(struct sock *sk) 1118 { 1119 struct vsock_sock *vsk = vsock_sk(sk); 1120 1121 if (vsock_stream_has_data(vsk) >= sk->sk_rcvlowat || 1122 sock_flag(sk, SOCK_DONE)) 1123 sk->sk_data_ready(sk); 1124 } 1125 EXPORT_SYMBOL_GPL(vsock_data_ready); 1126 1127 /* Dummy callback required by sockmap. 1128 * See unconditional call of saved_close() in sock_map_close(). 1129 */ 1130 static void vsock_close(struct sock *sk, long timeout) 1131 { 1132 } 1133 1134 static int vsock_release(struct socket *sock) 1135 { 1136 struct sock *sk = sock->sk; 1137 1138 if (!sk) 1139 return 0; 1140 1141 sk->sk_prot->close(sk, 0); 1142 __vsock_release(sk, 0); 1143 sock->sk = NULL; 1144 sock->state = SS_FREE; 1145 1146 return 0; 1147 } 1148 1149 static int 1150 vsock_bind(struct socket *sock, struct sockaddr_unsized *addr, int addr_len) 1151 { 1152 int err; 1153 struct sock *sk; 1154 struct sockaddr_vm *vm_addr; 1155 1156 sk = sock->sk; 1157 1158 if (vsock_addr_cast(addr, addr_len, &vm_addr) != 0) 1159 return -EINVAL; 1160 1161 lock_sock(sk); 1162 err = __vsock_bind(sk, vm_addr); 1163 release_sock(sk); 1164 1165 return err; 1166 } 1167 1168 static int vsock_getname(struct socket *sock, 1169 struct sockaddr *addr, int peer) 1170 { 1171 int err; 1172 struct sock *sk; 1173 struct vsock_sock *vsk; 1174 struct sockaddr_vm *vm_addr; 1175 1176 sk = sock->sk; 1177 vsk = vsock_sk(sk); 1178 err = 0; 1179 1180 lock_sock(sk); 1181 1182 if (peer) { 1183 if (sock->state != SS_CONNECTED) { 1184 err = -ENOTCONN; 1185 goto out; 1186 } 1187 vm_addr = &vsk->remote_addr; 1188 } else { 1189 vm_addr = &vsk->local_addr; 1190 } 1191 1192 BUILD_BUG_ON(sizeof(*vm_addr) > sizeof(struct sockaddr_storage)); 1193 memcpy(addr, vm_addr, sizeof(*vm_addr)); 1194 err = sizeof(*vm_addr); 1195 1196 out: 1197 release_sock(sk); 1198 return err; 1199 } 1200 1201 void vsock_linger(struct sock *sk) 1202 { 1203 DEFINE_WAIT_FUNC(wait, woken_wake_function); 1204 ssize_t (*unsent)(struct vsock_sock *vsk); 1205 struct vsock_sock *vsk = vsock_sk(sk); 1206 long timeout; 1207 1208 if (!sock_flag(sk, SOCK_LINGER)) 1209 return; 1210 1211 timeout = sk->sk_lingertime; 1212 if (!timeout) 1213 return; 1214 1215 /* Transports must implement `unsent_bytes` if they want to support 1216 * SOCK_LINGER through `vsock_linger()` since we use it to check when 1217 * the socket can be closed. 1218 */ 1219 unsent = vsk->transport->unsent_bytes; 1220 if (!unsent) 1221 return; 1222 1223 add_wait_queue(sk_sleep(sk), &wait); 1224 1225 do { 1226 if (sk_wait_event(sk, &timeout, unsent(vsk) == 0, &wait)) 1227 break; 1228 } while (!signal_pending(current) && timeout); 1229 1230 remove_wait_queue(sk_sleep(sk), &wait); 1231 } 1232 EXPORT_SYMBOL_GPL(vsock_linger); 1233 1234 static int vsock_shutdown(struct socket *sock, int mode) 1235 { 1236 int err; 1237 struct sock *sk; 1238 1239 /* User level uses SHUT_RD (0) and SHUT_WR (1), but the kernel uses 1240 * RCV_SHUTDOWN (1) and SEND_SHUTDOWN (2), so we must increment mode 1241 * here like the other address families do. Note also that the 1242 * increment makes SHUT_RDWR (2) into RCV_SHUTDOWN | SEND_SHUTDOWN (3), 1243 * which is what we want. 1244 */ 1245 mode++; 1246 1247 if ((mode & ~SHUTDOWN_MASK) || !mode) 1248 return -EINVAL; 1249 1250 /* If this is a connection oriented socket and it is not connected then 1251 * bail out immediately. If it is a DGRAM socket then we must first 1252 * kick the socket so that it wakes up from any sleeping calls, for 1253 * example recv(), and then afterwards return the error. 1254 */ 1255 1256 sk = sock->sk; 1257 1258 lock_sock(sk); 1259 if (sock->state == SS_UNCONNECTED) { 1260 err = -ENOTCONN; 1261 if (sock_type_connectible(sk->sk_type)) 1262 goto out; 1263 } else { 1264 sock->state = SS_DISCONNECTING; 1265 err = 0; 1266 } 1267 1268 /* Receive and send shutdowns are treated alike. */ 1269 mode = mode & (RCV_SHUTDOWN | SEND_SHUTDOWN); 1270 if (mode) { 1271 sk->sk_shutdown |= mode; 1272 sk->sk_state_change(sk); 1273 1274 if (sock_type_connectible(sk->sk_type)) { 1275 sock_reset_flag(sk, SOCK_DONE); 1276 vsock_send_shutdown(sk, mode); 1277 } 1278 } 1279 1280 out: 1281 release_sock(sk); 1282 return err; 1283 } 1284 1285 static __poll_t vsock_poll_shutdown(struct sock *sk, u32 peer_shutdown) 1286 { 1287 __poll_t mask = 0; 1288 1289 /* INET sockets treat local write shutdown and peer write shutdown as a 1290 * case of EPOLLHUP set. 1291 */ 1292 if (sk->sk_shutdown == SHUTDOWN_MASK || 1293 ((sk->sk_shutdown & SEND_SHUTDOWN) && 1294 (peer_shutdown & SEND_SHUTDOWN))) 1295 mask |= EPOLLHUP; 1296 1297 if (sk->sk_shutdown & RCV_SHUTDOWN || 1298 peer_shutdown & SEND_SHUTDOWN) 1299 mask |= EPOLLRDHUP; 1300 1301 return mask; 1302 } 1303 1304 static __poll_t vsock_poll(struct file *file, struct socket *sock, 1305 poll_table *wait) 1306 { 1307 struct sock *sk; 1308 __poll_t mask; 1309 struct vsock_sock *vsk; 1310 1311 sk = sock->sk; 1312 vsk = vsock_sk(sk); 1313 1314 poll_wait(file, sk_sleep(sk), wait); 1315 mask = 0; 1316 1317 if (sk->sk_err || !skb_queue_empty_lockless(&sk->sk_error_queue)) 1318 /* Signify that there has been an error on this socket. */ 1319 mask |= EPOLLERR; 1320 1321 if (sk_is_readable(sk)) 1322 mask |= EPOLLIN | EPOLLRDNORM; 1323 1324 if (sock->type == SOCK_DGRAM) { 1325 u32 peer_shutdown = READ_ONCE(vsk->peer_shutdown); 1326 1327 /* DGRAM sockets do not take lock_sock() in poll(), so use one 1328 * lockless snapshot for all shutdown-derived mask bits. 1329 */ 1330 mask |= vsock_poll_shutdown(sk, peer_shutdown); 1331 1332 /* For datagram sockets we can read if there is something in 1333 * the queue and write as long as the socket isn't shutdown for 1334 * sending. 1335 */ 1336 if (!skb_queue_empty_lockless(&sk->sk_receive_queue) || 1337 (sk->sk_shutdown & RCV_SHUTDOWN)) { 1338 mask |= EPOLLIN | EPOLLRDNORM; 1339 } 1340 1341 if (!(sk->sk_shutdown & SEND_SHUTDOWN)) 1342 mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND; 1343 1344 } else if (sock_type_connectible(sk->sk_type)) { 1345 const struct vsock_transport *transport; 1346 u32 peer_shutdown; 1347 1348 lock_sock(sk); 1349 1350 transport = vsk->transport; 1351 1352 /* Listening sockets that have connections in their accept 1353 * queue can be read. 1354 */ 1355 if (sk->sk_state == TCP_LISTEN 1356 && !vsock_is_accept_queue_empty(sk)) 1357 mask |= EPOLLIN | EPOLLRDNORM; 1358 1359 /* If there is something in the queue then we can read. */ 1360 if (transport && transport->stream_is_active(vsk) && 1361 !(sk->sk_shutdown & RCV_SHUTDOWN)) { 1362 bool data_ready_now = false; 1363 int target = sock_rcvlowat(sk, 0, INT_MAX); 1364 int ret = transport->notify_poll_in( 1365 vsk, target, &data_ready_now); 1366 if (ret < 0) { 1367 mask |= EPOLLERR; 1368 } else { 1369 if (data_ready_now) 1370 mask |= EPOLLIN | EPOLLRDNORM; 1371 1372 } 1373 } 1374 1375 /* Sockets whose connections have been closed, reset, or 1376 * terminated should also be considered read, and we check the 1377 * shutdown flag for that. 1378 */ 1379 peer_shutdown = READ_ONCE(vsk->peer_shutdown); 1380 mask |= vsock_poll_shutdown(sk, peer_shutdown); 1381 if (sk->sk_shutdown & RCV_SHUTDOWN || 1382 peer_shutdown & SEND_SHUTDOWN) { 1383 mask |= EPOLLIN | EPOLLRDNORM; 1384 } 1385 1386 /* Connected sockets that can produce data can be written. */ 1387 if (transport && sk->sk_state == TCP_ESTABLISHED) { 1388 if (!(sk->sk_shutdown & SEND_SHUTDOWN)) { 1389 bool space_avail_now = false; 1390 int ret = transport->notify_poll_out( 1391 vsk, 1, &space_avail_now); 1392 if (ret < 0) { 1393 mask |= EPOLLERR; 1394 } else { 1395 if (space_avail_now) 1396 /* Remove EPOLLWRBAND since INET 1397 * sockets are not setting it. 1398 */ 1399 mask |= EPOLLOUT | EPOLLWRNORM; 1400 1401 } 1402 } 1403 } 1404 1405 /* Simulate INET socket poll behaviors, which sets 1406 * EPOLLOUT|EPOLLWRNORM when peer is closed and nothing to read, 1407 * but local send is not shutdown. 1408 */ 1409 if (sk->sk_state == TCP_CLOSE || sk->sk_state == TCP_CLOSING) { 1410 if (!(sk->sk_shutdown & SEND_SHUTDOWN)) 1411 mask |= EPOLLOUT | EPOLLWRNORM; 1412 1413 } 1414 1415 release_sock(sk); 1416 } 1417 1418 return mask; 1419 } 1420 1421 static int vsock_read_skb(struct sock *sk, skb_read_actor_t read_actor) 1422 { 1423 struct vsock_sock *vsk = vsock_sk(sk); 1424 1425 if (WARN_ON_ONCE(!vsk->transport)) 1426 return -ENODEV; 1427 1428 return vsk->transport->read_skb(vsk, read_actor); 1429 } 1430 1431 static int vsock_dgram_sendmsg(struct socket *sock, struct msghdr *msg, 1432 size_t len) 1433 { 1434 int err; 1435 struct sock *sk; 1436 struct vsock_sock *vsk; 1437 struct sockaddr_vm *remote_addr; 1438 const struct vsock_transport *transport; 1439 1440 if (msg->msg_flags & MSG_OOB) 1441 return -EOPNOTSUPP; 1442 1443 /* For now, MSG_DONTWAIT is always assumed... */ 1444 err = 0; 1445 sk = sock->sk; 1446 vsk = vsock_sk(sk); 1447 1448 lock_sock(sk); 1449 1450 transport = vsk->transport; 1451 1452 err = vsock_auto_bind(vsk); 1453 if (err) 1454 goto out; 1455 1456 1457 /* If the provided message contains an address, use that. Otherwise 1458 * fall back on the socket's remote handle (if it has been connected). 1459 */ 1460 if (msg->msg_name && 1461 vsock_addr_cast(msg->msg_name, msg->msg_namelen, 1462 &remote_addr) == 0) { 1463 /* Ensure this address is of the right type and is a valid 1464 * destination. 1465 */ 1466 1467 if (remote_addr->svm_cid == VMADDR_CID_ANY) 1468 remote_addr->svm_cid = transport->get_local_cid(); 1469 1470 if (!vsock_addr_bound(remote_addr)) { 1471 err = -EINVAL; 1472 goto out; 1473 } 1474 } else if (sock->state == SS_CONNECTED) { 1475 remote_addr = &vsk->remote_addr; 1476 1477 if (remote_addr->svm_cid == VMADDR_CID_ANY) 1478 remote_addr->svm_cid = transport->get_local_cid(); 1479 1480 /* XXX Should connect() or this function ensure remote_addr is 1481 * bound? 1482 */ 1483 if (!vsock_addr_bound(&vsk->remote_addr)) { 1484 err = -EINVAL; 1485 goto out; 1486 } 1487 } else { 1488 err = -EINVAL; 1489 goto out; 1490 } 1491 1492 if (!transport->dgram_allow(vsk, remote_addr->svm_cid, 1493 remote_addr->svm_port)) { 1494 err = -EINVAL; 1495 goto out; 1496 } 1497 1498 err = transport->dgram_enqueue(vsk, remote_addr, msg, len); 1499 1500 out: 1501 release_sock(sk); 1502 return err; 1503 } 1504 1505 static int vsock_dgram_connect(struct socket *sock, 1506 struct sockaddr_unsized *addr, int addr_len, int flags) 1507 { 1508 int err; 1509 struct sock *sk; 1510 struct vsock_sock *vsk; 1511 struct sockaddr_vm *remote_addr; 1512 1513 sk = sock->sk; 1514 vsk = vsock_sk(sk); 1515 1516 err = vsock_addr_cast(addr, addr_len, &remote_addr); 1517 if (err == -EAFNOSUPPORT && remote_addr->svm_family == AF_UNSPEC) { 1518 lock_sock(sk); 1519 vsock_addr_init(&vsk->remote_addr, VMADDR_CID_ANY, 1520 VMADDR_PORT_ANY); 1521 sock->state = SS_UNCONNECTED; 1522 release_sock(sk); 1523 return 0; 1524 } else if (err != 0) 1525 return -EINVAL; 1526 1527 lock_sock(sk); 1528 1529 err = vsock_auto_bind(vsk); 1530 if (err) 1531 goto out; 1532 1533 if (!vsk->transport->dgram_allow(vsk, remote_addr->svm_cid, 1534 remote_addr->svm_port)) { 1535 err = -EINVAL; 1536 goto out; 1537 } 1538 1539 memcpy(&vsk->remote_addr, remote_addr, sizeof(vsk->remote_addr)); 1540 sock->state = SS_CONNECTED; 1541 1542 /* sock map disallows redirection of non-TCP sockets with sk_state != 1543 * TCP_ESTABLISHED (see sock_map_redirect_allowed()), so we set 1544 * TCP_ESTABLISHED here to allow redirection of connected vsock dgrams. 1545 * 1546 * This doesn't seem to be abnormal state for datagram sockets, as the 1547 * same approach can be see in other datagram socket types as well 1548 * (such as unix sockets). 1549 */ 1550 sk->sk_state = TCP_ESTABLISHED; 1551 1552 out: 1553 release_sock(sk); 1554 return err; 1555 } 1556 1557 int __vsock_dgram_recvmsg(struct socket *sock, struct msghdr *msg, 1558 size_t len, int flags) 1559 { 1560 struct sock *sk = sock->sk; 1561 struct vsock_sock *vsk = vsock_sk(sk); 1562 1563 return vsk->transport->dgram_dequeue(vsk, msg, len, flags); 1564 } 1565 1566 int vsock_dgram_recvmsg(struct socket *sock, struct msghdr *msg, 1567 size_t len, int flags) 1568 { 1569 #ifdef CONFIG_BPF_SYSCALL 1570 struct sock *sk = sock->sk; 1571 const struct proto *prot; 1572 1573 prot = READ_ONCE(sk->sk_prot); 1574 if (prot != &vsock_proto) 1575 return prot->recvmsg(sk, msg, len, flags); 1576 #endif 1577 1578 return __vsock_dgram_recvmsg(sock, msg, len, flags); 1579 } 1580 EXPORT_SYMBOL_GPL(vsock_dgram_recvmsg); 1581 1582 static int vsock_do_ioctl(struct socket *sock, unsigned int cmd, 1583 int __user *arg) 1584 { 1585 struct sock *sk = sock->sk; 1586 struct vsock_sock *vsk; 1587 int ret; 1588 1589 vsk = vsock_sk(sk); 1590 1591 switch (cmd) { 1592 case SIOCINQ: { 1593 ssize_t n_bytes; 1594 1595 if (!vsk->transport) { 1596 ret = -EOPNOTSUPP; 1597 break; 1598 } 1599 1600 if (sock_type_connectible(sk->sk_type) && 1601 sk->sk_state == TCP_LISTEN) { 1602 ret = -EINVAL; 1603 break; 1604 } 1605 1606 n_bytes = vsock_stream_has_data(vsk); 1607 if (n_bytes < 0) { 1608 ret = n_bytes; 1609 break; 1610 } 1611 ret = put_user(n_bytes, arg); 1612 break; 1613 } 1614 case SIOCOUTQ: { 1615 ssize_t n_bytes; 1616 1617 if (!vsk->transport || !vsk->transport->unsent_bytes) { 1618 ret = -EOPNOTSUPP; 1619 break; 1620 } 1621 1622 if (sock_type_connectible(sk->sk_type) && sk->sk_state == TCP_LISTEN) { 1623 ret = -EINVAL; 1624 break; 1625 } 1626 1627 n_bytes = vsk->transport->unsent_bytes(vsk); 1628 if (n_bytes < 0) { 1629 ret = n_bytes; 1630 break; 1631 } 1632 1633 ret = put_user(n_bytes, arg); 1634 break; 1635 } 1636 default: 1637 ret = -ENOIOCTLCMD; 1638 } 1639 1640 return ret; 1641 } 1642 1643 static int vsock_ioctl(struct socket *sock, unsigned int cmd, 1644 unsigned long arg) 1645 { 1646 int ret; 1647 1648 lock_sock(sock->sk); 1649 ret = vsock_do_ioctl(sock, cmd, (int __user *)arg); 1650 release_sock(sock->sk); 1651 1652 return ret; 1653 } 1654 1655 static const struct proto_ops vsock_dgram_ops = { 1656 .family = PF_VSOCK, 1657 .owner = THIS_MODULE, 1658 .release = vsock_release, 1659 .bind = vsock_bind, 1660 .connect = vsock_dgram_connect, 1661 .socketpair = sock_no_socketpair, 1662 .accept = sock_no_accept, 1663 .getname = vsock_getname, 1664 .poll = vsock_poll, 1665 .ioctl = vsock_ioctl, 1666 .listen = sock_no_listen, 1667 .shutdown = vsock_shutdown, 1668 .sendmsg = vsock_dgram_sendmsg, 1669 .recvmsg = vsock_dgram_recvmsg, 1670 .mmap = sock_no_mmap, 1671 .read_skb = vsock_read_skb, 1672 }; 1673 1674 static int vsock_transport_cancel_pkt(struct vsock_sock *vsk) 1675 { 1676 const struct vsock_transport *transport = vsk->transport; 1677 1678 if (!transport || !transport->cancel_pkt) 1679 return -EOPNOTSUPP; 1680 1681 return transport->cancel_pkt(vsk); 1682 } 1683 1684 static void vsock_connect_timeout(struct work_struct *work) 1685 { 1686 struct sock *sk; 1687 struct vsock_sock *vsk; 1688 1689 vsk = container_of(work, struct vsock_sock, connect_work.work); 1690 sk = sk_vsock(vsk); 1691 1692 lock_sock(sk); 1693 if (sk->sk_state == TCP_SYN_SENT && 1694 (sk->sk_shutdown != SHUTDOWN_MASK)) { 1695 sk->sk_state = TCP_CLOSE; 1696 sk->sk_socket->state = SS_UNCONNECTED; 1697 sk->sk_err = ETIMEDOUT; 1698 sk_error_report(sk); 1699 vsock_transport_cancel_pkt(vsk); 1700 } 1701 release_sock(sk); 1702 1703 sock_put(sk); 1704 } 1705 1706 static int vsock_connect(struct socket *sock, struct sockaddr_unsized *addr, 1707 int addr_len, int flags) 1708 { 1709 int err; 1710 struct sock *sk; 1711 struct vsock_sock *vsk; 1712 const struct vsock_transport *transport; 1713 struct sockaddr_vm *remote_addr; 1714 long timeout; 1715 DEFINE_WAIT(wait); 1716 1717 err = 0; 1718 sk = sock->sk; 1719 vsk = vsock_sk(sk); 1720 1721 lock_sock(sk); 1722 1723 /* XXX AF_UNSPEC should make us disconnect like AF_INET. */ 1724 switch (sock->state) { 1725 case SS_CONNECTED: 1726 err = -EISCONN; 1727 goto out; 1728 case SS_DISCONNECTING: 1729 err = -EINVAL; 1730 goto out; 1731 case SS_CONNECTING: 1732 /* This continues on so we can move sock into the SS_CONNECTED 1733 * state once the connection has completed (at which point err 1734 * will be set to zero also). Otherwise, we will either wait 1735 * for the connection or return -EALREADY should this be a 1736 * non-blocking call. 1737 */ 1738 err = -EALREADY; 1739 if (flags & O_NONBLOCK) 1740 goto out; 1741 break; 1742 default: 1743 if ((sk->sk_state == TCP_LISTEN) || 1744 vsock_addr_cast(addr, addr_len, &remote_addr) != 0) { 1745 err = -EINVAL; 1746 goto out; 1747 } 1748 1749 /* Set the remote address that we are connecting to. */ 1750 memcpy(&vsk->remote_addr, remote_addr, 1751 sizeof(vsk->remote_addr)); 1752 1753 err = vsock_assign_transport(vsk, NULL); 1754 if (err) 1755 goto out; 1756 1757 transport = vsk->transport; 1758 1759 /* The hypervisor and well-known contexts do not have socket 1760 * endpoints. 1761 */ 1762 if (!transport || 1763 !transport->stream_allow(vsk, remote_addr->svm_cid, 1764 remote_addr->svm_port)) { 1765 err = -ENETUNREACH; 1766 goto out; 1767 } 1768 1769 if (vsock_msgzerocopy_allow(transport)) { 1770 set_bit(SOCK_SUPPORT_ZC, &sk->sk_socket->flags); 1771 } else if (sock_flag(sk, SOCK_ZEROCOPY)) { 1772 /* If this option was set before 'connect()', 1773 * when transport was unknown, check that this 1774 * feature is supported here. 1775 */ 1776 err = -EOPNOTSUPP; 1777 goto out; 1778 } 1779 1780 err = vsock_auto_bind(vsk); 1781 if (err) 1782 goto out; 1783 1784 sk->sk_state = TCP_SYN_SENT; 1785 1786 err = transport->connect(vsk); 1787 if (err < 0) 1788 goto out; 1789 1790 /* sk_err might have been set as a result of an earlier 1791 * (failed) connect attempt. 1792 */ 1793 sk->sk_err = 0; 1794 1795 /* Mark sock as connecting and set the error code to in 1796 * progress in case this is a non-blocking connect. 1797 */ 1798 sock->state = SS_CONNECTING; 1799 err = -EINPROGRESS; 1800 } 1801 1802 /* The receive path will handle all communication until we are able to 1803 * enter the connected state. Here we wait for the connection to be 1804 * completed or a notification of an error. 1805 */ 1806 timeout = vsk->connect_timeout; 1807 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); 1808 1809 /* If the socket is already closing or it is in an error state, there 1810 * is no point in waiting. 1811 */ 1812 while (sk->sk_state != TCP_ESTABLISHED && 1813 sk->sk_state != TCP_CLOSING && sk->sk_err == 0) { 1814 if (flags & O_NONBLOCK) { 1815 /* If we're not going to block, we schedule a timeout 1816 * function to generate a timeout on the connection 1817 * attempt, in case the peer doesn't respond in a 1818 * timely manner. We hold on to the socket until the 1819 * timeout fires. 1820 */ 1821 sock_hold(sk); 1822 1823 /* If the timeout function is already scheduled, 1824 * reschedule it, then ungrab the socket refcount to 1825 * keep it balanced. 1826 */ 1827 if (mod_delayed_work(system_percpu_wq, &vsk->connect_work, 1828 timeout)) 1829 sock_put(sk); 1830 1831 /* Skip ahead to preserve error code set above. */ 1832 goto out_wait; 1833 } 1834 1835 release_sock(sk); 1836 timeout = schedule_timeout(timeout); 1837 lock_sock(sk); 1838 1839 /* Connection established. Whatever happens to socket once we 1840 * release it, that's not connect()'s concern. No need to go 1841 * into signal and timeout handling. Call it a day. 1842 * 1843 * Note that allowing to "reset" an already established socket 1844 * here is racy and insecure. 1845 */ 1846 if (sk->sk_state == TCP_ESTABLISHED) 1847 break; 1848 1849 /* If connection was _not_ established and a signal/timeout came 1850 * to be, we want the socket's state reset. User space may want 1851 * to retry. 1852 * 1853 * sk_state != TCP_ESTABLISHED implies that socket is not on 1854 * vsock_connected_table. We keep the binding and the transport 1855 * assigned. 1856 */ 1857 if (signal_pending(current) || timeout == 0) { 1858 err = timeout == 0 ? -ETIMEDOUT : sock_intr_errno(timeout); 1859 1860 /* Listener might have already responded with 1861 * VIRTIO_VSOCK_OP_RESPONSE. Its handling expects our 1862 * sk_state == TCP_SYN_SENT, which hereby we break. 1863 * In such case VIRTIO_VSOCK_OP_RST will follow. 1864 */ 1865 sk->sk_state = TCP_CLOSE; 1866 sock->state = SS_UNCONNECTED; 1867 1868 /* Try to cancel VIRTIO_VSOCK_OP_REQUEST skb sent out by 1869 * transport->connect(). 1870 */ 1871 vsock_transport_cancel_pkt(vsk); 1872 1873 goto out_wait; 1874 } 1875 1876 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); 1877 } 1878 1879 err = sock_error(sk); 1880 if (err) { 1881 sk->sk_state = TCP_CLOSE; 1882 sock->state = SS_UNCONNECTED; 1883 } 1884 1885 out_wait: 1886 finish_wait(sk_sleep(sk), &wait); 1887 out: 1888 release_sock(sk); 1889 return err; 1890 } 1891 1892 static int vsock_accept(struct socket *sock, struct socket *newsock, 1893 struct proto_accept_arg *arg) 1894 { 1895 struct sock *listener; 1896 int err; 1897 struct sock *connected; 1898 struct vsock_sock *vconnected; 1899 long timeout; 1900 DEFINE_WAIT(wait); 1901 1902 err = 0; 1903 listener = sock->sk; 1904 1905 lock_sock(listener); 1906 1907 if (!sock_type_connectible(sock->type)) { 1908 err = -EOPNOTSUPP; 1909 goto out; 1910 } 1911 1912 if (listener->sk_state != TCP_LISTEN) { 1913 err = -EINVAL; 1914 goto out; 1915 } 1916 1917 /* Wait for children sockets to appear; these are the new sockets 1918 * created upon connection establishment. 1919 */ 1920 timeout = sock_rcvtimeo(listener, arg->flags & O_NONBLOCK); 1921 1922 while ((connected = vsock_dequeue_accept(listener)) == NULL && 1923 timeout != 0) { 1924 prepare_to_wait(sk_sleep(listener), &wait, TASK_INTERRUPTIBLE); 1925 release_sock(listener); 1926 timeout = schedule_timeout(timeout); 1927 finish_wait(sk_sleep(listener), &wait); 1928 lock_sock(listener); 1929 1930 if (signal_pending(current)) { 1931 err = sock_intr_errno(timeout); 1932 goto out; 1933 } 1934 } 1935 1936 if (!connected) { 1937 err = -EAGAIN; 1938 } else { 1939 sk_acceptq_removed(listener); 1940 1941 lock_sock_nested(connected, SINGLE_DEPTH_NESTING); 1942 vconnected = vsock_sk(connected); 1943 1944 newsock->state = SS_CONNECTED; 1945 sock_graft(connected, newsock); 1946 1947 set_bit(SOCK_CUSTOM_SOCKOPT, 1948 &connected->sk_socket->flags); 1949 1950 if (vsock_msgzerocopy_allow(vconnected->transport)) 1951 set_bit(SOCK_SUPPORT_ZC, 1952 &connected->sk_socket->flags); 1953 1954 release_sock(connected); 1955 sock_put(connected); 1956 } 1957 1958 out: 1959 release_sock(listener); 1960 return err; 1961 } 1962 1963 static int vsock_listen(struct socket *sock, int backlog) 1964 { 1965 int err; 1966 struct sock *sk; 1967 struct vsock_sock *vsk; 1968 1969 sk = sock->sk; 1970 1971 lock_sock(sk); 1972 1973 if (!sock_type_connectible(sk->sk_type)) { 1974 err = -EOPNOTSUPP; 1975 goto out; 1976 } 1977 1978 if (sock->state != SS_UNCONNECTED) { 1979 err = -EINVAL; 1980 goto out; 1981 } 1982 1983 vsk = vsock_sk(sk); 1984 1985 if (!vsock_addr_bound(&vsk->local_addr)) { 1986 err = -EINVAL; 1987 goto out; 1988 } 1989 1990 sk->sk_max_ack_backlog = backlog; 1991 sk->sk_state = TCP_LISTEN; 1992 1993 err = 0; 1994 1995 out: 1996 release_sock(sk); 1997 return err; 1998 } 1999 2000 static void vsock_update_buffer_size(struct vsock_sock *vsk, 2001 const struct vsock_transport *transport, 2002 u64 val) 2003 { 2004 if (val < vsk->buffer_min_size) 2005 val = vsk->buffer_min_size; 2006 2007 if (val > vsk->buffer_max_size) 2008 val = vsk->buffer_max_size; 2009 2010 if (val != vsk->buffer_size && 2011 transport && transport->notify_buffer_size) 2012 transport->notify_buffer_size(vsk, &val); 2013 2014 vsk->buffer_size = val; 2015 } 2016 2017 static int vsock_connectible_setsockopt(struct socket *sock, 2018 int level, 2019 int optname, 2020 sockptr_t optval, 2021 unsigned int optlen) 2022 { 2023 int err; 2024 struct sock *sk; 2025 struct vsock_sock *vsk; 2026 const struct vsock_transport *transport; 2027 u64 val; 2028 2029 if (level != AF_VSOCK && level != SOL_SOCKET) 2030 return -ENOPROTOOPT; 2031 2032 #define COPY_IN(_v) \ 2033 do { \ 2034 if (optlen < sizeof(_v)) { \ 2035 err = -EINVAL; \ 2036 goto exit; \ 2037 } \ 2038 if (copy_from_sockptr(&_v, optval, sizeof(_v)) != 0) { \ 2039 err = -EFAULT; \ 2040 goto exit; \ 2041 } \ 2042 } while (0) 2043 2044 err = 0; 2045 sk = sock->sk; 2046 vsk = vsock_sk(sk); 2047 2048 lock_sock(sk); 2049 2050 transport = vsk->transport; 2051 2052 if (level == SOL_SOCKET) { 2053 int zerocopy; 2054 2055 if (optname != SO_ZEROCOPY) { 2056 release_sock(sk); 2057 return sock_setsockopt(sock, level, optname, optval, optlen); 2058 } 2059 2060 /* Use 'int' type here, because variable to 2061 * set this option usually has this type. 2062 */ 2063 COPY_IN(zerocopy); 2064 2065 if (zerocopy < 0 || zerocopy > 1) { 2066 err = -EINVAL; 2067 goto exit; 2068 } 2069 2070 if (transport && !vsock_msgzerocopy_allow(transport)) { 2071 err = -EOPNOTSUPP; 2072 goto exit; 2073 } 2074 2075 sock_valbool_flag(sk, SOCK_ZEROCOPY, zerocopy); 2076 goto exit; 2077 } 2078 2079 switch (optname) { 2080 case SO_VM_SOCKETS_BUFFER_SIZE: 2081 COPY_IN(val); 2082 vsock_update_buffer_size(vsk, transport, val); 2083 break; 2084 2085 case SO_VM_SOCKETS_BUFFER_MAX_SIZE: 2086 COPY_IN(val); 2087 vsk->buffer_max_size = val; 2088 vsock_update_buffer_size(vsk, transport, vsk->buffer_size); 2089 break; 2090 2091 case SO_VM_SOCKETS_BUFFER_MIN_SIZE: 2092 COPY_IN(val); 2093 vsk->buffer_min_size = val; 2094 vsock_update_buffer_size(vsk, transport, vsk->buffer_size); 2095 break; 2096 2097 case SO_VM_SOCKETS_CONNECT_TIMEOUT_NEW: 2098 case SO_VM_SOCKETS_CONNECT_TIMEOUT_OLD: { 2099 struct __kernel_sock_timeval tv; 2100 2101 err = sock_copy_user_timeval(&tv, optval, optlen, 2102 optname == SO_VM_SOCKETS_CONNECT_TIMEOUT_OLD); 2103 if (err) 2104 break; 2105 if (tv.tv_sec >= 0 && tv.tv_usec < USEC_PER_SEC && 2106 tv.tv_sec < (MAX_SCHEDULE_TIMEOUT / HZ - 1)) { 2107 vsk->connect_timeout = tv.tv_sec * HZ + 2108 DIV_ROUND_UP((unsigned long)tv.tv_usec, (USEC_PER_SEC / HZ)); 2109 if (vsk->connect_timeout == 0) 2110 vsk->connect_timeout = 2111 VSOCK_DEFAULT_CONNECT_TIMEOUT; 2112 2113 } else { 2114 err = -ERANGE; 2115 } 2116 break; 2117 } 2118 2119 default: 2120 err = -ENOPROTOOPT; 2121 break; 2122 } 2123 2124 #undef COPY_IN 2125 2126 exit: 2127 release_sock(sk); 2128 return err; 2129 } 2130 2131 static int vsock_connectible_getsockopt(struct socket *sock, 2132 int level, int optname, 2133 sockopt_t *opt) 2134 { 2135 struct sock *sk = sock->sk; 2136 struct vsock_sock *vsk = vsock_sk(sk); 2137 2138 union { 2139 u64 val64; 2140 struct old_timeval32 tm32; 2141 struct __kernel_old_timeval tm; 2142 struct __kernel_sock_timeval stm; 2143 } v; 2144 2145 int lv = sizeof(v.val64); 2146 int len; 2147 2148 if (level != AF_VSOCK) 2149 return -ENOPROTOOPT; 2150 2151 len = opt->optlen; 2152 2153 memset(&v, 0, sizeof(v)); 2154 2155 switch (optname) { 2156 case SO_VM_SOCKETS_BUFFER_SIZE: 2157 v.val64 = vsk->buffer_size; 2158 break; 2159 2160 case SO_VM_SOCKETS_BUFFER_MAX_SIZE: 2161 v.val64 = vsk->buffer_max_size; 2162 break; 2163 2164 case SO_VM_SOCKETS_BUFFER_MIN_SIZE: 2165 v.val64 = vsk->buffer_min_size; 2166 break; 2167 2168 case SO_VM_SOCKETS_CONNECT_TIMEOUT_NEW: 2169 case SO_VM_SOCKETS_CONNECT_TIMEOUT_OLD: 2170 lv = sock_get_timeout(vsk->connect_timeout, &v, 2171 optname == SO_VM_SOCKETS_CONNECT_TIMEOUT_OLD); 2172 break; 2173 2174 default: 2175 return -ENOPROTOOPT; 2176 } 2177 2178 if (len < lv) 2179 return -EINVAL; 2180 if (len > lv) 2181 len = lv; 2182 if (copy_to_iter(&v, len, &opt->iter_out) != len) 2183 return -EFAULT; 2184 2185 opt->optlen = len; 2186 2187 return 0; 2188 } 2189 2190 static int vsock_connectible_sendmsg(struct socket *sock, struct msghdr *msg, 2191 size_t len) 2192 { 2193 struct sock *sk; 2194 struct vsock_sock *vsk; 2195 const struct vsock_transport *transport; 2196 ssize_t total_written; 2197 long timeout; 2198 int err; 2199 struct vsock_transport_send_notify_data send_data; 2200 DEFINE_WAIT_FUNC(wait, woken_wake_function); 2201 2202 sk = sock->sk; 2203 vsk = vsock_sk(sk); 2204 total_written = 0; 2205 err = 0; 2206 2207 if (msg->msg_flags & MSG_OOB) 2208 return -EOPNOTSUPP; 2209 2210 lock_sock(sk); 2211 2212 transport = vsk->transport; 2213 2214 /* Callers should not provide a destination with connection oriented 2215 * sockets. 2216 */ 2217 if (msg->msg_namelen) { 2218 err = sk->sk_state == TCP_ESTABLISHED ? -EISCONN : -EOPNOTSUPP; 2219 goto out; 2220 } 2221 2222 /* Send data only if both sides are not shutdown in the direction. */ 2223 if (sk->sk_shutdown & SEND_SHUTDOWN || 2224 vsk->peer_shutdown & RCV_SHUTDOWN) { 2225 err = -EPIPE; 2226 goto out; 2227 } 2228 2229 if (!transport || sk->sk_state != TCP_ESTABLISHED || 2230 !vsock_addr_bound(&vsk->local_addr)) { 2231 err = -ENOTCONN; 2232 goto out; 2233 } 2234 2235 if (!vsock_addr_bound(&vsk->remote_addr)) { 2236 err = -EDESTADDRREQ; 2237 goto out; 2238 } 2239 2240 if (msg->msg_flags & MSG_ZEROCOPY && 2241 !vsock_msgzerocopy_allow(transport)) { 2242 err = -EOPNOTSUPP; 2243 goto out; 2244 } 2245 2246 /* Wait for room in the produce queue to enqueue our user's data. */ 2247 timeout = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); 2248 2249 err = transport->notify_send_init(vsk, &send_data); 2250 if (err < 0) 2251 goto out; 2252 2253 while (total_written < len) { 2254 ssize_t written; 2255 2256 add_wait_queue(sk_sleep(sk), &wait); 2257 while (vsock_stream_has_space(vsk) == 0 && 2258 sk->sk_err == 0 && 2259 !(sk->sk_shutdown & SEND_SHUTDOWN) && 2260 !(vsk->peer_shutdown & RCV_SHUTDOWN)) { 2261 2262 /* Don't wait for non-blocking sockets. */ 2263 if (timeout == 0) { 2264 err = -EAGAIN; 2265 remove_wait_queue(sk_sleep(sk), &wait); 2266 goto out_err; 2267 } 2268 2269 err = transport->notify_send_pre_block(vsk, &send_data); 2270 if (err < 0) { 2271 remove_wait_queue(sk_sleep(sk), &wait); 2272 goto out_err; 2273 } 2274 2275 release_sock(sk); 2276 timeout = wait_woken(&wait, TASK_INTERRUPTIBLE, timeout); 2277 lock_sock(sk); 2278 if (signal_pending(current)) { 2279 err = sock_intr_errno(timeout); 2280 remove_wait_queue(sk_sleep(sk), &wait); 2281 goto out_err; 2282 } else if (timeout == 0) { 2283 err = -EAGAIN; 2284 remove_wait_queue(sk_sleep(sk), &wait); 2285 goto out_err; 2286 } 2287 } 2288 remove_wait_queue(sk_sleep(sk), &wait); 2289 2290 /* These checks occur both as part of and after the loop 2291 * conditional since we need to check before and after 2292 * sleeping. 2293 */ 2294 if (sk->sk_err) { 2295 err = -sk->sk_err; 2296 goto out_err; 2297 } else if ((sk->sk_shutdown & SEND_SHUTDOWN) || 2298 (vsk->peer_shutdown & RCV_SHUTDOWN)) { 2299 err = -EPIPE; 2300 goto out_err; 2301 } 2302 2303 err = transport->notify_send_pre_enqueue(vsk, &send_data); 2304 if (err < 0) 2305 goto out_err; 2306 2307 /* Note that enqueue will only write as many bytes as are free 2308 * in the produce queue, so we don't need to ensure len is 2309 * smaller than the queue size. It is the caller's 2310 * responsibility to check how many bytes we were able to send. 2311 */ 2312 2313 if (sk->sk_type == SOCK_SEQPACKET) { 2314 written = transport->seqpacket_enqueue(vsk, 2315 msg, len - total_written); 2316 } else { 2317 written = transport->stream_enqueue(vsk, 2318 msg, len - total_written); 2319 } 2320 2321 if (written < 0) { 2322 err = written; 2323 goto out_err; 2324 } 2325 2326 total_written += written; 2327 2328 err = transport->notify_send_post_enqueue( 2329 vsk, written, &send_data); 2330 if (err < 0) 2331 goto out_err; 2332 2333 } 2334 2335 out_err: 2336 if (total_written > 0) { 2337 /* Return number of written bytes only if: 2338 * 1) SOCK_STREAM socket. 2339 * 2) SOCK_SEQPACKET socket when whole buffer is sent. 2340 */ 2341 if (sk->sk_type == SOCK_STREAM || total_written == len) 2342 err = total_written; 2343 } 2344 out: 2345 if (sk->sk_type == SOCK_STREAM) 2346 err = sk_stream_error(sk, msg->msg_flags, err); 2347 2348 release_sock(sk); 2349 return err; 2350 } 2351 2352 static int vsock_connectible_wait_data(struct sock *sk, 2353 struct wait_queue_entry *wait, 2354 long timeout, 2355 struct vsock_transport_recv_notify_data *recv_data, 2356 size_t target) 2357 { 2358 const struct vsock_transport *transport; 2359 struct vsock_sock *vsk; 2360 s64 data; 2361 int err; 2362 2363 vsk = vsock_sk(sk); 2364 err = 0; 2365 transport = vsk->transport; 2366 2367 while (1) { 2368 prepare_to_wait(sk_sleep(sk), wait, TASK_INTERRUPTIBLE); 2369 data = vsock_connectible_has_data(vsk); 2370 if (data != 0) 2371 break; 2372 2373 if (sk->sk_err != 0 || 2374 (sk->sk_shutdown & RCV_SHUTDOWN) || 2375 (vsk->peer_shutdown & SEND_SHUTDOWN)) { 2376 break; 2377 } 2378 2379 /* Don't wait for non-blocking sockets. */ 2380 if (timeout == 0) { 2381 err = -EAGAIN; 2382 break; 2383 } 2384 2385 if (recv_data) { 2386 err = transport->notify_recv_pre_block(vsk, target, recv_data); 2387 if (err < 0) 2388 break; 2389 } 2390 2391 release_sock(sk); 2392 timeout = schedule_timeout(timeout); 2393 lock_sock(sk); 2394 2395 if (signal_pending(current)) { 2396 err = sock_intr_errno(timeout); 2397 break; 2398 } else if (timeout == 0) { 2399 err = -EAGAIN; 2400 break; 2401 } 2402 } 2403 2404 finish_wait(sk_sleep(sk), wait); 2405 2406 if (err) 2407 return err; 2408 2409 /* Internal transport error when checking for available 2410 * data. XXX This should be changed to a connection 2411 * reset in a later change. 2412 */ 2413 if (data < 0) 2414 return -ENOMEM; 2415 2416 return data; 2417 } 2418 2419 static int __vsock_stream_recvmsg(struct sock *sk, struct msghdr *msg, 2420 size_t len, int flags) 2421 { 2422 struct vsock_transport_recv_notify_data recv_data; 2423 const struct vsock_transport *transport; 2424 struct vsock_sock *vsk; 2425 ssize_t copied; 2426 size_t target; 2427 long timeout; 2428 int err; 2429 2430 DEFINE_WAIT(wait); 2431 2432 vsk = vsock_sk(sk); 2433 transport = vsk->transport; 2434 2435 /* We must not copy less than target bytes into the user's buffer 2436 * before returning successfully, so we wait for the consume queue to 2437 * have that much data to consume before dequeueing. Note that this 2438 * makes it impossible to handle cases where target is greater than the 2439 * queue size. 2440 */ 2441 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); 2442 if (target >= transport->stream_rcvhiwat(vsk)) { 2443 err = -ENOMEM; 2444 goto out; 2445 } 2446 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 2447 copied = 0; 2448 2449 err = transport->notify_recv_init(vsk, target, &recv_data); 2450 if (err < 0) 2451 goto out; 2452 2453 2454 while (1) { 2455 ssize_t read; 2456 2457 err = vsock_connectible_wait_data(sk, &wait, timeout, 2458 &recv_data, target); 2459 if (err <= 0) 2460 break; 2461 2462 err = transport->notify_recv_pre_dequeue(vsk, target, 2463 &recv_data); 2464 if (err < 0) 2465 break; 2466 2467 read = transport->stream_dequeue(vsk, msg, len - copied, flags); 2468 if (read < 0) { 2469 err = read; 2470 break; 2471 } 2472 2473 copied += read; 2474 2475 err = transport->notify_recv_post_dequeue(vsk, target, read, 2476 !(flags & MSG_PEEK), &recv_data); 2477 if (err < 0) 2478 goto out; 2479 2480 if (read >= target || flags & MSG_PEEK) 2481 break; 2482 2483 target -= read; 2484 } 2485 2486 if (sk->sk_err) 2487 err = -sk->sk_err; 2488 else if (sk->sk_shutdown & RCV_SHUTDOWN) 2489 err = 0; 2490 2491 if (copied > 0) 2492 err = copied; 2493 2494 out: 2495 return err; 2496 } 2497 2498 static int __vsock_seqpacket_recvmsg(struct sock *sk, struct msghdr *msg, 2499 size_t len, int flags) 2500 { 2501 const struct vsock_transport *transport; 2502 struct vsock_sock *vsk; 2503 ssize_t msg_len; 2504 long timeout; 2505 int err = 0; 2506 DEFINE_WAIT(wait); 2507 2508 vsk = vsock_sk(sk); 2509 transport = vsk->transport; 2510 2511 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 2512 2513 err = vsock_connectible_wait_data(sk, &wait, timeout, NULL, 0); 2514 if (err <= 0) 2515 goto out; 2516 2517 msg_len = transport->seqpacket_dequeue(vsk, msg, flags); 2518 2519 if (msg_len < 0) { 2520 err = msg_len; 2521 goto out; 2522 } 2523 2524 if (sk->sk_err) { 2525 err = -sk->sk_err; 2526 } else if (sk->sk_shutdown & RCV_SHUTDOWN) { 2527 err = 0; 2528 } else { 2529 /* User sets MSG_TRUNC, so return real length of 2530 * packet. 2531 */ 2532 if (flags & MSG_TRUNC) 2533 err = msg_len; 2534 else 2535 err = len - msg_data_left(msg); 2536 2537 /* Always set MSG_TRUNC if real length of packet is 2538 * bigger than user's buffer. 2539 */ 2540 if (msg_len > len) 2541 msg->msg_flags |= MSG_TRUNC; 2542 } 2543 2544 out: 2545 return err; 2546 } 2547 2548 int 2549 __vsock_connectible_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, 2550 int flags) 2551 { 2552 struct sock *sk; 2553 struct vsock_sock *vsk; 2554 const struct vsock_transport *transport; 2555 int err; 2556 2557 sk = sock->sk; 2558 2559 if (unlikely(flags & MSG_ERRQUEUE)) 2560 return sock_recv_errqueue(sk, msg, len, SOL_VSOCK, VSOCK_RECVERR); 2561 2562 vsk = vsock_sk(sk); 2563 err = 0; 2564 2565 lock_sock(sk); 2566 2567 transport = vsk->transport; 2568 2569 if (!transport || sk->sk_state != TCP_ESTABLISHED) { 2570 /* Recvmsg is supposed to return 0 if a peer performs an 2571 * orderly shutdown. Differentiate between that case and when a 2572 * peer has not connected or a local shutdown occurred with the 2573 * SOCK_DONE flag. 2574 */ 2575 if (sock_flag(sk, SOCK_DONE)) 2576 err = 0; 2577 else 2578 err = -ENOTCONN; 2579 2580 goto out; 2581 } 2582 2583 if (flags & MSG_OOB) { 2584 err = -EOPNOTSUPP; 2585 goto out; 2586 } 2587 2588 /* We don't check peer_shutdown flag here since peer may actually shut 2589 * down, but there can be data in the queue that a local socket can 2590 * receive. 2591 */ 2592 if (sk->sk_shutdown & RCV_SHUTDOWN) { 2593 err = 0; 2594 goto out; 2595 } 2596 2597 /* It is valid on Linux to pass in a zero-length receive buffer. This 2598 * is not an error. We may as well bail out now. 2599 */ 2600 if (!len) { 2601 err = 0; 2602 goto out; 2603 } 2604 2605 if (sk->sk_type == SOCK_STREAM) 2606 err = __vsock_stream_recvmsg(sk, msg, len, flags); 2607 else 2608 err = __vsock_seqpacket_recvmsg(sk, msg, len, flags); 2609 2610 out: 2611 release_sock(sk); 2612 return err; 2613 } 2614 2615 int 2616 vsock_connectible_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, 2617 int flags) 2618 { 2619 #ifdef CONFIG_BPF_SYSCALL 2620 struct sock *sk = sock->sk; 2621 const struct proto *prot; 2622 2623 prot = READ_ONCE(sk->sk_prot); 2624 if (prot != &vsock_proto) 2625 return prot->recvmsg(sk, msg, len, flags); 2626 #endif 2627 2628 return __vsock_connectible_recvmsg(sock, msg, len, flags); 2629 } 2630 EXPORT_SYMBOL_GPL(vsock_connectible_recvmsg); 2631 2632 static int vsock_set_rcvlowat(struct sock *sk, int val) 2633 { 2634 const struct vsock_transport *transport; 2635 struct vsock_sock *vsk; 2636 2637 vsk = vsock_sk(sk); 2638 2639 if (val > vsk->buffer_size) 2640 return -EINVAL; 2641 2642 transport = vsk->transport; 2643 2644 if (transport && transport->notify_set_rcvlowat) { 2645 int err; 2646 2647 err = transport->notify_set_rcvlowat(vsk, val); 2648 if (err) 2649 return err; 2650 } 2651 2652 WRITE_ONCE(sk->sk_rcvlowat, val ? : 1); 2653 return 0; 2654 } 2655 2656 static const struct proto_ops vsock_stream_ops = { 2657 .family = PF_VSOCK, 2658 .owner = THIS_MODULE, 2659 .release = vsock_release, 2660 .bind = vsock_bind, 2661 .connect = vsock_connect, 2662 .socketpair = sock_no_socketpair, 2663 .accept = vsock_accept, 2664 .getname = vsock_getname, 2665 .poll = vsock_poll, 2666 .ioctl = vsock_ioctl, 2667 .listen = vsock_listen, 2668 .shutdown = vsock_shutdown, 2669 .setsockopt = vsock_connectible_setsockopt, 2670 .getsockopt_iter = vsock_connectible_getsockopt, 2671 .sendmsg = vsock_connectible_sendmsg, 2672 .recvmsg = vsock_connectible_recvmsg, 2673 .mmap = sock_no_mmap, 2674 .set_rcvlowat = vsock_set_rcvlowat, 2675 .read_skb = vsock_read_skb, 2676 }; 2677 2678 static const struct proto_ops vsock_seqpacket_ops = { 2679 .family = PF_VSOCK, 2680 .owner = THIS_MODULE, 2681 .release = vsock_release, 2682 .bind = vsock_bind, 2683 .connect = vsock_connect, 2684 .socketpair = sock_no_socketpair, 2685 .accept = vsock_accept, 2686 .getname = vsock_getname, 2687 .poll = vsock_poll, 2688 .ioctl = vsock_ioctl, 2689 .listen = vsock_listen, 2690 .shutdown = vsock_shutdown, 2691 .setsockopt = vsock_connectible_setsockopt, 2692 .getsockopt_iter = vsock_connectible_getsockopt, 2693 .sendmsg = vsock_connectible_sendmsg, 2694 .recvmsg = vsock_connectible_recvmsg, 2695 .mmap = sock_no_mmap, 2696 .read_skb = vsock_read_skb, 2697 }; 2698 2699 static int vsock_create(struct net *net, struct socket *sock, 2700 int protocol, int kern) 2701 { 2702 struct vsock_sock *vsk; 2703 struct sock *sk; 2704 int ret; 2705 2706 if (!sock) 2707 return -EINVAL; 2708 2709 if (protocol && protocol != PF_VSOCK) 2710 return -EPROTONOSUPPORT; 2711 2712 switch (sock->type) { 2713 case SOCK_DGRAM: 2714 sock->ops = &vsock_dgram_ops; 2715 break; 2716 case SOCK_STREAM: 2717 sock->ops = &vsock_stream_ops; 2718 break; 2719 case SOCK_SEQPACKET: 2720 sock->ops = &vsock_seqpacket_ops; 2721 break; 2722 default: 2723 return -ESOCKTNOSUPPORT; 2724 } 2725 2726 sock->state = SS_UNCONNECTED; 2727 2728 sk = __vsock_create(net, sock, NULL, GFP_KERNEL, 0, kern); 2729 if (!sk) 2730 return -ENOMEM; 2731 2732 vsk = vsock_sk(sk); 2733 2734 if (sock->type == SOCK_DGRAM) { 2735 ret = vsock_assign_transport(vsk, NULL); 2736 if (ret < 0) { 2737 sock->sk = NULL; 2738 sock_put(sk); 2739 return ret; 2740 } 2741 } 2742 2743 /* SOCK_DGRAM doesn't have 'setsockopt' callback set in its 2744 * proto_ops, so there is no handler for custom logic. 2745 */ 2746 if (sock_type_connectible(sock->type)) 2747 set_bit(SOCK_CUSTOM_SOCKOPT, &sk->sk_socket->flags); 2748 2749 vsock_insert_unbound(vsk); 2750 2751 return 0; 2752 } 2753 2754 static const struct net_proto_family vsock_family_ops = { 2755 .family = AF_VSOCK, 2756 .create = vsock_create, 2757 .owner = THIS_MODULE, 2758 }; 2759 2760 static long vsock_dev_do_ioctl(struct file *filp, 2761 unsigned int cmd, void __user *ptr) 2762 { 2763 u32 __user *p = ptr; 2764 int retval = 0; 2765 u32 cid; 2766 2767 switch (cmd) { 2768 case IOCTL_VM_SOCKETS_GET_LOCAL_CID: 2769 /* To be compatible with the VMCI behavior, we prioritize the 2770 * guest CID instead of well-know host CID (VMADDR_CID_HOST). 2771 */ 2772 cid = vsock_registered_transport_cid(&transport_g2h); 2773 if (cid == VMADDR_CID_ANY) 2774 cid = vsock_registered_transport_cid(&transport_h2g); 2775 if (cid == VMADDR_CID_ANY) 2776 cid = vsock_registered_transport_cid(&transport_local); 2777 2778 if (put_user(cid, p) != 0) 2779 retval = -EFAULT; 2780 break; 2781 2782 default: 2783 retval = -ENOIOCTLCMD; 2784 } 2785 2786 return retval; 2787 } 2788 2789 static long vsock_dev_ioctl(struct file *filp, 2790 unsigned int cmd, unsigned long arg) 2791 { 2792 return vsock_dev_do_ioctl(filp, cmd, (void __user *)arg); 2793 } 2794 2795 #ifdef CONFIG_COMPAT 2796 static long vsock_dev_compat_ioctl(struct file *filp, 2797 unsigned int cmd, unsigned long arg) 2798 { 2799 return vsock_dev_do_ioctl(filp, cmd, compat_ptr(arg)); 2800 } 2801 #endif 2802 2803 static const struct file_operations vsock_device_ops = { 2804 .owner = THIS_MODULE, 2805 .unlocked_ioctl = vsock_dev_ioctl, 2806 #ifdef CONFIG_COMPAT 2807 .compat_ioctl = vsock_dev_compat_ioctl, 2808 #endif 2809 .open = nonseekable_open, 2810 }; 2811 2812 static struct miscdevice vsock_device = { 2813 .name = "vsock", 2814 .fops = &vsock_device_ops, 2815 }; 2816 2817 static int __vsock_net_mode_string(const struct ctl_table *table, int write, 2818 void *buffer, size_t *lenp, loff_t *ppos, 2819 enum vsock_net_mode mode, 2820 enum vsock_net_mode *new_mode) 2821 { 2822 char data[VSOCK_NET_MODE_STR_MAX] = {0}; 2823 struct ctl_table tmp; 2824 int ret; 2825 2826 if (!table->data || !table->maxlen || !*lenp) { 2827 *lenp = 0; 2828 return 0; 2829 } 2830 2831 tmp = *table; 2832 tmp.data = data; 2833 2834 if (!write) { 2835 const char *p; 2836 2837 switch (mode) { 2838 case VSOCK_NET_MODE_GLOBAL: 2839 p = VSOCK_NET_MODE_STR_GLOBAL; 2840 break; 2841 case VSOCK_NET_MODE_LOCAL: 2842 p = VSOCK_NET_MODE_STR_LOCAL; 2843 break; 2844 default: 2845 WARN_ONCE(true, "netns has invalid vsock mode"); 2846 *lenp = 0; 2847 return 0; 2848 } 2849 2850 strscpy(data, p, sizeof(data)); 2851 tmp.maxlen = strlen(p); 2852 } 2853 2854 ret = proc_dostring(&tmp, write, buffer, lenp, ppos); 2855 if (ret || !write) 2856 return ret; 2857 2858 if (*lenp >= sizeof(data)) 2859 return -EINVAL; 2860 2861 if (!strncmp(data, VSOCK_NET_MODE_STR_GLOBAL, sizeof(data))) 2862 *new_mode = VSOCK_NET_MODE_GLOBAL; 2863 else if (!strncmp(data, VSOCK_NET_MODE_STR_LOCAL, sizeof(data))) 2864 *new_mode = VSOCK_NET_MODE_LOCAL; 2865 else 2866 return -EINVAL; 2867 2868 return 0; 2869 } 2870 2871 static int vsock_net_mode_string(const struct ctl_table *table, int write, 2872 void *buffer, size_t *lenp, loff_t *ppos) 2873 { 2874 struct net *net; 2875 2876 if (write) 2877 return -EPERM; 2878 2879 net = container_of(table->data, struct net, vsock.mode); 2880 2881 return __vsock_net_mode_string(table, write, buffer, lenp, ppos, 2882 vsock_net_mode(net), NULL); 2883 } 2884 2885 static int vsock_net_child_mode_string(const struct ctl_table *table, int write, 2886 void *buffer, size_t *lenp, loff_t *ppos) 2887 { 2888 enum vsock_net_mode new_mode; 2889 struct net *net; 2890 int ret; 2891 2892 net = container_of(table->data, struct net, vsock.child_ns_mode); 2893 2894 if (!*lenp) 2895 return 0; 2896 2897 ret = __vsock_net_mode_string(table, write, buffer, lenp, ppos, 2898 vsock_net_child_mode(net), &new_mode); 2899 if (ret) 2900 return ret; 2901 2902 if (write) { 2903 /* Prevent a "local" namespace from escalating to "global", 2904 * which would give nested namespaces access to global CIDs. 2905 */ 2906 if (vsock_net_mode(net) == VSOCK_NET_MODE_LOCAL && 2907 new_mode == VSOCK_NET_MODE_GLOBAL) 2908 return -EPERM; 2909 2910 if (!vsock_net_set_child_mode(net, new_mode)) 2911 return -EBUSY; 2912 } 2913 2914 return 0; 2915 } 2916 2917 static const struct ctl_table vsock_table[] = { 2918 { 2919 .procname = "ns_mode", 2920 .data = &init_net.vsock.mode, 2921 .maxlen = VSOCK_NET_MODE_STR_MAX, 2922 .mode = 0444, 2923 .proc_handler = vsock_net_mode_string 2924 }, 2925 { 2926 .procname = "child_ns_mode", 2927 .data = &init_net.vsock.child_ns_mode, 2928 .maxlen = VSOCK_NET_MODE_STR_MAX, 2929 .mode = 0644, 2930 .proc_handler = vsock_net_child_mode_string 2931 }, 2932 { 2933 .procname = "g2h_fallback", 2934 .data = &init_net.vsock.g2h_fallback, 2935 .maxlen = sizeof(int), 2936 .mode = 0644, 2937 .proc_handler = proc_dointvec_minmax, 2938 .extra1 = SYSCTL_ZERO, 2939 .extra2 = SYSCTL_ONE, 2940 }, 2941 }; 2942 2943 static const struct ctl_table *vsock_table_dup(struct net *net) 2944 { 2945 struct ctl_table *table; 2946 2947 table = kmemdup(vsock_table, sizeof(vsock_table), GFP_KERNEL); 2948 if (!table) 2949 return NULL; 2950 2951 table[0].data = &net->vsock.mode; 2952 table[1].data = &net->vsock.child_ns_mode; 2953 table[2].data = &net->vsock.g2h_fallback; 2954 2955 return table; 2956 } 2957 2958 static int __net_init vsock_sysctl_register(struct net *net) 2959 { 2960 const struct ctl_table *table; 2961 2962 if (net_eq(net, &init_net)) { 2963 table = vsock_table; 2964 } else { 2965 table = vsock_table_dup(net); 2966 if (!table) 2967 goto err_alloc; 2968 } 2969 2970 net->vsock.sysctl_hdr = register_net_sysctl_sz(net, "net/vsock", table, 2971 ARRAY_SIZE(vsock_table)); 2972 if (!net->vsock.sysctl_hdr) 2973 goto err_reg; 2974 2975 return 0; 2976 2977 err_reg: 2978 if (!net_eq(net, &init_net)) 2979 kfree(table); 2980 err_alloc: 2981 return -ENOMEM; 2982 } 2983 2984 static void vsock_sysctl_unregister(struct net *net) 2985 { 2986 const struct ctl_table *table; 2987 2988 table = net->vsock.sysctl_hdr->ctl_table_arg; 2989 unregister_net_sysctl_table(net->vsock.sysctl_hdr); 2990 if (!net_eq(net, &init_net)) 2991 kfree(table); 2992 } 2993 2994 static void vsock_net_init(struct net *net) 2995 { 2996 if (net_eq(net, &init_net)) 2997 net->vsock.mode = VSOCK_NET_MODE_GLOBAL; 2998 else 2999 net->vsock.mode = vsock_net_child_mode(current->nsproxy->net_ns); 3000 3001 net->vsock.child_ns_mode = net->vsock.mode; 3002 net->vsock.child_ns_mode_locked = 0; 3003 net->vsock.g2h_fallback = 1; 3004 } 3005 3006 static __net_init int vsock_sysctl_init_net(struct net *net) 3007 { 3008 vsock_net_init(net); 3009 3010 if (vsock_sysctl_register(net)) 3011 return -ENOMEM; 3012 3013 return 0; 3014 } 3015 3016 static __net_exit void vsock_sysctl_exit_net(struct net *net) 3017 { 3018 vsock_sysctl_unregister(net); 3019 } 3020 3021 static struct pernet_operations vsock_sysctl_ops = { 3022 .init = vsock_sysctl_init_net, 3023 .exit = vsock_sysctl_exit_net, 3024 }; 3025 3026 static int __init vsock_init(void) 3027 { 3028 int err = 0; 3029 3030 vsock_init_tables(); 3031 3032 vsock_proto.owner = THIS_MODULE; 3033 vsock_device.minor = MISC_DYNAMIC_MINOR; 3034 err = misc_register(&vsock_device); 3035 if (err) { 3036 pr_err("Failed to register misc device\n"); 3037 goto err_reset_transport; 3038 } 3039 3040 err = proto_register(&vsock_proto, 1); /* we want our slab */ 3041 if (err) { 3042 pr_err("Cannot register vsock protocol\n"); 3043 goto err_deregister_misc; 3044 } 3045 3046 err = sock_register(&vsock_family_ops); 3047 if (err) { 3048 pr_err("could not register af_vsock (%d) address family: %d\n", 3049 AF_VSOCK, err); 3050 goto err_unregister_proto; 3051 } 3052 3053 if (register_pernet_subsys(&vsock_sysctl_ops)) { 3054 err = -ENOMEM; 3055 goto err_unregister_sock; 3056 } 3057 3058 vsock_bpf_build_proto(); 3059 3060 return 0; 3061 3062 err_unregister_sock: 3063 sock_unregister(AF_VSOCK); 3064 err_unregister_proto: 3065 proto_unregister(&vsock_proto); 3066 err_deregister_misc: 3067 misc_deregister(&vsock_device); 3068 err_reset_transport: 3069 return err; 3070 } 3071 3072 static void __exit vsock_exit(void) 3073 { 3074 misc_deregister(&vsock_device); 3075 sock_unregister(AF_VSOCK); 3076 proto_unregister(&vsock_proto); 3077 unregister_pernet_subsys(&vsock_sysctl_ops); 3078 } 3079 3080 const struct vsock_transport *vsock_core_get_transport(struct vsock_sock *vsk) 3081 { 3082 return vsk->transport; 3083 } 3084 EXPORT_SYMBOL_GPL(vsock_core_get_transport); 3085 3086 int vsock_core_register(const struct vsock_transport *t, int features) 3087 { 3088 const struct vsock_transport *t_h2g, *t_g2h, *t_dgram, *t_local; 3089 int err = mutex_lock_interruptible(&vsock_register_mutex); 3090 3091 if (err) 3092 return err; 3093 3094 t_h2g = transport_h2g; 3095 t_g2h = transport_g2h; 3096 t_dgram = transport_dgram; 3097 t_local = transport_local; 3098 3099 if (features & VSOCK_TRANSPORT_F_H2G) { 3100 if (t_h2g) { 3101 err = -EBUSY; 3102 goto err_busy; 3103 } 3104 t_h2g = t; 3105 } 3106 3107 if (features & VSOCK_TRANSPORT_F_G2H) { 3108 if (t_g2h) { 3109 err = -EBUSY; 3110 goto err_busy; 3111 } 3112 t_g2h = t; 3113 } 3114 3115 if (features & VSOCK_TRANSPORT_F_DGRAM) { 3116 if (t_dgram) { 3117 err = -EBUSY; 3118 goto err_busy; 3119 } 3120 t_dgram = t; 3121 } 3122 3123 if (features & VSOCK_TRANSPORT_F_LOCAL) { 3124 if (t_local) { 3125 err = -EBUSY; 3126 goto err_busy; 3127 } 3128 t_local = t; 3129 } 3130 3131 transport_h2g = t_h2g; 3132 transport_g2h = t_g2h; 3133 transport_dgram = t_dgram; 3134 transport_local = t_local; 3135 3136 err_busy: 3137 mutex_unlock(&vsock_register_mutex); 3138 return err; 3139 } 3140 EXPORT_SYMBOL_GPL(vsock_core_register); 3141 3142 void vsock_core_unregister(const struct vsock_transport *t) 3143 { 3144 mutex_lock(&vsock_register_mutex); 3145 3146 if (transport_h2g == t) 3147 transport_h2g = NULL; 3148 3149 if (transport_g2h == t) 3150 transport_g2h = NULL; 3151 3152 if (transport_dgram == t) 3153 transport_dgram = NULL; 3154 3155 if (transport_local == t) 3156 transport_local = NULL; 3157 3158 mutex_unlock(&vsock_register_mutex); 3159 } 3160 EXPORT_SYMBOL_GPL(vsock_core_unregister); 3161 3162 module_init(vsock_init); 3163 module_exit(vsock_exit); 3164 3165 MODULE_AUTHOR("VMware, Inc."); 3166 MODULE_DESCRIPTION("VMware Virtual Socket Family"); 3167 MODULE_VERSION("1.0.2.0-k"); 3168 MODULE_LICENSE("GPL v2"); 3169