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