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