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