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 /* transport can be NULL if we hit a failure at init() time */ 1655 if (!vmci_trans(vsk)) 1656 return; 1657 1658 /* Ensure that the detach callback doesn't use the sk/vsk 1659 * we are about to destruct. 1660 */ 1661 spin_lock_bh(&vmci_trans(vsk)->lock); 1662 vmci_trans(vsk)->sk = NULL; 1663 spin_unlock_bh(&vmci_trans(vsk)->lock); 1664 1665 if (vmci_trans(vsk)->notify_ops) 1666 vmci_trans(vsk)->notify_ops->socket_destruct(vsk); 1667 1668 spin_lock_bh(&vmci_transport_cleanup_lock); 1669 list_add(&vmci_trans(vsk)->elem, &vmci_transport_cleanup_list); 1670 spin_unlock_bh(&vmci_transport_cleanup_lock); 1671 schedule_work(&vmci_transport_cleanup_work); 1672 1673 vsk->trans = NULL; 1674 } 1675 1676 static void vmci_transport_release(struct vsock_sock *vsk) 1677 { 1678 vsock_remove_sock(vsk); 1679 1680 if (!vmci_handle_is_invalid(vmci_trans(vsk)->dg_handle)) { 1681 vmci_datagram_destroy_handle(vmci_trans(vsk)->dg_handle); 1682 vmci_trans(vsk)->dg_handle = VMCI_INVALID_HANDLE; 1683 } 1684 } 1685 1686 static int vmci_transport_dgram_bind(struct vsock_sock *vsk, 1687 struct sockaddr_vm *addr) 1688 { 1689 u32 port; 1690 u32 flags; 1691 int err; 1692 1693 /* VMCI will select a resource ID for us if we provide 1694 * VMCI_INVALID_ID. 1695 */ 1696 port = addr->svm_port == VMADDR_PORT_ANY ? 1697 VMCI_INVALID_ID : addr->svm_port; 1698 1699 if (port <= LAST_RESERVED_PORT && !capable(CAP_NET_BIND_SERVICE)) 1700 return -EACCES; 1701 1702 flags = addr->svm_cid == VMADDR_CID_ANY ? 1703 VMCI_FLAG_ANYCID_DG_HND : 0; 1704 1705 err = vmci_transport_datagram_create_hnd(port, flags, 1706 vmci_transport_recv_dgram_cb, 1707 &vsk->sk, 1708 &vmci_trans(vsk)->dg_handle); 1709 if (err < VMCI_SUCCESS) 1710 return vmci_transport_error_to_vsock_error(err); 1711 vsock_addr_init(&vsk->local_addr, addr->svm_cid, 1712 vmci_trans(vsk)->dg_handle.resource); 1713 1714 return 0; 1715 } 1716 1717 static int vmci_transport_dgram_enqueue( 1718 struct vsock_sock *vsk, 1719 struct sockaddr_vm *remote_addr, 1720 struct msghdr *msg, 1721 size_t len) 1722 { 1723 int err; 1724 struct vmci_datagram *dg; 1725 1726 if (len > VMCI_MAX_DG_PAYLOAD_SIZE) 1727 return -EMSGSIZE; 1728 1729 if (!vmci_transport_allow_dgram(vsk, remote_addr->svm_cid)) 1730 return -EPERM; 1731 1732 /* Allocate a buffer for the user's message and our packet header. */ 1733 dg = kmalloc(len + sizeof(*dg), GFP_KERNEL); 1734 if (!dg) 1735 return -ENOMEM; 1736 1737 memcpy_from_msg(VMCI_DG_PAYLOAD(dg), msg, len); 1738 1739 dg->dst = vmci_make_handle(remote_addr->svm_cid, 1740 remote_addr->svm_port); 1741 dg->src = vmci_make_handle(vsk->local_addr.svm_cid, 1742 vsk->local_addr.svm_port); 1743 dg->payload_size = len; 1744 1745 err = vmci_datagram_send(dg); 1746 kfree(dg); 1747 if (err < 0) 1748 return vmci_transport_error_to_vsock_error(err); 1749 1750 return err - sizeof(*dg); 1751 } 1752 1753 static int vmci_transport_dgram_dequeue(struct vsock_sock *vsk, 1754 struct msghdr *msg, size_t len, 1755 int flags) 1756 { 1757 int err; 1758 int noblock; 1759 struct vmci_datagram *dg; 1760 size_t payload_len; 1761 struct sk_buff *skb; 1762 1763 noblock = flags & MSG_DONTWAIT; 1764 1765 if (flags & MSG_OOB || flags & MSG_ERRQUEUE) 1766 return -EOPNOTSUPP; 1767 1768 /* Retrieve the head sk_buff from the socket's receive queue. */ 1769 err = 0; 1770 skb = skb_recv_datagram(&vsk->sk, flags, noblock, &err); 1771 if (!skb) 1772 return err; 1773 1774 dg = (struct vmci_datagram *)skb->data; 1775 if (!dg) 1776 /* err is 0, meaning we read zero bytes. */ 1777 goto out; 1778 1779 payload_len = dg->payload_size; 1780 /* Ensure the sk_buff matches the payload size claimed in the packet. */ 1781 if (payload_len != skb->len - sizeof(*dg)) { 1782 err = -EINVAL; 1783 goto out; 1784 } 1785 1786 if (payload_len > len) { 1787 payload_len = len; 1788 msg->msg_flags |= MSG_TRUNC; 1789 } 1790 1791 /* Place the datagram payload in the user's iovec. */ 1792 err = skb_copy_datagram_msg(skb, sizeof(*dg), msg, payload_len); 1793 if (err) 1794 goto out; 1795 1796 if (msg->msg_name) { 1797 /* Provide the address of the sender. */ 1798 DECLARE_SOCKADDR(struct sockaddr_vm *, vm_addr, msg->msg_name); 1799 vsock_addr_init(vm_addr, dg->src.context, dg->src.resource); 1800 msg->msg_namelen = sizeof(*vm_addr); 1801 } 1802 err = payload_len; 1803 1804 out: 1805 skb_free_datagram(&vsk->sk, skb); 1806 return err; 1807 } 1808 1809 static bool vmci_transport_dgram_allow(u32 cid, u32 port) 1810 { 1811 if (cid == VMADDR_CID_HYPERVISOR) { 1812 /* Registrations of PBRPC Servers do not modify VMX/Hypervisor 1813 * state and are allowed. 1814 */ 1815 return port == VMCI_UNITY_PBRPC_REGISTER; 1816 } 1817 1818 return true; 1819 } 1820 1821 static int vmci_transport_connect(struct vsock_sock *vsk) 1822 { 1823 int err; 1824 bool old_pkt_proto = false; 1825 struct sock *sk = &vsk->sk; 1826 1827 if (vmci_transport_old_proto_override(&old_pkt_proto) && 1828 old_pkt_proto) { 1829 err = vmci_transport_send_conn_request( 1830 sk, vmci_trans(vsk)->queue_pair_size); 1831 if (err < 0) { 1832 sk->sk_state = TCP_CLOSE; 1833 return err; 1834 } 1835 } else { 1836 int supported_proto_versions = 1837 vmci_transport_new_proto_supported_versions(); 1838 err = vmci_transport_send_conn_request2( 1839 sk, vmci_trans(vsk)->queue_pair_size, 1840 supported_proto_versions); 1841 if (err < 0) { 1842 sk->sk_state = TCP_CLOSE; 1843 return err; 1844 } 1845 1846 vsk->sent_request = true; 1847 } 1848 1849 return err; 1850 } 1851 1852 static ssize_t vmci_transport_stream_dequeue( 1853 struct vsock_sock *vsk, 1854 struct msghdr *msg, 1855 size_t len, 1856 int flags) 1857 { 1858 if (flags & MSG_PEEK) 1859 return vmci_qpair_peekv(vmci_trans(vsk)->qpair, msg, len, 0); 1860 else 1861 return vmci_qpair_dequev(vmci_trans(vsk)->qpair, msg, len, 0); 1862 } 1863 1864 static ssize_t vmci_transport_stream_enqueue( 1865 struct vsock_sock *vsk, 1866 struct msghdr *msg, 1867 size_t len) 1868 { 1869 return vmci_qpair_enquev(vmci_trans(vsk)->qpair, msg, len, 0); 1870 } 1871 1872 static s64 vmci_transport_stream_has_data(struct vsock_sock *vsk) 1873 { 1874 return vmci_qpair_consume_buf_ready(vmci_trans(vsk)->qpair); 1875 } 1876 1877 static s64 vmci_transport_stream_has_space(struct vsock_sock *vsk) 1878 { 1879 return vmci_qpair_produce_free_space(vmci_trans(vsk)->qpair); 1880 } 1881 1882 static u64 vmci_transport_stream_rcvhiwat(struct vsock_sock *vsk) 1883 { 1884 return vmci_trans(vsk)->consume_size; 1885 } 1886 1887 static bool vmci_transport_stream_is_active(struct vsock_sock *vsk) 1888 { 1889 return !vmci_handle_is_invalid(vmci_trans(vsk)->qp_handle); 1890 } 1891 1892 static u64 vmci_transport_get_buffer_size(struct vsock_sock *vsk) 1893 { 1894 return vmci_trans(vsk)->queue_pair_size; 1895 } 1896 1897 static u64 vmci_transport_get_min_buffer_size(struct vsock_sock *vsk) 1898 { 1899 return vmci_trans(vsk)->queue_pair_min_size; 1900 } 1901 1902 static u64 vmci_transport_get_max_buffer_size(struct vsock_sock *vsk) 1903 { 1904 return vmci_trans(vsk)->queue_pair_max_size; 1905 } 1906 1907 static void vmci_transport_set_buffer_size(struct vsock_sock *vsk, u64 val) 1908 { 1909 if (val < vmci_trans(vsk)->queue_pair_min_size) 1910 vmci_trans(vsk)->queue_pair_min_size = val; 1911 if (val > vmci_trans(vsk)->queue_pair_max_size) 1912 vmci_trans(vsk)->queue_pair_max_size = val; 1913 vmci_trans(vsk)->queue_pair_size = val; 1914 } 1915 1916 static void vmci_transport_set_min_buffer_size(struct vsock_sock *vsk, 1917 u64 val) 1918 { 1919 if (val > vmci_trans(vsk)->queue_pair_size) 1920 vmci_trans(vsk)->queue_pair_size = val; 1921 vmci_trans(vsk)->queue_pair_min_size = val; 1922 } 1923 1924 static void vmci_transport_set_max_buffer_size(struct vsock_sock *vsk, 1925 u64 val) 1926 { 1927 if (val < vmci_trans(vsk)->queue_pair_size) 1928 vmci_trans(vsk)->queue_pair_size = val; 1929 vmci_trans(vsk)->queue_pair_max_size = val; 1930 } 1931 1932 static int vmci_transport_notify_poll_in( 1933 struct vsock_sock *vsk, 1934 size_t target, 1935 bool *data_ready_now) 1936 { 1937 return vmci_trans(vsk)->notify_ops->poll_in( 1938 &vsk->sk, target, data_ready_now); 1939 } 1940 1941 static int vmci_transport_notify_poll_out( 1942 struct vsock_sock *vsk, 1943 size_t target, 1944 bool *space_available_now) 1945 { 1946 return vmci_trans(vsk)->notify_ops->poll_out( 1947 &vsk->sk, target, space_available_now); 1948 } 1949 1950 static int vmci_transport_notify_recv_init( 1951 struct vsock_sock *vsk, 1952 size_t target, 1953 struct vsock_transport_recv_notify_data *data) 1954 { 1955 return vmci_trans(vsk)->notify_ops->recv_init( 1956 &vsk->sk, target, 1957 (struct vmci_transport_recv_notify_data *)data); 1958 } 1959 1960 static int vmci_transport_notify_recv_pre_block( 1961 struct vsock_sock *vsk, 1962 size_t target, 1963 struct vsock_transport_recv_notify_data *data) 1964 { 1965 return vmci_trans(vsk)->notify_ops->recv_pre_block( 1966 &vsk->sk, target, 1967 (struct vmci_transport_recv_notify_data *)data); 1968 } 1969 1970 static int vmci_transport_notify_recv_pre_dequeue( 1971 struct vsock_sock *vsk, 1972 size_t target, 1973 struct vsock_transport_recv_notify_data *data) 1974 { 1975 return vmci_trans(vsk)->notify_ops->recv_pre_dequeue( 1976 &vsk->sk, target, 1977 (struct vmci_transport_recv_notify_data *)data); 1978 } 1979 1980 static int vmci_transport_notify_recv_post_dequeue( 1981 struct vsock_sock *vsk, 1982 size_t target, 1983 ssize_t copied, 1984 bool data_read, 1985 struct vsock_transport_recv_notify_data *data) 1986 { 1987 return vmci_trans(vsk)->notify_ops->recv_post_dequeue( 1988 &vsk->sk, target, copied, data_read, 1989 (struct vmci_transport_recv_notify_data *)data); 1990 } 1991 1992 static int vmci_transport_notify_send_init( 1993 struct vsock_sock *vsk, 1994 struct vsock_transport_send_notify_data *data) 1995 { 1996 return vmci_trans(vsk)->notify_ops->send_init( 1997 &vsk->sk, 1998 (struct vmci_transport_send_notify_data *)data); 1999 } 2000 2001 static int vmci_transport_notify_send_pre_block( 2002 struct vsock_sock *vsk, 2003 struct vsock_transport_send_notify_data *data) 2004 { 2005 return vmci_trans(vsk)->notify_ops->send_pre_block( 2006 &vsk->sk, 2007 (struct vmci_transport_send_notify_data *)data); 2008 } 2009 2010 static int vmci_transport_notify_send_pre_enqueue( 2011 struct vsock_sock *vsk, 2012 struct vsock_transport_send_notify_data *data) 2013 { 2014 return vmci_trans(vsk)->notify_ops->send_pre_enqueue( 2015 &vsk->sk, 2016 (struct vmci_transport_send_notify_data *)data); 2017 } 2018 2019 static int vmci_transport_notify_send_post_enqueue( 2020 struct vsock_sock *vsk, 2021 ssize_t written, 2022 struct vsock_transport_send_notify_data *data) 2023 { 2024 return vmci_trans(vsk)->notify_ops->send_post_enqueue( 2025 &vsk->sk, written, 2026 (struct vmci_transport_send_notify_data *)data); 2027 } 2028 2029 static bool vmci_transport_old_proto_override(bool *old_pkt_proto) 2030 { 2031 if (PROTOCOL_OVERRIDE != -1) { 2032 if (PROTOCOL_OVERRIDE == 0) 2033 *old_pkt_proto = true; 2034 else 2035 *old_pkt_proto = false; 2036 2037 pr_info("Proto override in use\n"); 2038 return true; 2039 } 2040 2041 return false; 2042 } 2043 2044 static bool vmci_transport_proto_to_notify_struct(struct sock *sk, 2045 u16 *proto, 2046 bool old_pkt_proto) 2047 { 2048 struct vsock_sock *vsk = vsock_sk(sk); 2049 2050 if (old_pkt_proto) { 2051 if (*proto != VSOCK_PROTO_INVALID) { 2052 pr_err("Can't set both an old and new protocol\n"); 2053 return false; 2054 } 2055 vmci_trans(vsk)->notify_ops = &vmci_transport_notify_pkt_ops; 2056 goto exit; 2057 } 2058 2059 switch (*proto) { 2060 case VSOCK_PROTO_PKT_ON_NOTIFY: 2061 vmci_trans(vsk)->notify_ops = 2062 &vmci_transport_notify_pkt_q_state_ops; 2063 break; 2064 default: 2065 pr_err("Unknown notify protocol version\n"); 2066 return false; 2067 } 2068 2069 exit: 2070 vmci_trans(vsk)->notify_ops->socket_init(sk); 2071 return true; 2072 } 2073 2074 static u16 vmci_transport_new_proto_supported_versions(void) 2075 { 2076 if (PROTOCOL_OVERRIDE != -1) 2077 return PROTOCOL_OVERRIDE; 2078 2079 return VSOCK_PROTO_ALL_SUPPORTED; 2080 } 2081 2082 static u32 vmci_transport_get_local_cid(void) 2083 { 2084 return vmci_get_context_id(); 2085 } 2086 2087 static const struct vsock_transport vmci_transport = { 2088 .init = vmci_transport_socket_init, 2089 .destruct = vmci_transport_destruct, 2090 .release = vmci_transport_release, 2091 .connect = vmci_transport_connect, 2092 .dgram_bind = vmci_transport_dgram_bind, 2093 .dgram_dequeue = vmci_transport_dgram_dequeue, 2094 .dgram_enqueue = vmci_transport_dgram_enqueue, 2095 .dgram_allow = vmci_transport_dgram_allow, 2096 .stream_dequeue = vmci_transport_stream_dequeue, 2097 .stream_enqueue = vmci_transport_stream_enqueue, 2098 .stream_has_data = vmci_transport_stream_has_data, 2099 .stream_has_space = vmci_transport_stream_has_space, 2100 .stream_rcvhiwat = vmci_transport_stream_rcvhiwat, 2101 .stream_is_active = vmci_transport_stream_is_active, 2102 .stream_allow = vmci_transport_stream_allow, 2103 .notify_poll_in = vmci_transport_notify_poll_in, 2104 .notify_poll_out = vmci_transport_notify_poll_out, 2105 .notify_recv_init = vmci_transport_notify_recv_init, 2106 .notify_recv_pre_block = vmci_transport_notify_recv_pre_block, 2107 .notify_recv_pre_dequeue = vmci_transport_notify_recv_pre_dequeue, 2108 .notify_recv_post_dequeue = vmci_transport_notify_recv_post_dequeue, 2109 .notify_send_init = vmci_transport_notify_send_init, 2110 .notify_send_pre_block = vmci_transport_notify_send_pre_block, 2111 .notify_send_pre_enqueue = vmci_transport_notify_send_pre_enqueue, 2112 .notify_send_post_enqueue = vmci_transport_notify_send_post_enqueue, 2113 .shutdown = vmci_transport_shutdown, 2114 .set_buffer_size = vmci_transport_set_buffer_size, 2115 .set_min_buffer_size = vmci_transport_set_min_buffer_size, 2116 .set_max_buffer_size = vmci_transport_set_max_buffer_size, 2117 .get_buffer_size = vmci_transport_get_buffer_size, 2118 .get_min_buffer_size = vmci_transport_get_min_buffer_size, 2119 .get_max_buffer_size = vmci_transport_get_max_buffer_size, 2120 .get_local_cid = vmci_transport_get_local_cid, 2121 }; 2122 2123 static int __init vmci_transport_init(void) 2124 { 2125 int err; 2126 2127 /* Create the datagram handle that we will use to send and receive all 2128 * VSocket control messages for this context. 2129 */ 2130 err = vmci_transport_datagram_create_hnd(VMCI_TRANSPORT_PACKET_RID, 2131 VMCI_FLAG_ANYCID_DG_HND, 2132 vmci_transport_recv_stream_cb, 2133 NULL, 2134 &vmci_transport_stream_handle); 2135 if (err < VMCI_SUCCESS) { 2136 pr_err("Unable to create datagram handle. (%d)\n", err); 2137 return vmci_transport_error_to_vsock_error(err); 2138 } 2139 2140 err = vmci_event_subscribe(VMCI_EVENT_QP_RESUMED, 2141 vmci_transport_qp_resumed_cb, 2142 NULL, &vmci_transport_qp_resumed_sub_id); 2143 if (err < VMCI_SUCCESS) { 2144 pr_err("Unable to subscribe to resumed event. (%d)\n", err); 2145 err = vmci_transport_error_to_vsock_error(err); 2146 vmci_transport_qp_resumed_sub_id = VMCI_INVALID_ID; 2147 goto err_destroy_stream_handle; 2148 } 2149 2150 err = vsock_core_init(&vmci_transport); 2151 if (err < 0) 2152 goto err_unsubscribe; 2153 2154 return 0; 2155 2156 err_unsubscribe: 2157 vmci_event_unsubscribe(vmci_transport_qp_resumed_sub_id); 2158 err_destroy_stream_handle: 2159 vmci_datagram_destroy_handle(vmci_transport_stream_handle); 2160 return err; 2161 } 2162 module_init(vmci_transport_init); 2163 2164 static void __exit vmci_transport_exit(void) 2165 { 2166 cancel_work_sync(&vmci_transport_cleanup_work); 2167 vmci_transport_free_resources(&vmci_transport_cleanup_list); 2168 2169 if (!vmci_handle_is_invalid(vmci_transport_stream_handle)) { 2170 if (vmci_datagram_destroy_handle( 2171 vmci_transport_stream_handle) != VMCI_SUCCESS) 2172 pr_err("Couldn't destroy datagram handle\n"); 2173 vmci_transport_stream_handle = VMCI_INVALID_HANDLE; 2174 } 2175 2176 if (vmci_transport_qp_resumed_sub_id != VMCI_INVALID_ID) { 2177 vmci_event_unsubscribe(vmci_transport_qp_resumed_sub_id); 2178 vmci_transport_qp_resumed_sub_id = VMCI_INVALID_ID; 2179 } 2180 2181 vsock_core_exit(); 2182 } 2183 module_exit(vmci_transport_exit); 2184 2185 MODULE_AUTHOR("VMware, Inc."); 2186 MODULE_DESCRIPTION("VMCI transport for Virtual Sockets"); 2187 MODULE_VERSION("1.0.5.0-k"); 2188 MODULE_LICENSE("GPL v2"); 2189 MODULE_ALIAS("vmware_vsock"); 2190 MODULE_ALIAS_NETPROTO(PF_VSOCK); 2191