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