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