1 /* 2 * VMware vSockets Driver 3 * 4 * Copyright (C) 2007-2013 VMware, Inc. All rights reserved. 5 * 6 * This program is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License as published by the Free 8 * Software Foundation version 2 and no later version. 9 * 10 * This program is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 13 * more details. 14 */ 15 16 #include <linux/types.h> 17 #include <linux/bitops.h> 18 #include <linux/cred.h> 19 #include <linux/init.h> 20 #include <linux/io.h> 21 #include <linux/kernel.h> 22 #include <linux/kmod.h> 23 #include <linux/list.h> 24 #include <linux/module.h> 25 #include <linux/mutex.h> 26 #include <linux/net.h> 27 #include <linux/poll.h> 28 #include <linux/skbuff.h> 29 #include <linux/smp.h> 30 #include <linux/socket.h> 31 #include <linux/stddef.h> 32 #include <linux/unistd.h> 33 #include <linux/wait.h> 34 #include <linux/workqueue.h> 35 #include <net/sock.h> 36 #include <net/af_vsock.h> 37 38 #include "vmci_transport_notify.h" 39 40 static int vmci_transport_recv_dgram_cb(void *data, struct vmci_datagram *dg); 41 static int vmci_transport_recv_stream_cb(void *data, struct vmci_datagram *dg); 42 static void vmci_transport_peer_detach_cb(u32 sub_id, 43 const struct vmci_event_data *ed, 44 void *client_data); 45 static void vmci_transport_recv_pkt_work(struct work_struct *work); 46 static void vmci_transport_cleanup(struct work_struct *work); 47 static int vmci_transport_recv_listen(struct sock *sk, 48 struct vmci_transport_packet *pkt); 49 static int vmci_transport_recv_connecting_server( 50 struct sock *sk, 51 struct sock *pending, 52 struct vmci_transport_packet *pkt); 53 static int vmci_transport_recv_connecting_client( 54 struct sock *sk, 55 struct vmci_transport_packet *pkt); 56 static int vmci_transport_recv_connecting_client_negotiate( 57 struct sock *sk, 58 struct vmci_transport_packet *pkt); 59 static int vmci_transport_recv_connecting_client_invalid( 60 struct sock *sk, 61 struct vmci_transport_packet *pkt); 62 static int vmci_transport_recv_connected(struct sock *sk, 63 struct vmci_transport_packet *pkt); 64 static bool vmci_transport_old_proto_override(bool *old_pkt_proto); 65 static u16 vmci_transport_new_proto_supported_versions(void); 66 static bool vmci_transport_proto_to_notify_struct(struct sock *sk, u16 *proto, 67 bool old_pkt_proto); 68 69 struct vmci_transport_recv_pkt_info { 70 struct work_struct work; 71 struct sock *sk; 72 struct vmci_transport_packet pkt; 73 }; 74 75 static LIST_HEAD(vmci_transport_cleanup_list); 76 static DEFINE_SPINLOCK(vmci_transport_cleanup_lock); 77 static DECLARE_WORK(vmci_transport_cleanup_work, vmci_transport_cleanup); 78 79 static struct vmci_handle vmci_transport_stream_handle = { VMCI_INVALID_ID, 80 VMCI_INVALID_ID }; 81 static u32 vmci_transport_qp_resumed_sub_id = VMCI_INVALID_ID; 82 83 static int PROTOCOL_OVERRIDE = -1; 84 85 #define VMCI_TRANSPORT_DEFAULT_QP_SIZE_MIN 128 86 #define VMCI_TRANSPORT_DEFAULT_QP_SIZE 262144 87 #define VMCI_TRANSPORT_DEFAULT_QP_SIZE_MAX 262144 88 89 /* The default peer timeout indicates how long we will wait for a peer response 90 * to a control message. 91 */ 92 #define VSOCK_DEFAULT_CONNECT_TIMEOUT (2 * HZ) 93 94 /* Helper function to convert from a VMCI error code to a VSock error code. */ 95 96 static s32 vmci_transport_error_to_vsock_error(s32 vmci_error) 97 { 98 switch (vmci_error) { 99 case VMCI_ERROR_NO_MEM: 100 return -ENOMEM; 101 case VMCI_ERROR_DUPLICATE_ENTRY: 102 case VMCI_ERROR_ALREADY_EXISTS: 103 return -EADDRINUSE; 104 case VMCI_ERROR_NO_ACCESS: 105 return -EPERM; 106 case VMCI_ERROR_NO_RESOURCES: 107 return -ENOBUFS; 108 case VMCI_ERROR_INVALID_RESOURCE: 109 return -EHOSTUNREACH; 110 case VMCI_ERROR_INVALID_ARGS: 111 default: 112 break; 113 } 114 return -EINVAL; 115 } 116 117 static u32 vmci_transport_peer_rid(u32 peer_cid) 118 { 119 if (VMADDR_CID_HYPERVISOR == peer_cid) 120 return VMCI_TRANSPORT_HYPERVISOR_PACKET_RID; 121 122 return VMCI_TRANSPORT_PACKET_RID; 123 } 124 125 static inline void 126 vmci_transport_packet_init(struct vmci_transport_packet *pkt, 127 struct sockaddr_vm *src, 128 struct sockaddr_vm *dst, 129 u8 type, 130 u64 size, 131 u64 mode, 132 struct vmci_transport_waiting_info *wait, 133 u16 proto, 134 struct vmci_handle handle) 135 { 136 /* We register the stream control handler as an any cid handle so we 137 * must always send from a source address of VMADDR_CID_ANY 138 */ 139 pkt->dg.src = vmci_make_handle(VMADDR_CID_ANY, 140 VMCI_TRANSPORT_PACKET_RID); 141 pkt->dg.dst = vmci_make_handle(dst->svm_cid, 142 vmci_transport_peer_rid(dst->svm_cid)); 143 pkt->dg.payload_size = sizeof(*pkt) - sizeof(pkt->dg); 144 pkt->version = VMCI_TRANSPORT_PACKET_VERSION; 145 pkt->type = type; 146 pkt->src_port = src->svm_port; 147 pkt->dst_port = dst->svm_port; 148 memset(&pkt->proto, 0, sizeof(pkt->proto)); 149 memset(&pkt->_reserved2, 0, sizeof(pkt->_reserved2)); 150 151 switch (pkt->type) { 152 case VMCI_TRANSPORT_PACKET_TYPE_INVALID: 153 pkt->u.size = 0; 154 break; 155 156 case VMCI_TRANSPORT_PACKET_TYPE_REQUEST: 157 case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE: 158 pkt->u.size = size; 159 break; 160 161 case VMCI_TRANSPORT_PACKET_TYPE_OFFER: 162 case VMCI_TRANSPORT_PACKET_TYPE_ATTACH: 163 pkt->u.handle = handle; 164 break; 165 166 case VMCI_TRANSPORT_PACKET_TYPE_WROTE: 167 case VMCI_TRANSPORT_PACKET_TYPE_READ: 168 case VMCI_TRANSPORT_PACKET_TYPE_RST: 169 pkt->u.size = 0; 170 break; 171 172 case VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN: 173 pkt->u.mode = mode; 174 break; 175 176 case VMCI_TRANSPORT_PACKET_TYPE_WAITING_READ: 177 case VMCI_TRANSPORT_PACKET_TYPE_WAITING_WRITE: 178 memcpy(&pkt->u.wait, wait, sizeof(pkt->u.wait)); 179 break; 180 181 case VMCI_TRANSPORT_PACKET_TYPE_REQUEST2: 182 case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2: 183 pkt->u.size = size; 184 pkt->proto = proto; 185 break; 186 } 187 } 188 189 static inline void 190 vmci_transport_packet_get_addresses(struct vmci_transport_packet *pkt, 191 struct sockaddr_vm *local, 192 struct sockaddr_vm *remote) 193 { 194 vsock_addr_init(local, pkt->dg.dst.context, pkt->dst_port); 195 vsock_addr_init(remote, pkt->dg.src.context, pkt->src_port); 196 } 197 198 static int 199 __vmci_transport_send_control_pkt(struct vmci_transport_packet *pkt, 200 struct sockaddr_vm *src, 201 struct sockaddr_vm *dst, 202 enum vmci_transport_packet_type type, 203 u64 size, 204 u64 mode, 205 struct vmci_transport_waiting_info *wait, 206 u16 proto, 207 struct vmci_handle handle, 208 bool convert_error) 209 { 210 int err; 211 212 vmci_transport_packet_init(pkt, src, dst, type, size, mode, wait, 213 proto, handle); 214 err = vmci_datagram_send(&pkt->dg); 215 if (convert_error && (err < 0)) 216 return vmci_transport_error_to_vsock_error(err); 217 218 return err; 219 } 220 221 static int 222 vmci_transport_reply_control_pkt_fast(struct vmci_transport_packet *pkt, 223 enum vmci_transport_packet_type type, 224 u64 size, 225 u64 mode, 226 struct vmci_transport_waiting_info *wait, 227 struct vmci_handle handle) 228 { 229 struct vmci_transport_packet reply; 230 struct sockaddr_vm src, dst; 231 232 if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_RST) { 233 return 0; 234 } else { 235 vmci_transport_packet_get_addresses(pkt, &src, &dst); 236 return __vmci_transport_send_control_pkt(&reply, &src, &dst, 237 type, 238 size, mode, wait, 239 VSOCK_PROTO_INVALID, 240 handle, true); 241 } 242 } 243 244 static int 245 vmci_transport_send_control_pkt_bh(struct sockaddr_vm *src, 246 struct sockaddr_vm *dst, 247 enum vmci_transport_packet_type type, 248 u64 size, 249 u64 mode, 250 struct vmci_transport_waiting_info *wait, 251 struct vmci_handle handle) 252 { 253 /* Note that it is safe to use a single packet across all CPUs since 254 * two tasklets of the same type are guaranteed to not ever run 255 * simultaneously. If that ever changes, or VMCI stops using tasklets, 256 * we can use per-cpu packets. 257 */ 258 static struct vmci_transport_packet pkt; 259 260 return __vmci_transport_send_control_pkt(&pkt, src, dst, type, 261 size, mode, wait, 262 VSOCK_PROTO_INVALID, handle, 263 false); 264 } 265 266 static int 267 vmci_transport_alloc_send_control_pkt(struct sockaddr_vm *src, 268 struct sockaddr_vm *dst, 269 enum vmci_transport_packet_type type, 270 u64 size, 271 u64 mode, 272 struct vmci_transport_waiting_info *wait, 273 u16 proto, 274 struct vmci_handle handle) 275 { 276 struct vmci_transport_packet *pkt; 277 int err; 278 279 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL); 280 if (!pkt) 281 return -ENOMEM; 282 283 err = __vmci_transport_send_control_pkt(pkt, src, dst, type, size, 284 mode, wait, proto, handle, 285 true); 286 kfree(pkt); 287 288 return err; 289 } 290 291 static int 292 vmci_transport_send_control_pkt(struct sock *sk, 293 enum vmci_transport_packet_type type, 294 u64 size, 295 u64 mode, 296 struct vmci_transport_waiting_info *wait, 297 u16 proto, 298 struct vmci_handle handle) 299 { 300 struct vsock_sock *vsk; 301 302 vsk = vsock_sk(sk); 303 304 if (!vsock_addr_bound(&vsk->local_addr)) 305 return -EINVAL; 306 307 if (!vsock_addr_bound(&vsk->remote_addr)) 308 return -EINVAL; 309 310 return vmci_transport_alloc_send_control_pkt(&vsk->local_addr, 311 &vsk->remote_addr, 312 type, size, mode, 313 wait, proto, handle); 314 } 315 316 static int vmci_transport_send_reset_bh(struct sockaddr_vm *dst, 317 struct sockaddr_vm *src, 318 struct vmci_transport_packet *pkt) 319 { 320 if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_RST) 321 return 0; 322 return vmci_transport_send_control_pkt_bh( 323 dst, src, 324 VMCI_TRANSPORT_PACKET_TYPE_RST, 0, 325 0, NULL, VMCI_INVALID_HANDLE); 326 } 327 328 static int vmci_transport_send_reset(struct sock *sk, 329 struct vmci_transport_packet *pkt) 330 { 331 struct sockaddr_vm *dst_ptr; 332 struct sockaddr_vm dst; 333 struct vsock_sock *vsk; 334 335 if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_RST) 336 return 0; 337 338 vsk = vsock_sk(sk); 339 340 if (!vsock_addr_bound(&vsk->local_addr)) 341 return -EINVAL; 342 343 if (vsock_addr_bound(&vsk->remote_addr)) { 344 dst_ptr = &vsk->remote_addr; 345 } else { 346 vsock_addr_init(&dst, pkt->dg.src.context, 347 pkt->src_port); 348 dst_ptr = &dst; 349 } 350 return vmci_transport_alloc_send_control_pkt(&vsk->local_addr, dst_ptr, 351 VMCI_TRANSPORT_PACKET_TYPE_RST, 352 0, 0, NULL, VSOCK_PROTO_INVALID, 353 VMCI_INVALID_HANDLE); 354 } 355 356 static int vmci_transport_send_negotiate(struct sock *sk, size_t size) 357 { 358 return vmci_transport_send_control_pkt( 359 sk, 360 VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE, 361 size, 0, NULL, 362 VSOCK_PROTO_INVALID, 363 VMCI_INVALID_HANDLE); 364 } 365 366 static int vmci_transport_send_negotiate2(struct sock *sk, size_t size, 367 u16 version) 368 { 369 return vmci_transport_send_control_pkt( 370 sk, 371 VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2, 372 size, 0, NULL, version, 373 VMCI_INVALID_HANDLE); 374 } 375 376 static int vmci_transport_send_qp_offer(struct sock *sk, 377 struct vmci_handle handle) 378 { 379 return vmci_transport_send_control_pkt( 380 sk, VMCI_TRANSPORT_PACKET_TYPE_OFFER, 0, 381 0, NULL, 382 VSOCK_PROTO_INVALID, handle); 383 } 384 385 static int vmci_transport_send_attach(struct sock *sk, 386 struct vmci_handle handle) 387 { 388 return vmci_transport_send_control_pkt( 389 sk, VMCI_TRANSPORT_PACKET_TYPE_ATTACH, 390 0, 0, NULL, VSOCK_PROTO_INVALID, 391 handle); 392 } 393 394 static int vmci_transport_reply_reset(struct vmci_transport_packet *pkt) 395 { 396 return vmci_transport_reply_control_pkt_fast( 397 pkt, 398 VMCI_TRANSPORT_PACKET_TYPE_RST, 399 0, 0, NULL, 400 VMCI_INVALID_HANDLE); 401 } 402 403 static int vmci_transport_send_invalid_bh(struct sockaddr_vm *dst, 404 struct sockaddr_vm *src) 405 { 406 return vmci_transport_send_control_pkt_bh( 407 dst, src, 408 VMCI_TRANSPORT_PACKET_TYPE_INVALID, 409 0, 0, NULL, VMCI_INVALID_HANDLE); 410 } 411 412 int vmci_transport_send_wrote_bh(struct sockaddr_vm *dst, 413 struct sockaddr_vm *src) 414 { 415 return vmci_transport_send_control_pkt_bh( 416 dst, src, 417 VMCI_TRANSPORT_PACKET_TYPE_WROTE, 0, 418 0, NULL, VMCI_INVALID_HANDLE); 419 } 420 421 int vmci_transport_send_read_bh(struct sockaddr_vm *dst, 422 struct sockaddr_vm *src) 423 { 424 return vmci_transport_send_control_pkt_bh( 425 dst, src, 426 VMCI_TRANSPORT_PACKET_TYPE_READ, 0, 427 0, NULL, VMCI_INVALID_HANDLE); 428 } 429 430 int vmci_transport_send_wrote(struct sock *sk) 431 { 432 return vmci_transport_send_control_pkt( 433 sk, VMCI_TRANSPORT_PACKET_TYPE_WROTE, 0, 434 0, NULL, VSOCK_PROTO_INVALID, 435 VMCI_INVALID_HANDLE); 436 } 437 438 int vmci_transport_send_read(struct sock *sk) 439 { 440 return vmci_transport_send_control_pkt( 441 sk, VMCI_TRANSPORT_PACKET_TYPE_READ, 0, 442 0, NULL, VSOCK_PROTO_INVALID, 443 VMCI_INVALID_HANDLE); 444 } 445 446 int vmci_transport_send_waiting_write(struct sock *sk, 447 struct vmci_transport_waiting_info *wait) 448 { 449 return vmci_transport_send_control_pkt( 450 sk, VMCI_TRANSPORT_PACKET_TYPE_WAITING_WRITE, 451 0, 0, wait, VSOCK_PROTO_INVALID, 452 VMCI_INVALID_HANDLE); 453 } 454 455 int vmci_transport_send_waiting_read(struct sock *sk, 456 struct vmci_transport_waiting_info *wait) 457 { 458 return vmci_transport_send_control_pkt( 459 sk, VMCI_TRANSPORT_PACKET_TYPE_WAITING_READ, 460 0, 0, wait, VSOCK_PROTO_INVALID, 461 VMCI_INVALID_HANDLE); 462 } 463 464 static int vmci_transport_shutdown(struct vsock_sock *vsk, int mode) 465 { 466 return vmci_transport_send_control_pkt( 467 &vsk->sk, 468 VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN, 469 0, mode, NULL, 470 VSOCK_PROTO_INVALID, 471 VMCI_INVALID_HANDLE); 472 } 473 474 static int vmci_transport_send_conn_request(struct sock *sk, size_t size) 475 { 476 return vmci_transport_send_control_pkt(sk, 477 VMCI_TRANSPORT_PACKET_TYPE_REQUEST, 478 size, 0, NULL, 479 VSOCK_PROTO_INVALID, 480 VMCI_INVALID_HANDLE); 481 } 482 483 static int vmci_transport_send_conn_request2(struct sock *sk, size_t size, 484 u16 version) 485 { 486 return vmci_transport_send_control_pkt( 487 sk, VMCI_TRANSPORT_PACKET_TYPE_REQUEST2, 488 size, 0, NULL, version, 489 VMCI_INVALID_HANDLE); 490 } 491 492 static struct sock *vmci_transport_get_pending( 493 struct sock *listener, 494 struct vmci_transport_packet *pkt) 495 { 496 struct vsock_sock *vlistener; 497 struct vsock_sock *vpending; 498 struct sock *pending; 499 struct sockaddr_vm src; 500 501 vsock_addr_init(&src, pkt->dg.src.context, pkt->src_port); 502 503 vlistener = vsock_sk(listener); 504 505 list_for_each_entry(vpending, &vlistener->pending_links, 506 pending_links) { 507 if (vsock_addr_equals_addr(&src, &vpending->remote_addr) && 508 pkt->dst_port == vpending->local_addr.svm_port) { 509 pending = sk_vsock(vpending); 510 sock_hold(pending); 511 goto found; 512 } 513 } 514 515 pending = NULL; 516 found: 517 return pending; 518 519 } 520 521 static void vmci_transport_release_pending(struct sock *pending) 522 { 523 sock_put(pending); 524 } 525 526 /* We allow two kinds of sockets to communicate with a restricted VM: 1) 527 * trusted sockets 2) sockets from applications running as the same user as the 528 * VM (this is only true for the host side and only when using hosted products) 529 */ 530 531 static bool vmci_transport_is_trusted(struct vsock_sock *vsock, u32 peer_cid) 532 { 533 return vsock->trusted || 534 vmci_is_context_owner(peer_cid, vsock->owner->uid); 535 } 536 537 /* We allow sending datagrams to and receiving datagrams from a restricted VM 538 * only if it is trusted as described in vmci_transport_is_trusted. 539 */ 540 541 static bool vmci_transport_allow_dgram(struct vsock_sock *vsock, u32 peer_cid) 542 { 543 if (VMADDR_CID_HYPERVISOR == peer_cid) 544 return true; 545 546 if (vsock->cached_peer != peer_cid) { 547 vsock->cached_peer = peer_cid; 548 if (!vmci_transport_is_trusted(vsock, peer_cid) && 549 (vmci_context_get_priv_flags(peer_cid) & 550 VMCI_PRIVILEGE_FLAG_RESTRICTED)) { 551 vsock->cached_peer_allow_dgram = false; 552 } else { 553 vsock->cached_peer_allow_dgram = true; 554 } 555 } 556 557 return vsock->cached_peer_allow_dgram; 558 } 559 560 static int 561 vmci_transport_queue_pair_alloc(struct vmci_qp **qpair, 562 struct vmci_handle *handle, 563 u64 produce_size, 564 u64 consume_size, 565 u32 peer, u32 flags, bool trusted) 566 { 567 int err = 0; 568 569 if (trusted) { 570 /* Try to allocate our queue pair as trusted. This will only 571 * work if vsock is running in the host. 572 */ 573 574 err = vmci_qpair_alloc(qpair, handle, produce_size, 575 consume_size, 576 peer, flags, 577 VMCI_PRIVILEGE_FLAG_TRUSTED); 578 if (err != VMCI_ERROR_NO_ACCESS) 579 goto out; 580 581 } 582 583 err = vmci_qpair_alloc(qpair, handle, produce_size, consume_size, 584 peer, flags, VMCI_NO_PRIVILEGE_FLAGS); 585 out: 586 if (err < 0) { 587 pr_err("Could not attach to queue pair with %d\n", 588 err); 589 err = vmci_transport_error_to_vsock_error(err); 590 } 591 592 return err; 593 } 594 595 static int 596 vmci_transport_datagram_create_hnd(u32 resource_id, 597 u32 flags, 598 vmci_datagram_recv_cb recv_cb, 599 void *client_data, 600 struct vmci_handle *out_handle) 601 { 602 int err = 0; 603 604 /* Try to allocate our datagram handler as trusted. This will only work 605 * if vsock is running in the host. 606 */ 607 608 err = vmci_datagram_create_handle_priv(resource_id, flags, 609 VMCI_PRIVILEGE_FLAG_TRUSTED, 610 recv_cb, 611 client_data, out_handle); 612 613 if (err == VMCI_ERROR_NO_ACCESS) 614 err = vmci_datagram_create_handle(resource_id, flags, 615 recv_cb, client_data, 616 out_handle); 617 618 return err; 619 } 620 621 /* This is invoked as part of a tasklet that's scheduled when the VMCI 622 * interrupt fires. This is run in bottom-half context and if it ever needs to 623 * sleep it should defer that work to a work queue. 624 */ 625 626 static int vmci_transport_recv_dgram_cb(void *data, struct vmci_datagram *dg) 627 { 628 struct sock *sk; 629 size_t size; 630 struct sk_buff *skb; 631 struct vsock_sock *vsk; 632 633 sk = (struct sock *)data; 634 635 /* This handler is privileged when this module is running on the host. 636 * We will get datagrams from all endpoints (even VMs that are in a 637 * restricted context). If we get one from a restricted context then 638 * the destination socket must be trusted. 639 * 640 * NOTE: We access the socket struct without holding the lock here. 641 * This is ok because the field we are interested is never modified 642 * outside of the create and destruct socket functions. 643 */ 644 vsk = vsock_sk(sk); 645 if (!vmci_transport_allow_dgram(vsk, dg->src.context)) 646 return VMCI_ERROR_NO_ACCESS; 647 648 size = VMCI_DG_SIZE(dg); 649 650 /* Attach the packet to the socket's receive queue as an sk_buff. */ 651 skb = alloc_skb(size, GFP_ATOMIC); 652 if (!skb) 653 return VMCI_ERROR_NO_MEM; 654 655 /* sk_receive_skb() will do a sock_put(), so hold here. */ 656 sock_hold(sk); 657 skb_put(skb, size); 658 memcpy(skb->data, dg, size); 659 sk_receive_skb(sk, skb, 0); 660 661 return VMCI_SUCCESS; 662 } 663 664 static bool vmci_transport_stream_allow(u32 cid, u32 port) 665 { 666 static const u32 non_socket_contexts[] = { 667 VMADDR_CID_RESERVED, 668 }; 669 int i; 670 671 BUILD_BUG_ON(sizeof(cid) != sizeof(*non_socket_contexts)); 672 673 for (i = 0; i < ARRAY_SIZE(non_socket_contexts); i++) { 674 if (cid == non_socket_contexts[i]) 675 return false; 676 } 677 678 return true; 679 } 680 681 /* This is invoked as part of a tasklet that's scheduled when the VMCI 682 * interrupt fires. This is run in bottom-half context but it defers most of 683 * its work to the packet handling work queue. 684 */ 685 686 static int vmci_transport_recv_stream_cb(void *data, struct vmci_datagram *dg) 687 { 688 struct sock *sk; 689 struct sockaddr_vm dst; 690 struct sockaddr_vm src; 691 struct vmci_transport_packet *pkt; 692 struct vsock_sock *vsk; 693 bool bh_process_pkt; 694 int err; 695 696 sk = NULL; 697 err = VMCI_SUCCESS; 698 bh_process_pkt = false; 699 700 /* Ignore incoming packets from contexts without sockets, or resources 701 * that aren't vsock implementations. 702 */ 703 704 if (!vmci_transport_stream_allow(dg->src.context, -1) 705 || vmci_transport_peer_rid(dg->src.context) != dg->src.resource) 706 return VMCI_ERROR_NO_ACCESS; 707 708 if (VMCI_DG_SIZE(dg) < sizeof(*pkt)) 709 /* Drop datagrams that do not contain full VSock packets. */ 710 return VMCI_ERROR_INVALID_ARGS; 711 712 pkt = (struct vmci_transport_packet *)dg; 713 714 /* Find the socket that should handle this packet. First we look for a 715 * connected socket and if there is none we look for a socket bound to 716 * the destintation address. 717 */ 718 vsock_addr_init(&src, pkt->dg.src.context, pkt->src_port); 719 vsock_addr_init(&dst, pkt->dg.dst.context, pkt->dst_port); 720 721 sk = vsock_find_connected_socket(&src, &dst); 722 if (!sk) { 723 sk = vsock_find_bound_socket(&dst); 724 if (!sk) { 725 /* We could not find a socket for this specified 726 * address. If this packet is a RST, we just drop it. 727 * If it is another packet, we send a RST. Note that 728 * we do not send a RST reply to RSTs so that we do not 729 * continually send RSTs between two endpoints. 730 * 731 * Note that since this is a reply, dst is src and src 732 * is dst. 733 */ 734 if (vmci_transport_send_reset_bh(&dst, &src, pkt) < 0) 735 pr_err("unable to send reset\n"); 736 737 err = VMCI_ERROR_NOT_FOUND; 738 goto out; 739 } 740 } 741 742 /* If the received packet type is beyond all types known to this 743 * implementation, reply with an invalid message. Hopefully this will 744 * help when implementing backwards compatibility in the future. 745 */ 746 if (pkt->type >= VMCI_TRANSPORT_PACKET_TYPE_MAX) { 747 vmci_transport_send_invalid_bh(&dst, &src); 748 err = VMCI_ERROR_INVALID_ARGS; 749 goto out; 750 } 751 752 /* This handler is privileged when this module is running on the host. 753 * We will get datagram connect requests from all endpoints (even VMs 754 * that are in a restricted context). If we get one from a restricted 755 * context then the destination socket must be trusted. 756 * 757 * NOTE: We access the socket struct without holding the lock here. 758 * This is ok because the field we are interested is never modified 759 * outside of the create and destruct socket functions. 760 */ 761 vsk = vsock_sk(sk); 762 if (!vmci_transport_allow_dgram(vsk, pkt->dg.src.context)) { 763 err = VMCI_ERROR_NO_ACCESS; 764 goto out; 765 } 766 767 /* We do most everything in a work queue, but let's fast path the 768 * notification of reads and writes to help data transfer performance. 769 * We can only do this if there is no process context code executing 770 * for this socket since that may change the state. 771 */ 772 bh_lock_sock(sk); 773 774 if (!sock_owned_by_user(sk)) { 775 /* The local context ID may be out of date, update it. */ 776 vsk->local_addr.svm_cid = dst.svm_cid; 777 778 if (sk->sk_state == TCP_ESTABLISHED) 779 vmci_trans(vsk)->notify_ops->handle_notify_pkt( 780 sk, pkt, true, &dst, &src, 781 &bh_process_pkt); 782 } 783 784 bh_unlock_sock(sk); 785 786 if (!bh_process_pkt) { 787 struct vmci_transport_recv_pkt_info *recv_pkt_info; 788 789 recv_pkt_info = kmalloc(sizeof(*recv_pkt_info), GFP_ATOMIC); 790 if (!recv_pkt_info) { 791 if (vmci_transport_send_reset_bh(&dst, &src, pkt) < 0) 792 pr_err("unable to send reset\n"); 793 794 err = VMCI_ERROR_NO_MEM; 795 goto out; 796 } 797 798 recv_pkt_info->sk = sk; 799 memcpy(&recv_pkt_info->pkt, pkt, sizeof(recv_pkt_info->pkt)); 800 INIT_WORK(&recv_pkt_info->work, vmci_transport_recv_pkt_work); 801 802 schedule_work(&recv_pkt_info->work); 803 /* Clear sk so that the reference count incremented by one of 804 * the Find functions above is not decremented below. We need 805 * that reference count for the packet handler we've scheduled 806 * to run. 807 */ 808 sk = NULL; 809 } 810 811 out: 812 if (sk) 813 sock_put(sk); 814 815 return err; 816 } 817 818 static void vmci_transport_handle_detach(struct sock *sk) 819 { 820 struct vsock_sock *vsk; 821 822 vsk = vsock_sk(sk); 823 if (!vmci_handle_is_invalid(vmci_trans(vsk)->qp_handle)) { 824 sock_set_flag(sk, SOCK_DONE); 825 826 /* On a detach the peer will not be sending or receiving 827 * anymore. 828 */ 829 vsk->peer_shutdown = SHUTDOWN_MASK; 830 831 /* We should not be sending anymore since the peer won't be 832 * there to receive, but we can still receive if there is data 833 * left in our consume queue. If the local endpoint is a host, 834 * we can't call vsock_stream_has_data, since that may block, 835 * but a host endpoint can't read data once the VM has 836 * detached, so there is no available data in that case. 837 */ 838 if (vsk->local_addr.svm_cid == VMADDR_CID_HOST || 839 vsock_stream_has_data(vsk) <= 0) { 840 if (sk->sk_state == TCP_SYN_SENT) { 841 /* The peer may detach from a queue pair while 842 * we are still in the connecting state, i.e., 843 * if the peer VM is killed after attaching to 844 * a queue pair, but before we complete the 845 * handshake. In that case, we treat the detach 846 * event like a reset. 847 */ 848 849 sk->sk_state = TCP_CLOSE; 850 sk->sk_err = ECONNRESET; 851 sk->sk_error_report(sk); 852 return; 853 } 854 sk->sk_state = TCP_CLOSE; 855 } 856 sk->sk_state_change(sk); 857 } 858 } 859 860 static void vmci_transport_peer_detach_cb(u32 sub_id, 861 const struct vmci_event_data *e_data, 862 void *client_data) 863 { 864 struct vmci_transport *trans = client_data; 865 const struct vmci_event_payload_qp *e_payload; 866 867 e_payload = vmci_event_data_const_payload(e_data); 868 869 /* XXX This is lame, we should provide a way to lookup sockets by 870 * qp_handle. 871 */ 872 if (vmci_handle_is_invalid(e_payload->handle) || 873 !vmci_handle_is_equal(trans->qp_handle, e_payload->handle)) 874 return; 875 876 /* We don't ask for delayed CBs when we subscribe to this event (we 877 * pass 0 as flags to vmci_event_subscribe()). VMCI makes no 878 * guarantees in that case about what context we might be running in, 879 * so it could be BH or process, blockable or non-blockable. So we 880 * need to account for all possible contexts here. 881 */ 882 spin_lock_bh(&trans->lock); 883 if (!trans->sk) 884 goto out; 885 886 /* Apart from here, trans->lock is only grabbed as part of sk destruct, 887 * where trans->sk isn't locked. 888 */ 889 bh_lock_sock(trans->sk); 890 891 vmci_transport_handle_detach(trans->sk); 892 893 bh_unlock_sock(trans->sk); 894 out: 895 spin_unlock_bh(&trans->lock); 896 } 897 898 static void vmci_transport_qp_resumed_cb(u32 sub_id, 899 const struct vmci_event_data *e_data, 900 void *client_data) 901 { 902 vsock_for_each_connected_socket(vmci_transport_handle_detach); 903 } 904 905 static void vmci_transport_recv_pkt_work(struct work_struct *work) 906 { 907 struct vmci_transport_recv_pkt_info *recv_pkt_info; 908 struct vmci_transport_packet *pkt; 909 struct sock *sk; 910 911 recv_pkt_info = 912 container_of(work, struct vmci_transport_recv_pkt_info, work); 913 sk = recv_pkt_info->sk; 914 pkt = &recv_pkt_info->pkt; 915 916 lock_sock(sk); 917 918 /* The local context ID may be out of date. */ 919 vsock_sk(sk)->local_addr.svm_cid = pkt->dg.dst.context; 920 921 switch (sk->sk_state) { 922 case TCP_LISTEN: 923 vmci_transport_recv_listen(sk, pkt); 924 break; 925 case TCP_SYN_SENT: 926 /* Processing of pending connections for servers goes through 927 * the listening socket, so see vmci_transport_recv_listen() 928 * for that path. 929 */ 930 vmci_transport_recv_connecting_client(sk, pkt); 931 break; 932 case TCP_ESTABLISHED: 933 vmci_transport_recv_connected(sk, pkt); 934 break; 935 default: 936 /* Because this function does not run in the same context as 937 * vmci_transport_recv_stream_cb it is possible that the 938 * socket has closed. We need to let the other side know or it 939 * could be sitting in a connect and hang forever. Send a 940 * reset to prevent that. 941 */ 942 vmci_transport_send_reset(sk, pkt); 943 break; 944 } 945 946 release_sock(sk); 947 kfree(recv_pkt_info); 948 /* Release reference obtained in the stream callback when we fetched 949 * this socket out of the bound or connected list. 950 */ 951 sock_put(sk); 952 } 953 954 static int vmci_transport_recv_listen(struct sock *sk, 955 struct vmci_transport_packet *pkt) 956 { 957 struct sock *pending; 958 struct vsock_sock *vpending; 959 int err; 960 u64 qp_size; 961 bool old_request = false; 962 bool old_pkt_proto = false; 963 964 err = 0; 965 966 /* Because we are in the listen state, we could be receiving a packet 967 * for ourself or any previous connection requests that we received. 968 * If it's the latter, we try to find a socket in our list of pending 969 * connections and, if we do, call the appropriate handler for the 970 * state that that socket is in. Otherwise we try to service the 971 * connection request. 972 */ 973 pending = vmci_transport_get_pending(sk, pkt); 974 if (pending) { 975 lock_sock(pending); 976 977 /* The local context ID may be out of date. */ 978 vsock_sk(pending)->local_addr.svm_cid = pkt->dg.dst.context; 979 980 switch (pending->sk_state) { 981 case TCP_SYN_SENT: 982 err = vmci_transport_recv_connecting_server(sk, 983 pending, 984 pkt); 985 break; 986 default: 987 vmci_transport_send_reset(pending, pkt); 988 err = -EINVAL; 989 } 990 991 if (err < 0) 992 vsock_remove_pending(sk, pending); 993 994 release_sock(pending); 995 vmci_transport_release_pending(pending); 996 997 return err; 998 } 999 1000 /* The listen state only accepts connection requests. Reply with a 1001 * reset unless we received a reset. 1002 */ 1003 1004 if (!(pkt->type == VMCI_TRANSPORT_PACKET_TYPE_REQUEST || 1005 pkt->type == VMCI_TRANSPORT_PACKET_TYPE_REQUEST2)) { 1006 vmci_transport_reply_reset(pkt); 1007 return -EINVAL; 1008 } 1009 1010 if (pkt->u.size == 0) { 1011 vmci_transport_reply_reset(pkt); 1012 return -EINVAL; 1013 } 1014 1015 /* If this socket can't accommodate this connection request, we send a 1016 * reset. Otherwise we create and initialize a child socket and reply 1017 * with a connection negotiation. 1018 */ 1019 if (sk->sk_ack_backlog >= sk->sk_max_ack_backlog) { 1020 vmci_transport_reply_reset(pkt); 1021 return -ECONNREFUSED; 1022 } 1023 1024 pending = __vsock_create(sock_net(sk), NULL, sk, GFP_KERNEL, 1025 sk->sk_type, 0); 1026 if (!pending) { 1027 vmci_transport_send_reset(sk, pkt); 1028 return -ENOMEM; 1029 } 1030 1031 vpending = vsock_sk(pending); 1032 1033 vsock_addr_init(&vpending->local_addr, pkt->dg.dst.context, 1034 pkt->dst_port); 1035 vsock_addr_init(&vpending->remote_addr, pkt->dg.src.context, 1036 pkt->src_port); 1037 1038 /* If the proposed size fits within our min/max, accept it. Otherwise 1039 * propose our own size. 1040 */ 1041 if (pkt->u.size >= vmci_trans(vpending)->queue_pair_min_size && 1042 pkt->u.size <= vmci_trans(vpending)->queue_pair_max_size) { 1043 qp_size = pkt->u.size; 1044 } else { 1045 qp_size = vmci_trans(vpending)->queue_pair_size; 1046 } 1047 1048 /* Figure out if we are using old or new requests based on the 1049 * overrides pkt types sent by our peer. 1050 */ 1051 if (vmci_transport_old_proto_override(&old_pkt_proto)) { 1052 old_request = old_pkt_proto; 1053 } else { 1054 if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_REQUEST) 1055 old_request = true; 1056 else if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_REQUEST2) 1057 old_request = false; 1058 1059 } 1060 1061 if (old_request) { 1062 /* Handle a REQUEST (or override) */ 1063 u16 version = VSOCK_PROTO_INVALID; 1064 if (vmci_transport_proto_to_notify_struct( 1065 pending, &version, true)) 1066 err = vmci_transport_send_negotiate(pending, qp_size); 1067 else 1068 err = -EINVAL; 1069 1070 } else { 1071 /* Handle a REQUEST2 (or override) */ 1072 int proto_int = pkt->proto; 1073 int pos; 1074 u16 active_proto_version = 0; 1075 1076 /* The list of possible protocols is the intersection of all 1077 * protocols the client supports ... plus all the protocols we 1078 * support. 1079 */ 1080 proto_int &= vmci_transport_new_proto_supported_versions(); 1081 1082 /* We choose the highest possible protocol version and use that 1083 * one. 1084 */ 1085 pos = fls(proto_int); 1086 if (pos) { 1087 active_proto_version = (1 << (pos - 1)); 1088 if (vmci_transport_proto_to_notify_struct( 1089 pending, &active_proto_version, false)) 1090 err = vmci_transport_send_negotiate2(pending, 1091 qp_size, 1092 active_proto_version); 1093 else 1094 err = -EINVAL; 1095 1096 } else { 1097 err = -EINVAL; 1098 } 1099 } 1100 1101 if (err < 0) { 1102 vmci_transport_send_reset(sk, pkt); 1103 sock_put(pending); 1104 err = vmci_transport_error_to_vsock_error(err); 1105 goto out; 1106 } 1107 1108 vsock_add_pending(sk, pending); 1109 sk->sk_ack_backlog++; 1110 1111 pending->sk_state = TCP_SYN_SENT; 1112 vmci_trans(vpending)->produce_size = 1113 vmci_trans(vpending)->consume_size = qp_size; 1114 vmci_trans(vpending)->queue_pair_size = qp_size; 1115 1116 vmci_trans(vpending)->notify_ops->process_request(pending); 1117 1118 /* We might never receive another message for this socket and it's not 1119 * connected to any process, so we have to ensure it gets cleaned up 1120 * ourself. Our delayed work function will take care of that. Note 1121 * that we do not ever cancel this function since we have few 1122 * guarantees about its state when calling cancel_delayed_work(). 1123 * Instead we hold a reference on the socket for that function and make 1124 * it capable of handling cases where it needs to do nothing but 1125 * release that reference. 1126 */ 1127 vpending->listener = sk; 1128 sock_hold(sk); 1129 sock_hold(pending); 1130 schedule_delayed_work(&vpending->pending_work, HZ); 1131 1132 out: 1133 return err; 1134 } 1135 1136 static int 1137 vmci_transport_recv_connecting_server(struct sock *listener, 1138 struct sock *pending, 1139 struct vmci_transport_packet *pkt) 1140 { 1141 struct vsock_sock *vpending; 1142 struct vmci_handle handle; 1143 struct vmci_qp *qpair; 1144 bool is_local; 1145 u32 flags; 1146 u32 detach_sub_id; 1147 int err; 1148 int skerr; 1149 1150 vpending = vsock_sk(pending); 1151 detach_sub_id = VMCI_INVALID_ID; 1152 1153 switch (pkt->type) { 1154 case VMCI_TRANSPORT_PACKET_TYPE_OFFER: 1155 if (vmci_handle_is_invalid(pkt->u.handle)) { 1156 vmci_transport_send_reset(pending, pkt); 1157 skerr = EPROTO; 1158 err = -EINVAL; 1159 goto destroy; 1160 } 1161 break; 1162 default: 1163 /* Close and cleanup the connection. */ 1164 vmci_transport_send_reset(pending, pkt); 1165 skerr = EPROTO; 1166 err = pkt->type == VMCI_TRANSPORT_PACKET_TYPE_RST ? 0 : -EINVAL; 1167 goto destroy; 1168 } 1169 1170 /* In order to complete the connection we need to attach to the offered 1171 * queue pair and send an attach notification. We also subscribe to the 1172 * detach event so we know when our peer goes away, and we do that 1173 * before attaching so we don't miss an event. If all this succeeds, 1174 * we update our state and wakeup anything waiting in accept() for a 1175 * connection. 1176 */ 1177 1178 /* We don't care about attach since we ensure the other side has 1179 * attached by specifying the ATTACH_ONLY flag below. 1180 */ 1181 err = vmci_event_subscribe(VMCI_EVENT_QP_PEER_DETACH, 1182 vmci_transport_peer_detach_cb, 1183 vmci_trans(vpending), &detach_sub_id); 1184 if (err < VMCI_SUCCESS) { 1185 vmci_transport_send_reset(pending, pkt); 1186 err = vmci_transport_error_to_vsock_error(err); 1187 skerr = -err; 1188 goto destroy; 1189 } 1190 1191 vmci_trans(vpending)->detach_sub_id = detach_sub_id; 1192 1193 /* Now attach to the queue pair the client created. */ 1194 handle = pkt->u.handle; 1195 1196 /* vpending->local_addr always has a context id so we do not need to 1197 * worry about VMADDR_CID_ANY in this case. 1198 */ 1199 is_local = 1200 vpending->remote_addr.svm_cid == vpending->local_addr.svm_cid; 1201 flags = VMCI_QPFLAG_ATTACH_ONLY; 1202 flags |= is_local ? VMCI_QPFLAG_LOCAL : 0; 1203 1204 err = vmci_transport_queue_pair_alloc( 1205 &qpair, 1206 &handle, 1207 vmci_trans(vpending)->produce_size, 1208 vmci_trans(vpending)->consume_size, 1209 pkt->dg.src.context, 1210 flags, 1211 vmci_transport_is_trusted( 1212 vpending, 1213 vpending->remote_addr.svm_cid)); 1214 if (err < 0) { 1215 vmci_transport_send_reset(pending, pkt); 1216 skerr = -err; 1217 goto destroy; 1218 } 1219 1220 vmci_trans(vpending)->qp_handle = handle; 1221 vmci_trans(vpending)->qpair = qpair; 1222 1223 /* When we send the attach message, we must be ready to handle incoming 1224 * control messages on the newly connected socket. So we move the 1225 * pending socket to the connected state before sending the attach 1226 * message. Otherwise, an incoming packet triggered by the attach being 1227 * received by the peer may be processed concurrently with what happens 1228 * below after sending the attach message, and that incoming packet 1229 * will find the listening socket instead of the (currently) pending 1230 * socket. Note that enqueueing the socket increments the reference 1231 * count, so even if a reset comes before the connection is accepted, 1232 * the socket will be valid until it is removed from the queue. 1233 * 1234 * If we fail sending the attach below, we remove the socket from the 1235 * connected list and move the socket to TCP_CLOSE before 1236 * releasing the lock, so a pending slow path processing of an incoming 1237 * packet will not see the socket in the connected state in that case. 1238 */ 1239 pending->sk_state = TCP_ESTABLISHED; 1240 1241 vsock_insert_connected(vpending); 1242 1243 /* Notify our peer of our attach. */ 1244 err = vmci_transport_send_attach(pending, handle); 1245 if (err < 0) { 1246 vsock_remove_connected(vpending); 1247 pr_err("Could not send attach\n"); 1248 vmci_transport_send_reset(pending, pkt); 1249 err = vmci_transport_error_to_vsock_error(err); 1250 skerr = -err; 1251 goto destroy; 1252 } 1253 1254 /* We have a connection. Move the now connected socket from the 1255 * listener's pending list to the accept queue so callers of accept() 1256 * can find it. 1257 */ 1258 vsock_remove_pending(listener, pending); 1259 vsock_enqueue_accept(listener, pending); 1260 1261 /* Callers of accept() will be be waiting on the listening socket, not 1262 * the pending socket. 1263 */ 1264 listener->sk_data_ready(listener); 1265 1266 return 0; 1267 1268 destroy: 1269 pending->sk_err = skerr; 1270 pending->sk_state = TCP_CLOSE; 1271 /* As long as we drop our reference, all necessary cleanup will handle 1272 * when the cleanup function drops its reference and our destruct 1273 * implementation is called. Note that since the listen handler will 1274 * remove pending from the pending list upon our failure, the cleanup 1275 * function won't drop the additional reference, which is why we do it 1276 * here. 1277 */ 1278 sock_put(pending); 1279 1280 return err; 1281 } 1282 1283 static int 1284 vmci_transport_recv_connecting_client(struct sock *sk, 1285 struct vmci_transport_packet *pkt) 1286 { 1287 struct vsock_sock *vsk; 1288 int err; 1289 int skerr; 1290 1291 vsk = vsock_sk(sk); 1292 1293 switch (pkt->type) { 1294 case VMCI_TRANSPORT_PACKET_TYPE_ATTACH: 1295 if (vmci_handle_is_invalid(pkt->u.handle) || 1296 !vmci_handle_is_equal(pkt->u.handle, 1297 vmci_trans(vsk)->qp_handle)) { 1298 skerr = EPROTO; 1299 err = -EINVAL; 1300 goto destroy; 1301 } 1302 1303 /* Signify the socket is connected and wakeup the waiter in 1304 * connect(). Also place the socket in the connected table for 1305 * accounting (it can already be found since it's in the bound 1306 * table). 1307 */ 1308 sk->sk_state = TCP_ESTABLISHED; 1309 sk->sk_socket->state = SS_CONNECTED; 1310 vsock_insert_connected(vsk); 1311 sk->sk_state_change(sk); 1312 1313 break; 1314 case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE: 1315 case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2: 1316 if (pkt->u.size == 0 1317 || pkt->dg.src.context != vsk->remote_addr.svm_cid 1318 || pkt->src_port != vsk->remote_addr.svm_port 1319 || !vmci_handle_is_invalid(vmci_trans(vsk)->qp_handle) 1320 || vmci_trans(vsk)->qpair 1321 || vmci_trans(vsk)->produce_size != 0 1322 || vmci_trans(vsk)->consume_size != 0 1323 || vmci_trans(vsk)->detach_sub_id != VMCI_INVALID_ID) { 1324 skerr = EPROTO; 1325 err = -EINVAL; 1326 1327 goto destroy; 1328 } 1329 1330 err = vmci_transport_recv_connecting_client_negotiate(sk, pkt); 1331 if (err) { 1332 skerr = -err; 1333 goto destroy; 1334 } 1335 1336 break; 1337 case VMCI_TRANSPORT_PACKET_TYPE_INVALID: 1338 err = vmci_transport_recv_connecting_client_invalid(sk, pkt); 1339 if (err) { 1340 skerr = -err; 1341 goto destroy; 1342 } 1343 1344 break; 1345 case VMCI_TRANSPORT_PACKET_TYPE_RST: 1346 /* Older versions of the linux code (WS 6.5 / ESX 4.0) used to 1347 * continue processing here after they sent an INVALID packet. 1348 * This meant that we got a RST after the INVALID. We ignore a 1349 * RST after an INVALID. The common code doesn't send the RST 1350 * ... so we can hang if an old version of the common code 1351 * fails between getting a REQUEST and sending an OFFER back. 1352 * Not much we can do about it... except hope that it doesn't 1353 * happen. 1354 */ 1355 if (vsk->ignore_connecting_rst) { 1356 vsk->ignore_connecting_rst = false; 1357 } else { 1358 skerr = ECONNRESET; 1359 err = 0; 1360 goto destroy; 1361 } 1362 1363 break; 1364 default: 1365 /* Close and cleanup the connection. */ 1366 skerr = EPROTO; 1367 err = -EINVAL; 1368 goto destroy; 1369 } 1370 1371 return 0; 1372 1373 destroy: 1374 vmci_transport_send_reset(sk, pkt); 1375 1376 sk->sk_state = TCP_CLOSE; 1377 sk->sk_err = skerr; 1378 sk->sk_error_report(sk); 1379 return err; 1380 } 1381 1382 static int vmci_transport_recv_connecting_client_negotiate( 1383 struct sock *sk, 1384 struct vmci_transport_packet *pkt) 1385 { 1386 int err; 1387 struct vsock_sock *vsk; 1388 struct vmci_handle handle; 1389 struct vmci_qp *qpair; 1390 u32 detach_sub_id; 1391 bool is_local; 1392 u32 flags; 1393 bool old_proto = true; 1394 bool old_pkt_proto; 1395 u16 version; 1396 1397 vsk = vsock_sk(sk); 1398 handle = VMCI_INVALID_HANDLE; 1399 detach_sub_id = VMCI_INVALID_ID; 1400 1401 /* If we have gotten here then we should be past the point where old 1402 * linux vsock could have sent the bogus rst. 1403 */ 1404 vsk->sent_request = false; 1405 vsk->ignore_connecting_rst = false; 1406 1407 /* Verify that we're OK with the proposed queue pair size */ 1408 if (pkt->u.size < vmci_trans(vsk)->queue_pair_min_size || 1409 pkt->u.size > vmci_trans(vsk)->queue_pair_max_size) { 1410 err = -EINVAL; 1411 goto destroy; 1412 } 1413 1414 /* At this point we know the CID the peer is using to talk to us. */ 1415 1416 if (vsk->local_addr.svm_cid == VMADDR_CID_ANY) 1417 vsk->local_addr.svm_cid = pkt->dg.dst.context; 1418 1419 /* Setup the notify ops to be the highest supported version that both 1420 * the server and the client support. 1421 */ 1422 1423 if (vmci_transport_old_proto_override(&old_pkt_proto)) { 1424 old_proto = old_pkt_proto; 1425 } else { 1426 if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE) 1427 old_proto = true; 1428 else if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2) 1429 old_proto = false; 1430 1431 } 1432 1433 if (old_proto) 1434 version = VSOCK_PROTO_INVALID; 1435 else 1436 version = pkt->proto; 1437 1438 if (!vmci_transport_proto_to_notify_struct(sk, &version, old_proto)) { 1439 err = -EINVAL; 1440 goto destroy; 1441 } 1442 1443 /* Subscribe to detach events first. 1444 * 1445 * XXX We attach once for each queue pair created for now so it is easy 1446 * to find the socket (it's provided), but later we should only 1447 * subscribe once and add a way to lookup sockets by queue pair handle. 1448 */ 1449 err = vmci_event_subscribe(VMCI_EVENT_QP_PEER_DETACH, 1450 vmci_transport_peer_detach_cb, 1451 vmci_trans(vsk), &detach_sub_id); 1452 if (err < VMCI_SUCCESS) { 1453 err = vmci_transport_error_to_vsock_error(err); 1454 goto destroy; 1455 } 1456 1457 /* Make VMCI select the handle for us. */ 1458 handle = VMCI_INVALID_HANDLE; 1459 is_local = vsk->remote_addr.svm_cid == vsk->local_addr.svm_cid; 1460 flags = is_local ? VMCI_QPFLAG_LOCAL : 0; 1461 1462 err = vmci_transport_queue_pair_alloc(&qpair, 1463 &handle, 1464 pkt->u.size, 1465 pkt->u.size, 1466 vsk->remote_addr.svm_cid, 1467 flags, 1468 vmci_transport_is_trusted( 1469 vsk, 1470 vsk-> 1471 remote_addr.svm_cid)); 1472 if (err < 0) 1473 goto destroy; 1474 1475 err = vmci_transport_send_qp_offer(sk, handle); 1476 if (err < 0) { 1477 err = vmci_transport_error_to_vsock_error(err); 1478 goto destroy; 1479 } 1480 1481 vmci_trans(vsk)->qp_handle = handle; 1482 vmci_trans(vsk)->qpair = qpair; 1483 1484 vmci_trans(vsk)->produce_size = vmci_trans(vsk)->consume_size = 1485 pkt->u.size; 1486 1487 vmci_trans(vsk)->detach_sub_id = detach_sub_id; 1488 1489 vmci_trans(vsk)->notify_ops->process_negotiate(sk); 1490 1491 return 0; 1492 1493 destroy: 1494 if (detach_sub_id != VMCI_INVALID_ID) 1495 vmci_event_unsubscribe(detach_sub_id); 1496 1497 if (!vmci_handle_is_invalid(handle)) 1498 vmci_qpair_detach(&qpair); 1499 1500 return err; 1501 } 1502 1503 static int 1504 vmci_transport_recv_connecting_client_invalid(struct sock *sk, 1505 struct vmci_transport_packet *pkt) 1506 { 1507 int err = 0; 1508 struct vsock_sock *vsk = vsock_sk(sk); 1509 1510 if (vsk->sent_request) { 1511 vsk->sent_request = false; 1512 vsk->ignore_connecting_rst = true; 1513 1514 err = vmci_transport_send_conn_request( 1515 sk, vmci_trans(vsk)->queue_pair_size); 1516 if (err < 0) 1517 err = vmci_transport_error_to_vsock_error(err); 1518 else 1519 err = 0; 1520 1521 } 1522 1523 return err; 1524 } 1525 1526 static int vmci_transport_recv_connected(struct sock *sk, 1527 struct vmci_transport_packet *pkt) 1528 { 1529 struct vsock_sock *vsk; 1530 bool pkt_processed = false; 1531 1532 /* In cases where we are closing the connection, it's sufficient to 1533 * mark the state change (and maybe error) and wake up any waiting 1534 * threads. Since this is a connected socket, it's owned by a user 1535 * process and will be cleaned up when the failure is passed back on 1536 * the current or next system call. Our system call implementations 1537 * must therefore check for error and state changes on entry and when 1538 * being awoken. 1539 */ 1540 switch (pkt->type) { 1541 case VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN: 1542 if (pkt->u.mode) { 1543 vsk = vsock_sk(sk); 1544 1545 vsk->peer_shutdown |= pkt->u.mode; 1546 sk->sk_state_change(sk); 1547 } 1548 break; 1549 1550 case VMCI_TRANSPORT_PACKET_TYPE_RST: 1551 vsk = vsock_sk(sk); 1552 /* It is possible that we sent our peer a message (e.g a 1553 * WAITING_READ) right before we got notified that the peer had 1554 * detached. If that happens then we can get a RST pkt back 1555 * from our peer even though there is data available for us to 1556 * read. In that case, don't shutdown the socket completely but 1557 * instead allow the local client to finish reading data off 1558 * the queuepair. Always treat a RST pkt in connected mode like 1559 * a clean shutdown. 1560 */ 1561 sock_set_flag(sk, SOCK_DONE); 1562 vsk->peer_shutdown = SHUTDOWN_MASK; 1563 if (vsock_stream_has_data(vsk) <= 0) 1564 sk->sk_state = TCP_CLOSING; 1565 1566 sk->sk_state_change(sk); 1567 break; 1568 1569 default: 1570 vsk = vsock_sk(sk); 1571 vmci_trans(vsk)->notify_ops->handle_notify_pkt( 1572 sk, pkt, false, NULL, NULL, 1573 &pkt_processed); 1574 if (!pkt_processed) 1575 return -EINVAL; 1576 1577 break; 1578 } 1579 1580 return 0; 1581 } 1582 1583 static int vmci_transport_socket_init(struct vsock_sock *vsk, 1584 struct vsock_sock *psk) 1585 { 1586 vsk->trans = kmalloc(sizeof(struct vmci_transport), GFP_KERNEL); 1587 if (!vsk->trans) 1588 return -ENOMEM; 1589 1590 vmci_trans(vsk)->dg_handle = VMCI_INVALID_HANDLE; 1591 vmci_trans(vsk)->qp_handle = VMCI_INVALID_HANDLE; 1592 vmci_trans(vsk)->qpair = NULL; 1593 vmci_trans(vsk)->produce_size = vmci_trans(vsk)->consume_size = 0; 1594 vmci_trans(vsk)->detach_sub_id = VMCI_INVALID_ID; 1595 vmci_trans(vsk)->notify_ops = NULL; 1596 INIT_LIST_HEAD(&vmci_trans(vsk)->elem); 1597 vmci_trans(vsk)->sk = &vsk->sk; 1598 spin_lock_init(&vmci_trans(vsk)->lock); 1599 if (psk) { 1600 vmci_trans(vsk)->queue_pair_size = 1601 vmci_trans(psk)->queue_pair_size; 1602 vmci_trans(vsk)->queue_pair_min_size = 1603 vmci_trans(psk)->queue_pair_min_size; 1604 vmci_trans(vsk)->queue_pair_max_size = 1605 vmci_trans(psk)->queue_pair_max_size; 1606 } else { 1607 vmci_trans(vsk)->queue_pair_size = 1608 VMCI_TRANSPORT_DEFAULT_QP_SIZE; 1609 vmci_trans(vsk)->queue_pair_min_size = 1610 VMCI_TRANSPORT_DEFAULT_QP_SIZE_MIN; 1611 vmci_trans(vsk)->queue_pair_max_size = 1612 VMCI_TRANSPORT_DEFAULT_QP_SIZE_MAX; 1613 } 1614 1615 return 0; 1616 } 1617 1618 static void vmci_transport_free_resources(struct list_head *transport_list) 1619 { 1620 while (!list_empty(transport_list)) { 1621 struct vmci_transport *transport = 1622 list_first_entry(transport_list, struct vmci_transport, 1623 elem); 1624 list_del(&transport->elem); 1625 1626 if (transport->detach_sub_id != VMCI_INVALID_ID) { 1627 vmci_event_unsubscribe(transport->detach_sub_id); 1628 transport->detach_sub_id = VMCI_INVALID_ID; 1629 } 1630 1631 if (!vmci_handle_is_invalid(transport->qp_handle)) { 1632 vmci_qpair_detach(&transport->qpair); 1633 transport->qp_handle = VMCI_INVALID_HANDLE; 1634 transport->produce_size = 0; 1635 transport->consume_size = 0; 1636 } 1637 1638 kfree(transport); 1639 } 1640 } 1641 1642 static void vmci_transport_cleanup(struct work_struct *work) 1643 { 1644 LIST_HEAD(pending); 1645 1646 spin_lock_bh(&vmci_transport_cleanup_lock); 1647 list_replace_init(&vmci_transport_cleanup_list, &pending); 1648 spin_unlock_bh(&vmci_transport_cleanup_lock); 1649 vmci_transport_free_resources(&pending); 1650 } 1651 1652 static void vmci_transport_destruct(struct vsock_sock *vsk) 1653 { 1654 /* Ensure that the detach callback doesn't use the sk/vsk 1655 * we are about to destruct. 1656 */ 1657 spin_lock_bh(&vmci_trans(vsk)->lock); 1658 vmci_trans(vsk)->sk = NULL; 1659 spin_unlock_bh(&vmci_trans(vsk)->lock); 1660 1661 if (vmci_trans(vsk)->notify_ops) 1662 vmci_trans(vsk)->notify_ops->socket_destruct(vsk); 1663 1664 spin_lock_bh(&vmci_transport_cleanup_lock); 1665 list_add(&vmci_trans(vsk)->elem, &vmci_transport_cleanup_list); 1666 spin_unlock_bh(&vmci_transport_cleanup_lock); 1667 schedule_work(&vmci_transport_cleanup_work); 1668 1669 vsk->trans = NULL; 1670 } 1671 1672 static void vmci_transport_release(struct vsock_sock *vsk) 1673 { 1674 vsock_remove_sock(vsk); 1675 1676 if (!vmci_handle_is_invalid(vmci_trans(vsk)->dg_handle)) { 1677 vmci_datagram_destroy_handle(vmci_trans(vsk)->dg_handle); 1678 vmci_trans(vsk)->dg_handle = VMCI_INVALID_HANDLE; 1679 } 1680 } 1681 1682 static int vmci_transport_dgram_bind(struct vsock_sock *vsk, 1683 struct sockaddr_vm *addr) 1684 { 1685 u32 port; 1686 u32 flags; 1687 int err; 1688 1689 /* VMCI will select a resource ID for us if we provide 1690 * VMCI_INVALID_ID. 1691 */ 1692 port = addr->svm_port == VMADDR_PORT_ANY ? 1693 VMCI_INVALID_ID : addr->svm_port; 1694 1695 if (port <= LAST_RESERVED_PORT && !capable(CAP_NET_BIND_SERVICE)) 1696 return -EACCES; 1697 1698 flags = addr->svm_cid == VMADDR_CID_ANY ? 1699 VMCI_FLAG_ANYCID_DG_HND : 0; 1700 1701 err = vmci_transport_datagram_create_hnd(port, flags, 1702 vmci_transport_recv_dgram_cb, 1703 &vsk->sk, 1704 &vmci_trans(vsk)->dg_handle); 1705 if (err < VMCI_SUCCESS) 1706 return vmci_transport_error_to_vsock_error(err); 1707 vsock_addr_init(&vsk->local_addr, addr->svm_cid, 1708 vmci_trans(vsk)->dg_handle.resource); 1709 1710 return 0; 1711 } 1712 1713 static int vmci_transport_dgram_enqueue( 1714 struct vsock_sock *vsk, 1715 struct sockaddr_vm *remote_addr, 1716 struct msghdr *msg, 1717 size_t len) 1718 { 1719 int err; 1720 struct vmci_datagram *dg; 1721 1722 if (len > VMCI_MAX_DG_PAYLOAD_SIZE) 1723 return -EMSGSIZE; 1724 1725 if (!vmci_transport_allow_dgram(vsk, remote_addr->svm_cid)) 1726 return -EPERM; 1727 1728 /* Allocate a buffer for the user's message and our packet header. */ 1729 dg = kmalloc(len + sizeof(*dg), GFP_KERNEL); 1730 if (!dg) 1731 return -ENOMEM; 1732 1733 memcpy_from_msg(VMCI_DG_PAYLOAD(dg), msg, len); 1734 1735 dg->dst = vmci_make_handle(remote_addr->svm_cid, 1736 remote_addr->svm_port); 1737 dg->src = vmci_make_handle(vsk->local_addr.svm_cid, 1738 vsk->local_addr.svm_port); 1739 dg->payload_size = len; 1740 1741 err = vmci_datagram_send(dg); 1742 kfree(dg); 1743 if (err < 0) 1744 return vmci_transport_error_to_vsock_error(err); 1745 1746 return err - sizeof(*dg); 1747 } 1748 1749 static int vmci_transport_dgram_dequeue(struct vsock_sock *vsk, 1750 struct msghdr *msg, size_t len, 1751 int flags) 1752 { 1753 int err; 1754 int noblock; 1755 struct vmci_datagram *dg; 1756 size_t payload_len; 1757 struct sk_buff *skb; 1758 1759 noblock = flags & MSG_DONTWAIT; 1760 1761 if (flags & MSG_OOB || flags & MSG_ERRQUEUE) 1762 return -EOPNOTSUPP; 1763 1764 /* Retrieve the head sk_buff from the socket's receive queue. */ 1765 err = 0; 1766 skb = skb_recv_datagram(&vsk->sk, flags, noblock, &err); 1767 if (!skb) 1768 return err; 1769 1770 dg = (struct vmci_datagram *)skb->data; 1771 if (!dg) 1772 /* err is 0, meaning we read zero bytes. */ 1773 goto out; 1774 1775 payload_len = dg->payload_size; 1776 /* Ensure the sk_buff matches the payload size claimed in the packet. */ 1777 if (payload_len != skb->len - sizeof(*dg)) { 1778 err = -EINVAL; 1779 goto out; 1780 } 1781 1782 if (payload_len > len) { 1783 payload_len = len; 1784 msg->msg_flags |= MSG_TRUNC; 1785 } 1786 1787 /* Place the datagram payload in the user's iovec. */ 1788 err = skb_copy_datagram_msg(skb, sizeof(*dg), msg, payload_len); 1789 if (err) 1790 goto out; 1791 1792 if (msg->msg_name) { 1793 /* Provide the address of the sender. */ 1794 DECLARE_SOCKADDR(struct sockaddr_vm *, vm_addr, msg->msg_name); 1795 vsock_addr_init(vm_addr, dg->src.context, dg->src.resource); 1796 msg->msg_namelen = sizeof(*vm_addr); 1797 } 1798 err = payload_len; 1799 1800 out: 1801 skb_free_datagram(&vsk->sk, skb); 1802 return err; 1803 } 1804 1805 static bool vmci_transport_dgram_allow(u32 cid, u32 port) 1806 { 1807 if (cid == VMADDR_CID_HYPERVISOR) { 1808 /* Registrations of PBRPC Servers do not modify VMX/Hypervisor 1809 * state and are allowed. 1810 */ 1811 return port == VMCI_UNITY_PBRPC_REGISTER; 1812 } 1813 1814 return true; 1815 } 1816 1817 static int vmci_transport_connect(struct vsock_sock *vsk) 1818 { 1819 int err; 1820 bool old_pkt_proto = false; 1821 struct sock *sk = &vsk->sk; 1822 1823 if (vmci_transport_old_proto_override(&old_pkt_proto) && 1824 old_pkt_proto) { 1825 err = vmci_transport_send_conn_request( 1826 sk, vmci_trans(vsk)->queue_pair_size); 1827 if (err < 0) { 1828 sk->sk_state = TCP_CLOSE; 1829 return err; 1830 } 1831 } else { 1832 int supported_proto_versions = 1833 vmci_transport_new_proto_supported_versions(); 1834 err = vmci_transport_send_conn_request2( 1835 sk, vmci_trans(vsk)->queue_pair_size, 1836 supported_proto_versions); 1837 if (err < 0) { 1838 sk->sk_state = TCP_CLOSE; 1839 return err; 1840 } 1841 1842 vsk->sent_request = true; 1843 } 1844 1845 return err; 1846 } 1847 1848 static ssize_t vmci_transport_stream_dequeue( 1849 struct vsock_sock *vsk, 1850 struct msghdr *msg, 1851 size_t len, 1852 int flags) 1853 { 1854 if (flags & MSG_PEEK) 1855 return vmci_qpair_peekv(vmci_trans(vsk)->qpair, msg, len, 0); 1856 else 1857 return vmci_qpair_dequev(vmci_trans(vsk)->qpair, msg, len, 0); 1858 } 1859 1860 static ssize_t vmci_transport_stream_enqueue( 1861 struct vsock_sock *vsk, 1862 struct msghdr *msg, 1863 size_t len) 1864 { 1865 return vmci_qpair_enquev(vmci_trans(vsk)->qpair, msg, len, 0); 1866 } 1867 1868 static s64 vmci_transport_stream_has_data(struct vsock_sock *vsk) 1869 { 1870 return vmci_qpair_consume_buf_ready(vmci_trans(vsk)->qpair); 1871 } 1872 1873 static s64 vmci_transport_stream_has_space(struct vsock_sock *vsk) 1874 { 1875 return vmci_qpair_produce_free_space(vmci_trans(vsk)->qpair); 1876 } 1877 1878 static u64 vmci_transport_stream_rcvhiwat(struct vsock_sock *vsk) 1879 { 1880 return vmci_trans(vsk)->consume_size; 1881 } 1882 1883 static bool vmci_transport_stream_is_active(struct vsock_sock *vsk) 1884 { 1885 return !vmci_handle_is_invalid(vmci_trans(vsk)->qp_handle); 1886 } 1887 1888 static u64 vmci_transport_get_buffer_size(struct vsock_sock *vsk) 1889 { 1890 return vmci_trans(vsk)->queue_pair_size; 1891 } 1892 1893 static u64 vmci_transport_get_min_buffer_size(struct vsock_sock *vsk) 1894 { 1895 return vmci_trans(vsk)->queue_pair_min_size; 1896 } 1897 1898 static u64 vmci_transport_get_max_buffer_size(struct vsock_sock *vsk) 1899 { 1900 return vmci_trans(vsk)->queue_pair_max_size; 1901 } 1902 1903 static void vmci_transport_set_buffer_size(struct vsock_sock *vsk, u64 val) 1904 { 1905 if (val < vmci_trans(vsk)->queue_pair_min_size) 1906 vmci_trans(vsk)->queue_pair_min_size = val; 1907 if (val > vmci_trans(vsk)->queue_pair_max_size) 1908 vmci_trans(vsk)->queue_pair_max_size = val; 1909 vmci_trans(vsk)->queue_pair_size = val; 1910 } 1911 1912 static void vmci_transport_set_min_buffer_size(struct vsock_sock *vsk, 1913 u64 val) 1914 { 1915 if (val > vmci_trans(vsk)->queue_pair_size) 1916 vmci_trans(vsk)->queue_pair_size = val; 1917 vmci_trans(vsk)->queue_pair_min_size = val; 1918 } 1919 1920 static void vmci_transport_set_max_buffer_size(struct vsock_sock *vsk, 1921 u64 val) 1922 { 1923 if (val < vmci_trans(vsk)->queue_pair_size) 1924 vmci_trans(vsk)->queue_pair_size = val; 1925 vmci_trans(vsk)->queue_pair_max_size = val; 1926 } 1927 1928 static int vmci_transport_notify_poll_in( 1929 struct vsock_sock *vsk, 1930 size_t target, 1931 bool *data_ready_now) 1932 { 1933 return vmci_trans(vsk)->notify_ops->poll_in( 1934 &vsk->sk, target, data_ready_now); 1935 } 1936 1937 static int vmci_transport_notify_poll_out( 1938 struct vsock_sock *vsk, 1939 size_t target, 1940 bool *space_available_now) 1941 { 1942 return vmci_trans(vsk)->notify_ops->poll_out( 1943 &vsk->sk, target, space_available_now); 1944 } 1945 1946 static int vmci_transport_notify_recv_init( 1947 struct vsock_sock *vsk, 1948 size_t target, 1949 struct vsock_transport_recv_notify_data *data) 1950 { 1951 return vmci_trans(vsk)->notify_ops->recv_init( 1952 &vsk->sk, target, 1953 (struct vmci_transport_recv_notify_data *)data); 1954 } 1955 1956 static int vmci_transport_notify_recv_pre_block( 1957 struct vsock_sock *vsk, 1958 size_t target, 1959 struct vsock_transport_recv_notify_data *data) 1960 { 1961 return vmci_trans(vsk)->notify_ops->recv_pre_block( 1962 &vsk->sk, target, 1963 (struct vmci_transport_recv_notify_data *)data); 1964 } 1965 1966 static int vmci_transport_notify_recv_pre_dequeue( 1967 struct vsock_sock *vsk, 1968 size_t target, 1969 struct vsock_transport_recv_notify_data *data) 1970 { 1971 return vmci_trans(vsk)->notify_ops->recv_pre_dequeue( 1972 &vsk->sk, target, 1973 (struct vmci_transport_recv_notify_data *)data); 1974 } 1975 1976 static int vmci_transport_notify_recv_post_dequeue( 1977 struct vsock_sock *vsk, 1978 size_t target, 1979 ssize_t copied, 1980 bool data_read, 1981 struct vsock_transport_recv_notify_data *data) 1982 { 1983 return vmci_trans(vsk)->notify_ops->recv_post_dequeue( 1984 &vsk->sk, target, copied, data_read, 1985 (struct vmci_transport_recv_notify_data *)data); 1986 } 1987 1988 static int vmci_transport_notify_send_init( 1989 struct vsock_sock *vsk, 1990 struct vsock_transport_send_notify_data *data) 1991 { 1992 return vmci_trans(vsk)->notify_ops->send_init( 1993 &vsk->sk, 1994 (struct vmci_transport_send_notify_data *)data); 1995 } 1996 1997 static int vmci_transport_notify_send_pre_block( 1998 struct vsock_sock *vsk, 1999 struct vsock_transport_send_notify_data *data) 2000 { 2001 return vmci_trans(vsk)->notify_ops->send_pre_block( 2002 &vsk->sk, 2003 (struct vmci_transport_send_notify_data *)data); 2004 } 2005 2006 static int vmci_transport_notify_send_pre_enqueue( 2007 struct vsock_sock *vsk, 2008 struct vsock_transport_send_notify_data *data) 2009 { 2010 return vmci_trans(vsk)->notify_ops->send_pre_enqueue( 2011 &vsk->sk, 2012 (struct vmci_transport_send_notify_data *)data); 2013 } 2014 2015 static int vmci_transport_notify_send_post_enqueue( 2016 struct vsock_sock *vsk, 2017 ssize_t written, 2018 struct vsock_transport_send_notify_data *data) 2019 { 2020 return vmci_trans(vsk)->notify_ops->send_post_enqueue( 2021 &vsk->sk, written, 2022 (struct vmci_transport_send_notify_data *)data); 2023 } 2024 2025 static bool vmci_transport_old_proto_override(bool *old_pkt_proto) 2026 { 2027 if (PROTOCOL_OVERRIDE != -1) { 2028 if (PROTOCOL_OVERRIDE == 0) 2029 *old_pkt_proto = true; 2030 else 2031 *old_pkt_proto = false; 2032 2033 pr_info("Proto override in use\n"); 2034 return true; 2035 } 2036 2037 return false; 2038 } 2039 2040 static bool vmci_transport_proto_to_notify_struct(struct sock *sk, 2041 u16 *proto, 2042 bool old_pkt_proto) 2043 { 2044 struct vsock_sock *vsk = vsock_sk(sk); 2045 2046 if (old_pkt_proto) { 2047 if (*proto != VSOCK_PROTO_INVALID) { 2048 pr_err("Can't set both an old and new protocol\n"); 2049 return false; 2050 } 2051 vmci_trans(vsk)->notify_ops = &vmci_transport_notify_pkt_ops; 2052 goto exit; 2053 } 2054 2055 switch (*proto) { 2056 case VSOCK_PROTO_PKT_ON_NOTIFY: 2057 vmci_trans(vsk)->notify_ops = 2058 &vmci_transport_notify_pkt_q_state_ops; 2059 break; 2060 default: 2061 pr_err("Unknown notify protocol version\n"); 2062 return false; 2063 } 2064 2065 exit: 2066 vmci_trans(vsk)->notify_ops->socket_init(sk); 2067 return true; 2068 } 2069 2070 static u16 vmci_transport_new_proto_supported_versions(void) 2071 { 2072 if (PROTOCOL_OVERRIDE != -1) 2073 return PROTOCOL_OVERRIDE; 2074 2075 return VSOCK_PROTO_ALL_SUPPORTED; 2076 } 2077 2078 static u32 vmci_transport_get_local_cid(void) 2079 { 2080 return vmci_get_context_id(); 2081 } 2082 2083 static const struct vsock_transport vmci_transport = { 2084 .init = vmci_transport_socket_init, 2085 .destruct = vmci_transport_destruct, 2086 .release = vmci_transport_release, 2087 .connect = vmci_transport_connect, 2088 .dgram_bind = vmci_transport_dgram_bind, 2089 .dgram_dequeue = vmci_transport_dgram_dequeue, 2090 .dgram_enqueue = vmci_transport_dgram_enqueue, 2091 .dgram_allow = vmci_transport_dgram_allow, 2092 .stream_dequeue = vmci_transport_stream_dequeue, 2093 .stream_enqueue = vmci_transport_stream_enqueue, 2094 .stream_has_data = vmci_transport_stream_has_data, 2095 .stream_has_space = vmci_transport_stream_has_space, 2096 .stream_rcvhiwat = vmci_transport_stream_rcvhiwat, 2097 .stream_is_active = vmci_transport_stream_is_active, 2098 .stream_allow = vmci_transport_stream_allow, 2099 .notify_poll_in = vmci_transport_notify_poll_in, 2100 .notify_poll_out = vmci_transport_notify_poll_out, 2101 .notify_recv_init = vmci_transport_notify_recv_init, 2102 .notify_recv_pre_block = vmci_transport_notify_recv_pre_block, 2103 .notify_recv_pre_dequeue = vmci_transport_notify_recv_pre_dequeue, 2104 .notify_recv_post_dequeue = vmci_transport_notify_recv_post_dequeue, 2105 .notify_send_init = vmci_transport_notify_send_init, 2106 .notify_send_pre_block = vmci_transport_notify_send_pre_block, 2107 .notify_send_pre_enqueue = vmci_transport_notify_send_pre_enqueue, 2108 .notify_send_post_enqueue = vmci_transport_notify_send_post_enqueue, 2109 .shutdown = vmci_transport_shutdown, 2110 .set_buffer_size = vmci_transport_set_buffer_size, 2111 .set_min_buffer_size = vmci_transport_set_min_buffer_size, 2112 .set_max_buffer_size = vmci_transport_set_max_buffer_size, 2113 .get_buffer_size = vmci_transport_get_buffer_size, 2114 .get_min_buffer_size = vmci_transport_get_min_buffer_size, 2115 .get_max_buffer_size = vmci_transport_get_max_buffer_size, 2116 .get_local_cid = vmci_transport_get_local_cid, 2117 }; 2118 2119 static int __init vmci_transport_init(void) 2120 { 2121 int err; 2122 2123 /* Create the datagram handle that we will use to send and receive all 2124 * VSocket control messages for this context. 2125 */ 2126 err = vmci_transport_datagram_create_hnd(VMCI_TRANSPORT_PACKET_RID, 2127 VMCI_FLAG_ANYCID_DG_HND, 2128 vmci_transport_recv_stream_cb, 2129 NULL, 2130 &vmci_transport_stream_handle); 2131 if (err < VMCI_SUCCESS) { 2132 pr_err("Unable to create datagram handle. (%d)\n", err); 2133 return vmci_transport_error_to_vsock_error(err); 2134 } 2135 2136 err = vmci_event_subscribe(VMCI_EVENT_QP_RESUMED, 2137 vmci_transport_qp_resumed_cb, 2138 NULL, &vmci_transport_qp_resumed_sub_id); 2139 if (err < VMCI_SUCCESS) { 2140 pr_err("Unable to subscribe to resumed event. (%d)\n", err); 2141 err = vmci_transport_error_to_vsock_error(err); 2142 vmci_transport_qp_resumed_sub_id = VMCI_INVALID_ID; 2143 goto err_destroy_stream_handle; 2144 } 2145 2146 err = vsock_core_init(&vmci_transport); 2147 if (err < 0) 2148 goto err_unsubscribe; 2149 2150 return 0; 2151 2152 err_unsubscribe: 2153 vmci_event_unsubscribe(vmci_transport_qp_resumed_sub_id); 2154 err_destroy_stream_handle: 2155 vmci_datagram_destroy_handle(vmci_transport_stream_handle); 2156 return err; 2157 } 2158 module_init(vmci_transport_init); 2159 2160 static void __exit vmci_transport_exit(void) 2161 { 2162 cancel_work_sync(&vmci_transport_cleanup_work); 2163 vmci_transport_free_resources(&vmci_transport_cleanup_list); 2164 2165 if (!vmci_handle_is_invalid(vmci_transport_stream_handle)) { 2166 if (vmci_datagram_destroy_handle( 2167 vmci_transport_stream_handle) != VMCI_SUCCESS) 2168 pr_err("Couldn't destroy datagram handle\n"); 2169 vmci_transport_stream_handle = VMCI_INVALID_HANDLE; 2170 } 2171 2172 if (vmci_transport_qp_resumed_sub_id != VMCI_INVALID_ID) { 2173 vmci_event_unsubscribe(vmci_transport_qp_resumed_sub_id); 2174 vmci_transport_qp_resumed_sub_id = VMCI_INVALID_ID; 2175 } 2176 2177 vsock_core_exit(); 2178 } 2179 module_exit(vmci_transport_exit); 2180 2181 MODULE_AUTHOR("VMware, Inc."); 2182 MODULE_DESCRIPTION("VMCI transport for Virtual Sockets"); 2183 MODULE_VERSION("1.0.5.0-k"); 2184 MODULE_LICENSE("GPL v2"); 2185 MODULE_ALIAS("vmware_vsock"); 2186 MODULE_ALIAS_NETPROTO(PF_VSOCK); 2187