kconfig: fix memory leak when EOF is encountered in quotation
[linux/fpc-iii.git] / net / vmw_vsock / vmci_transport.c
blobcb332adb84cdcadc006de6d7a8668111babd2f53
1 /*
2 * VMware vSockets Driver
4 * Copyright (C) 2007-2013 VMware, Inc. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation version 2 and no later version.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
16 #include <linux/types.h>
17 #include <linux/bitops.h>
18 #include <linux/cred.h>
19 #include <linux/init.h>
20 #include <linux/io.h>
21 #include <linux/kernel.h>
22 #include <linux/kmod.h>
23 #include <linux/list.h>
24 #include <linux/module.h>
25 #include <linux/mutex.h>
26 #include <linux/net.h>
27 #include <linux/poll.h>
28 #include <linux/skbuff.h>
29 #include <linux/smp.h>
30 #include <linux/socket.h>
31 #include <linux/stddef.h>
32 #include <linux/unistd.h>
33 #include <linux/wait.h>
34 #include <linux/workqueue.h>
35 #include <net/sock.h>
36 #include <net/af_vsock.h>
38 #include "vmci_transport_notify.h"
40 static int vmci_transport_recv_dgram_cb(void *data, struct vmci_datagram *dg);
41 static int vmci_transport_recv_stream_cb(void *data, struct vmci_datagram *dg);
42 static void vmci_transport_peer_detach_cb(u32 sub_id,
43 const struct vmci_event_data *ed,
44 void *client_data);
45 static void vmci_transport_recv_pkt_work(struct work_struct *work);
46 static void vmci_transport_cleanup(struct work_struct *work);
47 static int vmci_transport_recv_listen(struct sock *sk,
48 struct vmci_transport_packet *pkt);
49 static int vmci_transport_recv_connecting_server(
50 struct sock *sk,
51 struct sock *pending,
52 struct vmci_transport_packet *pkt);
53 static int vmci_transport_recv_connecting_client(
54 struct sock *sk,
55 struct vmci_transport_packet *pkt);
56 static int vmci_transport_recv_connecting_client_negotiate(
57 struct sock *sk,
58 struct vmci_transport_packet *pkt);
59 static int vmci_transport_recv_connecting_client_invalid(
60 struct sock *sk,
61 struct vmci_transport_packet *pkt);
62 static int vmci_transport_recv_connected(struct sock *sk,
63 struct vmci_transport_packet *pkt);
64 static bool vmci_transport_old_proto_override(bool *old_pkt_proto);
65 static u16 vmci_transport_new_proto_supported_versions(void);
66 static bool vmci_transport_proto_to_notify_struct(struct sock *sk, u16 *proto,
67 bool old_pkt_proto);
69 struct vmci_transport_recv_pkt_info {
70 struct work_struct work;
71 struct sock *sk;
72 struct vmci_transport_packet pkt;
75 static LIST_HEAD(vmci_transport_cleanup_list);
76 static DEFINE_SPINLOCK(vmci_transport_cleanup_lock);
77 static DECLARE_WORK(vmci_transport_cleanup_work, vmci_transport_cleanup);
79 static struct vmci_handle vmci_transport_stream_handle = { VMCI_INVALID_ID,
80 VMCI_INVALID_ID };
81 static u32 vmci_transport_qp_resumed_sub_id = VMCI_INVALID_ID;
83 static int PROTOCOL_OVERRIDE = -1;
85 #define VMCI_TRANSPORT_DEFAULT_QP_SIZE_MIN 128
86 #define VMCI_TRANSPORT_DEFAULT_QP_SIZE 262144
87 #define VMCI_TRANSPORT_DEFAULT_QP_SIZE_MAX 262144
89 /* The default peer timeout indicates how long we will wait for a peer response
90 * to a control message.
92 #define VSOCK_DEFAULT_CONNECT_TIMEOUT (2 * HZ)
94 /* Helper function to convert from a VMCI error code to a VSock error code. */
96 static s32 vmci_transport_error_to_vsock_error(s32 vmci_error)
98 switch (vmci_error) {
99 case VMCI_ERROR_NO_MEM:
100 return -ENOMEM;
101 case VMCI_ERROR_DUPLICATE_ENTRY:
102 case VMCI_ERROR_ALREADY_EXISTS:
103 return -EADDRINUSE;
104 case VMCI_ERROR_NO_ACCESS:
105 return -EPERM;
106 case VMCI_ERROR_NO_RESOURCES:
107 return -ENOBUFS;
108 case VMCI_ERROR_INVALID_RESOURCE:
109 return -EHOSTUNREACH;
110 case VMCI_ERROR_INVALID_ARGS:
111 default:
112 break;
114 return -EINVAL;
117 static u32 vmci_transport_peer_rid(u32 peer_cid)
119 if (VMADDR_CID_HYPERVISOR == peer_cid)
120 return VMCI_TRANSPORT_HYPERVISOR_PACKET_RID;
122 return VMCI_TRANSPORT_PACKET_RID;
125 static inline void
126 vmci_transport_packet_init(struct vmci_transport_packet *pkt,
127 struct sockaddr_vm *src,
128 struct sockaddr_vm *dst,
129 u8 type,
130 u64 size,
131 u64 mode,
132 struct vmci_transport_waiting_info *wait,
133 u16 proto,
134 struct vmci_handle handle)
136 /* We register the stream control handler as an any cid handle so we
137 * must always send from a source address of VMADDR_CID_ANY
139 pkt->dg.src = vmci_make_handle(VMADDR_CID_ANY,
140 VMCI_TRANSPORT_PACKET_RID);
141 pkt->dg.dst = vmci_make_handle(dst->svm_cid,
142 vmci_transport_peer_rid(dst->svm_cid));
143 pkt->dg.payload_size = sizeof(*pkt) - sizeof(pkt->dg);
144 pkt->version = VMCI_TRANSPORT_PACKET_VERSION;
145 pkt->type = type;
146 pkt->src_port = src->svm_port;
147 pkt->dst_port = dst->svm_port;
148 memset(&pkt->proto, 0, sizeof(pkt->proto));
149 memset(&pkt->_reserved2, 0, sizeof(pkt->_reserved2));
151 switch (pkt->type) {
152 case VMCI_TRANSPORT_PACKET_TYPE_INVALID:
153 pkt->u.size = 0;
154 break;
156 case VMCI_TRANSPORT_PACKET_TYPE_REQUEST:
157 case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE:
158 pkt->u.size = size;
159 break;
161 case VMCI_TRANSPORT_PACKET_TYPE_OFFER:
162 case VMCI_TRANSPORT_PACKET_TYPE_ATTACH:
163 pkt->u.handle = handle;
164 break;
166 case VMCI_TRANSPORT_PACKET_TYPE_WROTE:
167 case VMCI_TRANSPORT_PACKET_TYPE_READ:
168 case VMCI_TRANSPORT_PACKET_TYPE_RST:
169 pkt->u.size = 0;
170 break;
172 case VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN:
173 pkt->u.mode = mode;
174 break;
176 case VMCI_TRANSPORT_PACKET_TYPE_WAITING_READ:
177 case VMCI_TRANSPORT_PACKET_TYPE_WAITING_WRITE:
178 memcpy(&pkt->u.wait, wait, sizeof(pkt->u.wait));
179 break;
181 case VMCI_TRANSPORT_PACKET_TYPE_REQUEST2:
182 case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2:
183 pkt->u.size = size;
184 pkt->proto = proto;
185 break;
189 static inline void
190 vmci_transport_packet_get_addresses(struct vmci_transport_packet *pkt,
191 struct sockaddr_vm *local,
192 struct sockaddr_vm *remote)
194 vsock_addr_init(local, pkt->dg.dst.context, pkt->dst_port);
195 vsock_addr_init(remote, pkt->dg.src.context, pkt->src_port);
198 static int
199 __vmci_transport_send_control_pkt(struct vmci_transport_packet *pkt,
200 struct sockaddr_vm *src,
201 struct sockaddr_vm *dst,
202 enum vmci_transport_packet_type type,
203 u64 size,
204 u64 mode,
205 struct vmci_transport_waiting_info *wait,
206 u16 proto,
207 struct vmci_handle handle,
208 bool convert_error)
210 int err;
212 vmci_transport_packet_init(pkt, src, dst, type, size, mode, wait,
213 proto, handle);
214 err = vmci_datagram_send(&pkt->dg);
215 if (convert_error && (err < 0))
216 return vmci_transport_error_to_vsock_error(err);
218 return err;
221 static int
222 vmci_transport_reply_control_pkt_fast(struct vmci_transport_packet *pkt,
223 enum vmci_transport_packet_type type,
224 u64 size,
225 u64 mode,
226 struct vmci_transport_waiting_info *wait,
227 struct vmci_handle handle)
229 struct vmci_transport_packet reply;
230 struct sockaddr_vm src, dst;
232 if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_RST) {
233 return 0;
234 } else {
235 vmci_transport_packet_get_addresses(pkt, &src, &dst);
236 return __vmci_transport_send_control_pkt(&reply, &src, &dst,
237 type,
238 size, mode, wait,
239 VSOCK_PROTO_INVALID,
240 handle, true);
244 static int
245 vmci_transport_send_control_pkt_bh(struct sockaddr_vm *src,
246 struct sockaddr_vm *dst,
247 enum vmci_transport_packet_type type,
248 u64 size,
249 u64 mode,
250 struct vmci_transport_waiting_info *wait,
251 struct vmci_handle handle)
253 /* Note that it is safe to use a single packet across all CPUs since
254 * two tasklets of the same type are guaranteed to not ever run
255 * simultaneously. If that ever changes, or VMCI stops using tasklets,
256 * we can use per-cpu packets.
258 static struct vmci_transport_packet pkt;
260 return __vmci_transport_send_control_pkt(&pkt, src, dst, type,
261 size, mode, wait,
262 VSOCK_PROTO_INVALID, handle,
263 false);
266 static int
267 vmci_transport_send_control_pkt(struct sock *sk,
268 enum vmci_transport_packet_type type,
269 u64 size,
270 u64 mode,
271 struct vmci_transport_waiting_info *wait,
272 u16 proto,
273 struct vmci_handle handle)
275 struct vmci_transport_packet *pkt;
276 struct vsock_sock *vsk;
277 int err;
279 vsk = vsock_sk(sk);
281 if (!vsock_addr_bound(&vsk->local_addr))
282 return -EINVAL;
284 if (!vsock_addr_bound(&vsk->remote_addr))
285 return -EINVAL;
287 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
288 if (!pkt)
289 return -ENOMEM;
291 err = __vmci_transport_send_control_pkt(pkt, &vsk->local_addr,
292 &vsk->remote_addr, type, size,
293 mode, wait, proto, handle,
294 true);
295 kfree(pkt);
297 return err;
300 static int vmci_transport_send_reset_bh(struct sockaddr_vm *dst,
301 struct sockaddr_vm *src,
302 struct vmci_transport_packet *pkt)
304 if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_RST)
305 return 0;
306 return vmci_transport_send_control_pkt_bh(
307 dst, src,
308 VMCI_TRANSPORT_PACKET_TYPE_RST, 0,
309 0, NULL, VMCI_INVALID_HANDLE);
312 static int vmci_transport_send_reset(struct sock *sk,
313 struct vmci_transport_packet *pkt)
315 if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_RST)
316 return 0;
317 return vmci_transport_send_control_pkt(sk,
318 VMCI_TRANSPORT_PACKET_TYPE_RST,
319 0, 0, NULL, VSOCK_PROTO_INVALID,
320 VMCI_INVALID_HANDLE);
323 static int vmci_transport_send_negotiate(struct sock *sk, size_t size)
325 return vmci_transport_send_control_pkt(
327 VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE,
328 size, 0, NULL,
329 VSOCK_PROTO_INVALID,
330 VMCI_INVALID_HANDLE);
333 static int vmci_transport_send_negotiate2(struct sock *sk, size_t size,
334 u16 version)
336 return vmci_transport_send_control_pkt(
338 VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2,
339 size, 0, NULL, version,
340 VMCI_INVALID_HANDLE);
343 static int vmci_transport_send_qp_offer(struct sock *sk,
344 struct vmci_handle handle)
346 return vmci_transport_send_control_pkt(
347 sk, VMCI_TRANSPORT_PACKET_TYPE_OFFER, 0,
348 0, NULL,
349 VSOCK_PROTO_INVALID, handle);
352 static int vmci_transport_send_attach(struct sock *sk,
353 struct vmci_handle handle)
355 return vmci_transport_send_control_pkt(
356 sk, VMCI_TRANSPORT_PACKET_TYPE_ATTACH,
357 0, 0, NULL, VSOCK_PROTO_INVALID,
358 handle);
361 static int vmci_transport_reply_reset(struct vmci_transport_packet *pkt)
363 return vmci_transport_reply_control_pkt_fast(
364 pkt,
365 VMCI_TRANSPORT_PACKET_TYPE_RST,
366 0, 0, NULL,
367 VMCI_INVALID_HANDLE);
370 static int vmci_transport_send_invalid_bh(struct sockaddr_vm *dst,
371 struct sockaddr_vm *src)
373 return vmci_transport_send_control_pkt_bh(
374 dst, src,
375 VMCI_TRANSPORT_PACKET_TYPE_INVALID,
376 0, 0, NULL, VMCI_INVALID_HANDLE);
379 int vmci_transport_send_wrote_bh(struct sockaddr_vm *dst,
380 struct sockaddr_vm *src)
382 return vmci_transport_send_control_pkt_bh(
383 dst, src,
384 VMCI_TRANSPORT_PACKET_TYPE_WROTE, 0,
385 0, NULL, VMCI_INVALID_HANDLE);
388 int vmci_transport_send_read_bh(struct sockaddr_vm *dst,
389 struct sockaddr_vm *src)
391 return vmci_transport_send_control_pkt_bh(
392 dst, src,
393 VMCI_TRANSPORT_PACKET_TYPE_READ, 0,
394 0, NULL, VMCI_INVALID_HANDLE);
397 int vmci_transport_send_wrote(struct sock *sk)
399 return vmci_transport_send_control_pkt(
400 sk, VMCI_TRANSPORT_PACKET_TYPE_WROTE, 0,
401 0, NULL, VSOCK_PROTO_INVALID,
402 VMCI_INVALID_HANDLE);
405 int vmci_transport_send_read(struct sock *sk)
407 return vmci_transport_send_control_pkt(
408 sk, VMCI_TRANSPORT_PACKET_TYPE_READ, 0,
409 0, NULL, VSOCK_PROTO_INVALID,
410 VMCI_INVALID_HANDLE);
413 int vmci_transport_send_waiting_write(struct sock *sk,
414 struct vmci_transport_waiting_info *wait)
416 return vmci_transport_send_control_pkt(
417 sk, VMCI_TRANSPORT_PACKET_TYPE_WAITING_WRITE,
418 0, 0, wait, VSOCK_PROTO_INVALID,
419 VMCI_INVALID_HANDLE);
422 int vmci_transport_send_waiting_read(struct sock *sk,
423 struct vmci_transport_waiting_info *wait)
425 return vmci_transport_send_control_pkt(
426 sk, VMCI_TRANSPORT_PACKET_TYPE_WAITING_READ,
427 0, 0, wait, VSOCK_PROTO_INVALID,
428 VMCI_INVALID_HANDLE);
431 static int vmci_transport_shutdown(struct vsock_sock *vsk, int mode)
433 return vmci_transport_send_control_pkt(
434 &vsk->sk,
435 VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN,
436 0, mode, NULL,
437 VSOCK_PROTO_INVALID,
438 VMCI_INVALID_HANDLE);
441 static int vmci_transport_send_conn_request(struct sock *sk, size_t size)
443 return vmci_transport_send_control_pkt(sk,
444 VMCI_TRANSPORT_PACKET_TYPE_REQUEST,
445 size, 0, NULL,
446 VSOCK_PROTO_INVALID,
447 VMCI_INVALID_HANDLE);
450 static int vmci_transport_send_conn_request2(struct sock *sk, size_t size,
451 u16 version)
453 return vmci_transport_send_control_pkt(
454 sk, VMCI_TRANSPORT_PACKET_TYPE_REQUEST2,
455 size, 0, NULL, version,
456 VMCI_INVALID_HANDLE);
459 static struct sock *vmci_transport_get_pending(
460 struct sock *listener,
461 struct vmci_transport_packet *pkt)
463 struct vsock_sock *vlistener;
464 struct vsock_sock *vpending;
465 struct sock *pending;
466 struct sockaddr_vm src;
468 vsock_addr_init(&src, pkt->dg.src.context, pkt->src_port);
470 vlistener = vsock_sk(listener);
472 list_for_each_entry(vpending, &vlistener->pending_links,
473 pending_links) {
474 if (vsock_addr_equals_addr(&src, &vpending->remote_addr) &&
475 pkt->dst_port == vpending->local_addr.svm_port) {
476 pending = sk_vsock(vpending);
477 sock_hold(pending);
478 goto found;
482 pending = NULL;
483 found:
484 return pending;
488 static void vmci_transport_release_pending(struct sock *pending)
490 sock_put(pending);
493 /* We allow two kinds of sockets to communicate with a restricted VM: 1)
494 * trusted sockets 2) sockets from applications running as the same user as the
495 * VM (this is only true for the host side and only when using hosted products)
498 static bool vmci_transport_is_trusted(struct vsock_sock *vsock, u32 peer_cid)
500 return vsock->trusted ||
501 vmci_is_context_owner(peer_cid, vsock->owner->uid);
504 /* We allow sending datagrams to and receiving datagrams from a restricted VM
505 * only if it is trusted as described in vmci_transport_is_trusted.
508 static bool vmci_transport_allow_dgram(struct vsock_sock *vsock, u32 peer_cid)
510 if (VMADDR_CID_HYPERVISOR == peer_cid)
511 return true;
513 if (vsock->cached_peer != peer_cid) {
514 vsock->cached_peer = peer_cid;
515 if (!vmci_transport_is_trusted(vsock, peer_cid) &&
516 (vmci_context_get_priv_flags(peer_cid) &
517 VMCI_PRIVILEGE_FLAG_RESTRICTED)) {
518 vsock->cached_peer_allow_dgram = false;
519 } else {
520 vsock->cached_peer_allow_dgram = true;
524 return vsock->cached_peer_allow_dgram;
527 static int
528 vmci_transport_queue_pair_alloc(struct vmci_qp **qpair,
529 struct vmci_handle *handle,
530 u64 produce_size,
531 u64 consume_size,
532 u32 peer, u32 flags, bool trusted)
534 int err = 0;
536 if (trusted) {
537 /* Try to allocate our queue pair as trusted. This will only
538 * work if vsock is running in the host.
541 err = vmci_qpair_alloc(qpair, handle, produce_size,
542 consume_size,
543 peer, flags,
544 VMCI_PRIVILEGE_FLAG_TRUSTED);
545 if (err != VMCI_ERROR_NO_ACCESS)
546 goto out;
550 err = vmci_qpair_alloc(qpair, handle, produce_size, consume_size,
551 peer, flags, VMCI_NO_PRIVILEGE_FLAGS);
552 out:
553 if (err < 0) {
554 pr_err("Could not attach to queue pair with %d\n",
555 err);
556 err = vmci_transport_error_to_vsock_error(err);
559 return err;
562 static int
563 vmci_transport_datagram_create_hnd(u32 resource_id,
564 u32 flags,
565 vmci_datagram_recv_cb recv_cb,
566 void *client_data,
567 struct vmci_handle *out_handle)
569 int err = 0;
571 /* Try to allocate our datagram handler as trusted. This will only work
572 * if vsock is running in the host.
575 err = vmci_datagram_create_handle_priv(resource_id, flags,
576 VMCI_PRIVILEGE_FLAG_TRUSTED,
577 recv_cb,
578 client_data, out_handle);
580 if (err == VMCI_ERROR_NO_ACCESS)
581 err = vmci_datagram_create_handle(resource_id, flags,
582 recv_cb, client_data,
583 out_handle);
585 return err;
588 /* This is invoked as part of a tasklet that's scheduled when the VMCI
589 * interrupt fires. This is run in bottom-half context and if it ever needs to
590 * sleep it should defer that work to a work queue.
593 static int vmci_transport_recv_dgram_cb(void *data, struct vmci_datagram *dg)
595 struct sock *sk;
596 size_t size;
597 struct sk_buff *skb;
598 struct vsock_sock *vsk;
600 sk = (struct sock *)data;
602 /* This handler is privileged when this module is running on the host.
603 * We will get datagrams from all endpoints (even VMs that are in a
604 * restricted context). If we get one from a restricted context then
605 * the destination socket must be trusted.
607 * NOTE: We access the socket struct without holding the lock here.
608 * This is ok because the field we are interested is never modified
609 * outside of the create and destruct socket functions.
611 vsk = vsock_sk(sk);
612 if (!vmci_transport_allow_dgram(vsk, dg->src.context))
613 return VMCI_ERROR_NO_ACCESS;
615 size = VMCI_DG_SIZE(dg);
617 /* Attach the packet to the socket's receive queue as an sk_buff. */
618 skb = alloc_skb(size, GFP_ATOMIC);
619 if (!skb)
620 return VMCI_ERROR_NO_MEM;
622 /* sk_receive_skb() will do a sock_put(), so hold here. */
623 sock_hold(sk);
624 skb_put(skb, size);
625 memcpy(skb->data, dg, size);
626 sk_receive_skb(sk, skb, 0);
628 return VMCI_SUCCESS;
631 static bool vmci_transport_stream_allow(u32 cid, u32 port)
633 static const u32 non_socket_contexts[] = {
634 VMADDR_CID_RESERVED,
636 int i;
638 BUILD_BUG_ON(sizeof(cid) != sizeof(*non_socket_contexts));
640 for (i = 0; i < ARRAY_SIZE(non_socket_contexts); i++) {
641 if (cid == non_socket_contexts[i])
642 return false;
645 return true;
648 /* This is invoked as part of a tasklet that's scheduled when the VMCI
649 * interrupt fires. This is run in bottom-half context but it defers most of
650 * its work to the packet handling work queue.
653 static int vmci_transport_recv_stream_cb(void *data, struct vmci_datagram *dg)
655 struct sock *sk;
656 struct sockaddr_vm dst;
657 struct sockaddr_vm src;
658 struct vmci_transport_packet *pkt;
659 struct vsock_sock *vsk;
660 bool bh_process_pkt;
661 int err;
663 sk = NULL;
664 err = VMCI_SUCCESS;
665 bh_process_pkt = false;
667 /* Ignore incoming packets from contexts without sockets, or resources
668 * that aren't vsock implementations.
671 if (!vmci_transport_stream_allow(dg->src.context, -1)
672 || vmci_transport_peer_rid(dg->src.context) != dg->src.resource)
673 return VMCI_ERROR_NO_ACCESS;
675 if (VMCI_DG_SIZE(dg) < sizeof(*pkt))
676 /* Drop datagrams that do not contain full VSock packets. */
677 return VMCI_ERROR_INVALID_ARGS;
679 pkt = (struct vmci_transport_packet *)dg;
681 /* Find the socket that should handle this packet. First we look for a
682 * connected socket and if there is none we look for a socket bound to
683 * the destintation address.
685 vsock_addr_init(&src, pkt->dg.src.context, pkt->src_port);
686 vsock_addr_init(&dst, pkt->dg.dst.context, pkt->dst_port);
688 sk = vsock_find_connected_socket(&src, &dst);
689 if (!sk) {
690 sk = vsock_find_bound_socket(&dst);
691 if (!sk) {
692 /* We could not find a socket for this specified
693 * address. If this packet is a RST, we just drop it.
694 * If it is another packet, we send a RST. Note that
695 * we do not send a RST reply to RSTs so that we do not
696 * continually send RSTs between two endpoints.
698 * Note that since this is a reply, dst is src and src
699 * is dst.
701 if (vmci_transport_send_reset_bh(&dst, &src, pkt) < 0)
702 pr_err("unable to send reset\n");
704 err = VMCI_ERROR_NOT_FOUND;
705 goto out;
709 /* If the received packet type is beyond all types known to this
710 * implementation, reply with an invalid message. Hopefully this will
711 * help when implementing backwards compatibility in the future.
713 if (pkt->type >= VMCI_TRANSPORT_PACKET_TYPE_MAX) {
714 vmci_transport_send_invalid_bh(&dst, &src);
715 err = VMCI_ERROR_INVALID_ARGS;
716 goto out;
719 /* This handler is privileged when this module is running on the host.
720 * We will get datagram connect requests from all endpoints (even VMs
721 * that are in a restricted context). If we get one from a restricted
722 * context then the destination socket must be trusted.
724 * NOTE: We access the socket struct without holding the lock here.
725 * This is ok because the field we are interested is never modified
726 * outside of the create and destruct socket functions.
728 vsk = vsock_sk(sk);
729 if (!vmci_transport_allow_dgram(vsk, pkt->dg.src.context)) {
730 err = VMCI_ERROR_NO_ACCESS;
731 goto out;
734 /* We do most everything in a work queue, but let's fast path the
735 * notification of reads and writes to help data transfer performance.
736 * We can only do this if there is no process context code executing
737 * for this socket since that may change the state.
739 bh_lock_sock(sk);
741 if (!sock_owned_by_user(sk)) {
742 /* The local context ID may be out of date, update it. */
743 vsk->local_addr.svm_cid = dst.svm_cid;
745 if (sk->sk_state == TCP_ESTABLISHED)
746 vmci_trans(vsk)->notify_ops->handle_notify_pkt(
747 sk, pkt, true, &dst, &src,
748 &bh_process_pkt);
751 bh_unlock_sock(sk);
753 if (!bh_process_pkt) {
754 struct vmci_transport_recv_pkt_info *recv_pkt_info;
756 recv_pkt_info = kmalloc(sizeof(*recv_pkt_info), GFP_ATOMIC);
757 if (!recv_pkt_info) {
758 if (vmci_transport_send_reset_bh(&dst, &src, pkt) < 0)
759 pr_err("unable to send reset\n");
761 err = VMCI_ERROR_NO_MEM;
762 goto out;
765 recv_pkt_info->sk = sk;
766 memcpy(&recv_pkt_info->pkt, pkt, sizeof(recv_pkt_info->pkt));
767 INIT_WORK(&recv_pkt_info->work, vmci_transport_recv_pkt_work);
769 schedule_work(&recv_pkt_info->work);
770 /* Clear sk so that the reference count incremented by one of
771 * the Find functions above is not decremented below. We need
772 * that reference count for the packet handler we've scheduled
773 * to run.
775 sk = NULL;
778 out:
779 if (sk)
780 sock_put(sk);
782 return err;
785 static void vmci_transport_handle_detach(struct sock *sk)
787 struct vsock_sock *vsk;
789 vsk = vsock_sk(sk);
790 if (!vmci_handle_is_invalid(vmci_trans(vsk)->qp_handle)) {
791 sock_set_flag(sk, SOCK_DONE);
793 /* On a detach the peer will not be sending or receiving
794 * anymore.
796 vsk->peer_shutdown = SHUTDOWN_MASK;
798 /* We should not be sending anymore since the peer won't be
799 * there to receive, but we can still receive if there is data
800 * left in our consume queue. If the local endpoint is a host,
801 * we can't call vsock_stream_has_data, since that may block,
802 * but a host endpoint can't read data once the VM has
803 * detached, so there is no available data in that case.
805 if (vsk->local_addr.svm_cid == VMADDR_CID_HOST ||
806 vsock_stream_has_data(vsk) <= 0) {
807 if (sk->sk_state == TCP_SYN_SENT) {
808 /* The peer may detach from a queue pair while
809 * we are still in the connecting state, i.e.,
810 * if the peer VM is killed after attaching to
811 * a queue pair, but before we complete the
812 * handshake. In that case, we treat the detach
813 * event like a reset.
816 sk->sk_state = TCP_CLOSE;
817 sk->sk_err = ECONNRESET;
818 sk->sk_error_report(sk);
819 return;
821 sk->sk_state = TCP_CLOSE;
823 sk->sk_state_change(sk);
827 static void vmci_transport_peer_detach_cb(u32 sub_id,
828 const struct vmci_event_data *e_data,
829 void *client_data)
831 struct vmci_transport *trans = client_data;
832 const struct vmci_event_payload_qp *e_payload;
834 e_payload = vmci_event_data_const_payload(e_data);
836 /* XXX This is lame, we should provide a way to lookup sockets by
837 * qp_handle.
839 if (vmci_handle_is_invalid(e_payload->handle) ||
840 !vmci_handle_is_equal(trans->qp_handle, e_payload->handle))
841 return;
843 /* We don't ask for delayed CBs when we subscribe to this event (we
844 * pass 0 as flags to vmci_event_subscribe()). VMCI makes no
845 * guarantees in that case about what context we might be running in,
846 * so it could be BH or process, blockable or non-blockable. So we
847 * need to account for all possible contexts here.
849 spin_lock_bh(&trans->lock);
850 if (!trans->sk)
851 goto out;
853 /* Apart from here, trans->lock is only grabbed as part of sk destruct,
854 * where trans->sk isn't locked.
856 bh_lock_sock(trans->sk);
858 vmci_transport_handle_detach(trans->sk);
860 bh_unlock_sock(trans->sk);
861 out:
862 spin_unlock_bh(&trans->lock);
865 static void vmci_transport_qp_resumed_cb(u32 sub_id,
866 const struct vmci_event_data *e_data,
867 void *client_data)
869 vsock_for_each_connected_socket(vmci_transport_handle_detach);
872 static void vmci_transport_recv_pkt_work(struct work_struct *work)
874 struct vmci_transport_recv_pkt_info *recv_pkt_info;
875 struct vmci_transport_packet *pkt;
876 struct sock *sk;
878 recv_pkt_info =
879 container_of(work, struct vmci_transport_recv_pkt_info, work);
880 sk = recv_pkt_info->sk;
881 pkt = &recv_pkt_info->pkt;
883 lock_sock(sk);
885 /* The local context ID may be out of date. */
886 vsock_sk(sk)->local_addr.svm_cid = pkt->dg.dst.context;
888 switch (sk->sk_state) {
889 case TCP_LISTEN:
890 vmci_transport_recv_listen(sk, pkt);
891 break;
892 case TCP_SYN_SENT:
893 /* Processing of pending connections for servers goes through
894 * the listening socket, so see vmci_transport_recv_listen()
895 * for that path.
897 vmci_transport_recv_connecting_client(sk, pkt);
898 break;
899 case TCP_ESTABLISHED:
900 vmci_transport_recv_connected(sk, pkt);
901 break;
902 default:
903 /* Because this function does not run in the same context as
904 * vmci_transport_recv_stream_cb it is possible that the
905 * socket has closed. We need to let the other side know or it
906 * could be sitting in a connect and hang forever. Send a
907 * reset to prevent that.
909 vmci_transport_send_reset(sk, pkt);
910 break;
913 release_sock(sk);
914 kfree(recv_pkt_info);
915 /* Release reference obtained in the stream callback when we fetched
916 * this socket out of the bound or connected list.
918 sock_put(sk);
921 static int vmci_transport_recv_listen(struct sock *sk,
922 struct vmci_transport_packet *pkt)
924 struct sock *pending;
925 struct vsock_sock *vpending;
926 int err;
927 u64 qp_size;
928 bool old_request = false;
929 bool old_pkt_proto = false;
931 err = 0;
933 /* Because we are in the listen state, we could be receiving a packet
934 * for ourself or any previous connection requests that we received.
935 * If it's the latter, we try to find a socket in our list of pending
936 * connections and, if we do, call the appropriate handler for the
937 * state that that socket is in. Otherwise we try to service the
938 * connection request.
940 pending = vmci_transport_get_pending(sk, pkt);
941 if (pending) {
942 lock_sock(pending);
944 /* The local context ID may be out of date. */
945 vsock_sk(pending)->local_addr.svm_cid = pkt->dg.dst.context;
947 switch (pending->sk_state) {
948 case TCP_SYN_SENT:
949 err = vmci_transport_recv_connecting_server(sk,
950 pending,
951 pkt);
952 break;
953 default:
954 vmci_transport_send_reset(pending, pkt);
955 err = -EINVAL;
958 if (err < 0)
959 vsock_remove_pending(sk, pending);
961 release_sock(pending);
962 vmci_transport_release_pending(pending);
964 return err;
967 /* The listen state only accepts connection requests. Reply with a
968 * reset unless we received a reset.
971 if (!(pkt->type == VMCI_TRANSPORT_PACKET_TYPE_REQUEST ||
972 pkt->type == VMCI_TRANSPORT_PACKET_TYPE_REQUEST2)) {
973 vmci_transport_reply_reset(pkt);
974 return -EINVAL;
977 if (pkt->u.size == 0) {
978 vmci_transport_reply_reset(pkt);
979 return -EINVAL;
982 /* If this socket can't accommodate this connection request, we send a
983 * reset. Otherwise we create and initialize a child socket and reply
984 * with a connection negotiation.
986 if (sk->sk_ack_backlog >= sk->sk_max_ack_backlog) {
987 vmci_transport_reply_reset(pkt);
988 return -ECONNREFUSED;
991 pending = __vsock_create(sock_net(sk), NULL, sk, GFP_KERNEL,
992 sk->sk_type, 0);
993 if (!pending) {
994 vmci_transport_send_reset(sk, pkt);
995 return -ENOMEM;
998 vpending = vsock_sk(pending);
1000 vsock_addr_init(&vpending->local_addr, pkt->dg.dst.context,
1001 pkt->dst_port);
1002 vsock_addr_init(&vpending->remote_addr, pkt->dg.src.context,
1003 pkt->src_port);
1005 /* If the proposed size fits within our min/max, accept it. Otherwise
1006 * propose our own size.
1008 if (pkt->u.size >= vmci_trans(vpending)->queue_pair_min_size &&
1009 pkt->u.size <= vmci_trans(vpending)->queue_pair_max_size) {
1010 qp_size = pkt->u.size;
1011 } else {
1012 qp_size = vmci_trans(vpending)->queue_pair_size;
1015 /* Figure out if we are using old or new requests based on the
1016 * overrides pkt types sent by our peer.
1018 if (vmci_transport_old_proto_override(&old_pkt_proto)) {
1019 old_request = old_pkt_proto;
1020 } else {
1021 if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_REQUEST)
1022 old_request = true;
1023 else if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_REQUEST2)
1024 old_request = false;
1028 if (old_request) {
1029 /* Handle a REQUEST (or override) */
1030 u16 version = VSOCK_PROTO_INVALID;
1031 if (vmci_transport_proto_to_notify_struct(
1032 pending, &version, true))
1033 err = vmci_transport_send_negotiate(pending, qp_size);
1034 else
1035 err = -EINVAL;
1037 } else {
1038 /* Handle a REQUEST2 (or override) */
1039 int proto_int = pkt->proto;
1040 int pos;
1041 u16 active_proto_version = 0;
1043 /* The list of possible protocols is the intersection of all
1044 * protocols the client supports ... plus all the protocols we
1045 * support.
1047 proto_int &= vmci_transport_new_proto_supported_versions();
1049 /* We choose the highest possible protocol version and use that
1050 * one.
1052 pos = fls(proto_int);
1053 if (pos) {
1054 active_proto_version = (1 << (pos - 1));
1055 if (vmci_transport_proto_to_notify_struct(
1056 pending, &active_proto_version, false))
1057 err = vmci_transport_send_negotiate2(pending,
1058 qp_size,
1059 active_proto_version);
1060 else
1061 err = -EINVAL;
1063 } else {
1064 err = -EINVAL;
1068 if (err < 0) {
1069 vmci_transport_send_reset(sk, pkt);
1070 sock_put(pending);
1071 err = vmci_transport_error_to_vsock_error(err);
1072 goto out;
1075 vsock_add_pending(sk, pending);
1076 sk->sk_ack_backlog++;
1078 pending->sk_state = TCP_SYN_SENT;
1079 vmci_trans(vpending)->produce_size =
1080 vmci_trans(vpending)->consume_size = qp_size;
1081 vmci_trans(vpending)->queue_pair_size = qp_size;
1083 vmci_trans(vpending)->notify_ops->process_request(pending);
1085 /* We might never receive another message for this socket and it's not
1086 * connected to any process, so we have to ensure it gets cleaned up
1087 * ourself. Our delayed work function will take care of that. Note
1088 * that we do not ever cancel this function since we have few
1089 * guarantees about its state when calling cancel_delayed_work().
1090 * Instead we hold a reference on the socket for that function and make
1091 * it capable of handling cases where it needs to do nothing but
1092 * release that reference.
1094 vpending->listener = sk;
1095 sock_hold(sk);
1096 sock_hold(pending);
1097 schedule_delayed_work(&vpending->pending_work, HZ);
1099 out:
1100 return err;
1103 static int
1104 vmci_transport_recv_connecting_server(struct sock *listener,
1105 struct sock *pending,
1106 struct vmci_transport_packet *pkt)
1108 struct vsock_sock *vpending;
1109 struct vmci_handle handle;
1110 struct vmci_qp *qpair;
1111 bool is_local;
1112 u32 flags;
1113 u32 detach_sub_id;
1114 int err;
1115 int skerr;
1117 vpending = vsock_sk(pending);
1118 detach_sub_id = VMCI_INVALID_ID;
1120 switch (pkt->type) {
1121 case VMCI_TRANSPORT_PACKET_TYPE_OFFER:
1122 if (vmci_handle_is_invalid(pkt->u.handle)) {
1123 vmci_transport_send_reset(pending, pkt);
1124 skerr = EPROTO;
1125 err = -EINVAL;
1126 goto destroy;
1128 break;
1129 default:
1130 /* Close and cleanup the connection. */
1131 vmci_transport_send_reset(pending, pkt);
1132 skerr = EPROTO;
1133 err = pkt->type == VMCI_TRANSPORT_PACKET_TYPE_RST ? 0 : -EINVAL;
1134 goto destroy;
1137 /* In order to complete the connection we need to attach to the offered
1138 * queue pair and send an attach notification. We also subscribe to the
1139 * detach event so we know when our peer goes away, and we do that
1140 * before attaching so we don't miss an event. If all this succeeds,
1141 * we update our state and wakeup anything waiting in accept() for a
1142 * connection.
1145 /* We don't care about attach since we ensure the other side has
1146 * attached by specifying the ATTACH_ONLY flag below.
1148 err = vmci_event_subscribe(VMCI_EVENT_QP_PEER_DETACH,
1149 vmci_transport_peer_detach_cb,
1150 vmci_trans(vpending), &detach_sub_id);
1151 if (err < VMCI_SUCCESS) {
1152 vmci_transport_send_reset(pending, pkt);
1153 err = vmci_transport_error_to_vsock_error(err);
1154 skerr = -err;
1155 goto destroy;
1158 vmci_trans(vpending)->detach_sub_id = detach_sub_id;
1160 /* Now attach to the queue pair the client created. */
1161 handle = pkt->u.handle;
1163 /* vpending->local_addr always has a context id so we do not need to
1164 * worry about VMADDR_CID_ANY in this case.
1166 is_local =
1167 vpending->remote_addr.svm_cid == vpending->local_addr.svm_cid;
1168 flags = VMCI_QPFLAG_ATTACH_ONLY;
1169 flags |= is_local ? VMCI_QPFLAG_LOCAL : 0;
1171 err = vmci_transport_queue_pair_alloc(
1172 &qpair,
1173 &handle,
1174 vmci_trans(vpending)->produce_size,
1175 vmci_trans(vpending)->consume_size,
1176 pkt->dg.src.context,
1177 flags,
1178 vmci_transport_is_trusted(
1179 vpending,
1180 vpending->remote_addr.svm_cid));
1181 if (err < 0) {
1182 vmci_transport_send_reset(pending, pkt);
1183 skerr = -err;
1184 goto destroy;
1187 vmci_trans(vpending)->qp_handle = handle;
1188 vmci_trans(vpending)->qpair = qpair;
1190 /* When we send the attach message, we must be ready to handle incoming
1191 * control messages on the newly connected socket. So we move the
1192 * pending socket to the connected state before sending the attach
1193 * message. Otherwise, an incoming packet triggered by the attach being
1194 * received by the peer may be processed concurrently with what happens
1195 * below after sending the attach message, and that incoming packet
1196 * will find the listening socket instead of the (currently) pending
1197 * socket. Note that enqueueing the socket increments the reference
1198 * count, so even if a reset comes before the connection is accepted,
1199 * the socket will be valid until it is removed from the queue.
1201 * If we fail sending the attach below, we remove the socket from the
1202 * connected list and move the socket to TCP_CLOSE before
1203 * releasing the lock, so a pending slow path processing of an incoming
1204 * packet will not see the socket in the connected state in that case.
1206 pending->sk_state = TCP_ESTABLISHED;
1208 vsock_insert_connected(vpending);
1210 /* Notify our peer of our attach. */
1211 err = vmci_transport_send_attach(pending, handle);
1212 if (err < 0) {
1213 vsock_remove_connected(vpending);
1214 pr_err("Could not send attach\n");
1215 vmci_transport_send_reset(pending, pkt);
1216 err = vmci_transport_error_to_vsock_error(err);
1217 skerr = -err;
1218 goto destroy;
1221 /* We have a connection. Move the now connected socket from the
1222 * listener's pending list to the accept queue so callers of accept()
1223 * can find it.
1225 vsock_remove_pending(listener, pending);
1226 vsock_enqueue_accept(listener, pending);
1228 /* Callers of accept() will be be waiting on the listening socket, not
1229 * the pending socket.
1231 listener->sk_data_ready(listener);
1233 return 0;
1235 destroy:
1236 pending->sk_err = skerr;
1237 pending->sk_state = TCP_CLOSE;
1238 /* As long as we drop our reference, all necessary cleanup will handle
1239 * when the cleanup function drops its reference and our destruct
1240 * implementation is called. Note that since the listen handler will
1241 * remove pending from the pending list upon our failure, the cleanup
1242 * function won't drop the additional reference, which is why we do it
1243 * here.
1245 sock_put(pending);
1247 return err;
1250 static int
1251 vmci_transport_recv_connecting_client(struct sock *sk,
1252 struct vmci_transport_packet *pkt)
1254 struct vsock_sock *vsk;
1255 int err;
1256 int skerr;
1258 vsk = vsock_sk(sk);
1260 switch (pkt->type) {
1261 case VMCI_TRANSPORT_PACKET_TYPE_ATTACH:
1262 if (vmci_handle_is_invalid(pkt->u.handle) ||
1263 !vmci_handle_is_equal(pkt->u.handle,
1264 vmci_trans(vsk)->qp_handle)) {
1265 skerr = EPROTO;
1266 err = -EINVAL;
1267 goto destroy;
1270 /* Signify the socket is connected and wakeup the waiter in
1271 * connect(). Also place the socket in the connected table for
1272 * accounting (it can already be found since it's in the bound
1273 * table).
1275 sk->sk_state = TCP_ESTABLISHED;
1276 sk->sk_socket->state = SS_CONNECTED;
1277 vsock_insert_connected(vsk);
1278 sk->sk_state_change(sk);
1280 break;
1281 case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE:
1282 case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2:
1283 if (pkt->u.size == 0
1284 || pkt->dg.src.context != vsk->remote_addr.svm_cid
1285 || pkt->src_port != vsk->remote_addr.svm_port
1286 || !vmci_handle_is_invalid(vmci_trans(vsk)->qp_handle)
1287 || vmci_trans(vsk)->qpair
1288 || vmci_trans(vsk)->produce_size != 0
1289 || vmci_trans(vsk)->consume_size != 0
1290 || vmci_trans(vsk)->detach_sub_id != VMCI_INVALID_ID) {
1291 skerr = EPROTO;
1292 err = -EINVAL;
1294 goto destroy;
1297 err = vmci_transport_recv_connecting_client_negotiate(sk, pkt);
1298 if (err) {
1299 skerr = -err;
1300 goto destroy;
1303 break;
1304 case VMCI_TRANSPORT_PACKET_TYPE_INVALID:
1305 err = vmci_transport_recv_connecting_client_invalid(sk, pkt);
1306 if (err) {
1307 skerr = -err;
1308 goto destroy;
1311 break;
1312 case VMCI_TRANSPORT_PACKET_TYPE_RST:
1313 /* Older versions of the linux code (WS 6.5 / ESX 4.0) used to
1314 * continue processing here after they sent an INVALID packet.
1315 * This meant that we got a RST after the INVALID. We ignore a
1316 * RST after an INVALID. The common code doesn't send the RST
1317 * ... so we can hang if an old version of the common code
1318 * fails between getting a REQUEST and sending an OFFER back.
1319 * Not much we can do about it... except hope that it doesn't
1320 * happen.
1322 if (vsk->ignore_connecting_rst) {
1323 vsk->ignore_connecting_rst = false;
1324 } else {
1325 skerr = ECONNRESET;
1326 err = 0;
1327 goto destroy;
1330 break;
1331 default:
1332 /* Close and cleanup the connection. */
1333 skerr = EPROTO;
1334 err = -EINVAL;
1335 goto destroy;
1338 return 0;
1340 destroy:
1341 vmci_transport_send_reset(sk, pkt);
1343 sk->sk_state = TCP_CLOSE;
1344 sk->sk_err = skerr;
1345 sk->sk_error_report(sk);
1346 return err;
1349 static int vmci_transport_recv_connecting_client_negotiate(
1350 struct sock *sk,
1351 struct vmci_transport_packet *pkt)
1353 int err;
1354 struct vsock_sock *vsk;
1355 struct vmci_handle handle;
1356 struct vmci_qp *qpair;
1357 u32 detach_sub_id;
1358 bool is_local;
1359 u32 flags;
1360 bool old_proto = true;
1361 bool old_pkt_proto;
1362 u16 version;
1364 vsk = vsock_sk(sk);
1365 handle = VMCI_INVALID_HANDLE;
1366 detach_sub_id = VMCI_INVALID_ID;
1368 /* If we have gotten here then we should be past the point where old
1369 * linux vsock could have sent the bogus rst.
1371 vsk->sent_request = false;
1372 vsk->ignore_connecting_rst = false;
1374 /* Verify that we're OK with the proposed queue pair size */
1375 if (pkt->u.size < vmci_trans(vsk)->queue_pair_min_size ||
1376 pkt->u.size > vmci_trans(vsk)->queue_pair_max_size) {
1377 err = -EINVAL;
1378 goto destroy;
1381 /* At this point we know the CID the peer is using to talk to us. */
1383 if (vsk->local_addr.svm_cid == VMADDR_CID_ANY)
1384 vsk->local_addr.svm_cid = pkt->dg.dst.context;
1386 /* Setup the notify ops to be the highest supported version that both
1387 * the server and the client support.
1390 if (vmci_transport_old_proto_override(&old_pkt_proto)) {
1391 old_proto = old_pkt_proto;
1392 } else {
1393 if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE)
1394 old_proto = true;
1395 else if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2)
1396 old_proto = false;
1400 if (old_proto)
1401 version = VSOCK_PROTO_INVALID;
1402 else
1403 version = pkt->proto;
1405 if (!vmci_transport_proto_to_notify_struct(sk, &version, old_proto)) {
1406 err = -EINVAL;
1407 goto destroy;
1410 /* Subscribe to detach events first.
1412 * XXX We attach once for each queue pair created for now so it is easy
1413 * to find the socket (it's provided), but later we should only
1414 * subscribe once and add a way to lookup sockets by queue pair handle.
1416 err = vmci_event_subscribe(VMCI_EVENT_QP_PEER_DETACH,
1417 vmci_transport_peer_detach_cb,
1418 vmci_trans(vsk), &detach_sub_id);
1419 if (err < VMCI_SUCCESS) {
1420 err = vmci_transport_error_to_vsock_error(err);
1421 goto destroy;
1424 /* Make VMCI select the handle for us. */
1425 handle = VMCI_INVALID_HANDLE;
1426 is_local = vsk->remote_addr.svm_cid == vsk->local_addr.svm_cid;
1427 flags = is_local ? VMCI_QPFLAG_LOCAL : 0;
1429 err = vmci_transport_queue_pair_alloc(&qpair,
1430 &handle,
1431 pkt->u.size,
1432 pkt->u.size,
1433 vsk->remote_addr.svm_cid,
1434 flags,
1435 vmci_transport_is_trusted(
1436 vsk,
1437 vsk->
1438 remote_addr.svm_cid));
1439 if (err < 0)
1440 goto destroy;
1442 err = vmci_transport_send_qp_offer(sk, handle);
1443 if (err < 0) {
1444 err = vmci_transport_error_to_vsock_error(err);
1445 goto destroy;
1448 vmci_trans(vsk)->qp_handle = handle;
1449 vmci_trans(vsk)->qpair = qpair;
1451 vmci_trans(vsk)->produce_size = vmci_trans(vsk)->consume_size =
1452 pkt->u.size;
1454 vmci_trans(vsk)->detach_sub_id = detach_sub_id;
1456 vmci_trans(vsk)->notify_ops->process_negotiate(sk);
1458 return 0;
1460 destroy:
1461 if (detach_sub_id != VMCI_INVALID_ID)
1462 vmci_event_unsubscribe(detach_sub_id);
1464 if (!vmci_handle_is_invalid(handle))
1465 vmci_qpair_detach(&qpair);
1467 return err;
1470 static int
1471 vmci_transport_recv_connecting_client_invalid(struct sock *sk,
1472 struct vmci_transport_packet *pkt)
1474 int err = 0;
1475 struct vsock_sock *vsk = vsock_sk(sk);
1477 if (vsk->sent_request) {
1478 vsk->sent_request = false;
1479 vsk->ignore_connecting_rst = true;
1481 err = vmci_transport_send_conn_request(
1482 sk, vmci_trans(vsk)->queue_pair_size);
1483 if (err < 0)
1484 err = vmci_transport_error_to_vsock_error(err);
1485 else
1486 err = 0;
1490 return err;
1493 static int vmci_transport_recv_connected(struct sock *sk,
1494 struct vmci_transport_packet *pkt)
1496 struct vsock_sock *vsk;
1497 bool pkt_processed = false;
1499 /* In cases where we are closing the connection, it's sufficient to
1500 * mark the state change (and maybe error) and wake up any waiting
1501 * threads. Since this is a connected socket, it's owned by a user
1502 * process and will be cleaned up when the failure is passed back on
1503 * the current or next system call. Our system call implementations
1504 * must therefore check for error and state changes on entry and when
1505 * being awoken.
1507 switch (pkt->type) {
1508 case VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN:
1509 if (pkt->u.mode) {
1510 vsk = vsock_sk(sk);
1512 vsk->peer_shutdown |= pkt->u.mode;
1513 sk->sk_state_change(sk);
1515 break;
1517 case VMCI_TRANSPORT_PACKET_TYPE_RST:
1518 vsk = vsock_sk(sk);
1519 /* It is possible that we sent our peer a message (e.g a
1520 * WAITING_READ) right before we got notified that the peer had
1521 * detached. If that happens then we can get a RST pkt back
1522 * from our peer even though there is data available for us to
1523 * read. In that case, don't shutdown the socket completely but
1524 * instead allow the local client to finish reading data off
1525 * the queuepair. Always treat a RST pkt in connected mode like
1526 * a clean shutdown.
1528 sock_set_flag(sk, SOCK_DONE);
1529 vsk->peer_shutdown = SHUTDOWN_MASK;
1530 if (vsock_stream_has_data(vsk) <= 0)
1531 sk->sk_state = TCP_CLOSING;
1533 sk->sk_state_change(sk);
1534 break;
1536 default:
1537 vsk = vsock_sk(sk);
1538 vmci_trans(vsk)->notify_ops->handle_notify_pkt(
1539 sk, pkt, false, NULL, NULL,
1540 &pkt_processed);
1541 if (!pkt_processed)
1542 return -EINVAL;
1544 break;
1547 return 0;
1550 static int vmci_transport_socket_init(struct vsock_sock *vsk,
1551 struct vsock_sock *psk)
1553 vsk->trans = kmalloc(sizeof(struct vmci_transport), GFP_KERNEL);
1554 if (!vsk->trans)
1555 return -ENOMEM;
1557 vmci_trans(vsk)->dg_handle = VMCI_INVALID_HANDLE;
1558 vmci_trans(vsk)->qp_handle = VMCI_INVALID_HANDLE;
1559 vmci_trans(vsk)->qpair = NULL;
1560 vmci_trans(vsk)->produce_size = vmci_trans(vsk)->consume_size = 0;
1561 vmci_trans(vsk)->detach_sub_id = VMCI_INVALID_ID;
1562 vmci_trans(vsk)->notify_ops = NULL;
1563 INIT_LIST_HEAD(&vmci_trans(vsk)->elem);
1564 vmci_trans(vsk)->sk = &vsk->sk;
1565 spin_lock_init(&vmci_trans(vsk)->lock);
1566 if (psk) {
1567 vmci_trans(vsk)->queue_pair_size =
1568 vmci_trans(psk)->queue_pair_size;
1569 vmci_trans(vsk)->queue_pair_min_size =
1570 vmci_trans(psk)->queue_pair_min_size;
1571 vmci_trans(vsk)->queue_pair_max_size =
1572 vmci_trans(psk)->queue_pair_max_size;
1573 } else {
1574 vmci_trans(vsk)->queue_pair_size =
1575 VMCI_TRANSPORT_DEFAULT_QP_SIZE;
1576 vmci_trans(vsk)->queue_pair_min_size =
1577 VMCI_TRANSPORT_DEFAULT_QP_SIZE_MIN;
1578 vmci_trans(vsk)->queue_pair_max_size =
1579 VMCI_TRANSPORT_DEFAULT_QP_SIZE_MAX;
1582 return 0;
1585 static void vmci_transport_free_resources(struct list_head *transport_list)
1587 while (!list_empty(transport_list)) {
1588 struct vmci_transport *transport =
1589 list_first_entry(transport_list, struct vmci_transport,
1590 elem);
1591 list_del(&transport->elem);
1593 if (transport->detach_sub_id != VMCI_INVALID_ID) {
1594 vmci_event_unsubscribe(transport->detach_sub_id);
1595 transport->detach_sub_id = VMCI_INVALID_ID;
1598 if (!vmci_handle_is_invalid(transport->qp_handle)) {
1599 vmci_qpair_detach(&transport->qpair);
1600 transport->qp_handle = VMCI_INVALID_HANDLE;
1601 transport->produce_size = 0;
1602 transport->consume_size = 0;
1605 kfree(transport);
1609 static void vmci_transport_cleanup(struct work_struct *work)
1611 LIST_HEAD(pending);
1613 spin_lock_bh(&vmci_transport_cleanup_lock);
1614 list_replace_init(&vmci_transport_cleanup_list, &pending);
1615 spin_unlock_bh(&vmci_transport_cleanup_lock);
1616 vmci_transport_free_resources(&pending);
1619 static void vmci_transport_destruct(struct vsock_sock *vsk)
1621 /* Ensure that the detach callback doesn't use the sk/vsk
1622 * we are about to destruct.
1624 spin_lock_bh(&vmci_trans(vsk)->lock);
1625 vmci_trans(vsk)->sk = NULL;
1626 spin_unlock_bh(&vmci_trans(vsk)->lock);
1628 if (vmci_trans(vsk)->notify_ops)
1629 vmci_trans(vsk)->notify_ops->socket_destruct(vsk);
1631 spin_lock_bh(&vmci_transport_cleanup_lock);
1632 list_add(&vmci_trans(vsk)->elem, &vmci_transport_cleanup_list);
1633 spin_unlock_bh(&vmci_transport_cleanup_lock);
1634 schedule_work(&vmci_transport_cleanup_work);
1636 vsk->trans = NULL;
1639 static void vmci_transport_release(struct vsock_sock *vsk)
1641 vsock_remove_sock(vsk);
1643 if (!vmci_handle_is_invalid(vmci_trans(vsk)->dg_handle)) {
1644 vmci_datagram_destroy_handle(vmci_trans(vsk)->dg_handle);
1645 vmci_trans(vsk)->dg_handle = VMCI_INVALID_HANDLE;
1649 static int vmci_transport_dgram_bind(struct vsock_sock *vsk,
1650 struct sockaddr_vm *addr)
1652 u32 port;
1653 u32 flags;
1654 int err;
1656 /* VMCI will select a resource ID for us if we provide
1657 * VMCI_INVALID_ID.
1659 port = addr->svm_port == VMADDR_PORT_ANY ?
1660 VMCI_INVALID_ID : addr->svm_port;
1662 if (port <= LAST_RESERVED_PORT && !capable(CAP_NET_BIND_SERVICE))
1663 return -EACCES;
1665 flags = addr->svm_cid == VMADDR_CID_ANY ?
1666 VMCI_FLAG_ANYCID_DG_HND : 0;
1668 err = vmci_transport_datagram_create_hnd(port, flags,
1669 vmci_transport_recv_dgram_cb,
1670 &vsk->sk,
1671 &vmci_trans(vsk)->dg_handle);
1672 if (err < VMCI_SUCCESS)
1673 return vmci_transport_error_to_vsock_error(err);
1674 vsock_addr_init(&vsk->local_addr, addr->svm_cid,
1675 vmci_trans(vsk)->dg_handle.resource);
1677 return 0;
1680 static int vmci_transport_dgram_enqueue(
1681 struct vsock_sock *vsk,
1682 struct sockaddr_vm *remote_addr,
1683 struct msghdr *msg,
1684 size_t len)
1686 int err;
1687 struct vmci_datagram *dg;
1689 if (len > VMCI_MAX_DG_PAYLOAD_SIZE)
1690 return -EMSGSIZE;
1692 if (!vmci_transport_allow_dgram(vsk, remote_addr->svm_cid))
1693 return -EPERM;
1695 /* Allocate a buffer for the user's message and our packet header. */
1696 dg = kmalloc(len + sizeof(*dg), GFP_KERNEL);
1697 if (!dg)
1698 return -ENOMEM;
1700 memcpy_from_msg(VMCI_DG_PAYLOAD(dg), msg, len);
1702 dg->dst = vmci_make_handle(remote_addr->svm_cid,
1703 remote_addr->svm_port);
1704 dg->src = vmci_make_handle(vsk->local_addr.svm_cid,
1705 vsk->local_addr.svm_port);
1706 dg->payload_size = len;
1708 err = vmci_datagram_send(dg);
1709 kfree(dg);
1710 if (err < 0)
1711 return vmci_transport_error_to_vsock_error(err);
1713 return err - sizeof(*dg);
1716 static int vmci_transport_dgram_dequeue(struct vsock_sock *vsk,
1717 struct msghdr *msg, size_t len,
1718 int flags)
1720 int err;
1721 int noblock;
1722 struct vmci_datagram *dg;
1723 size_t payload_len;
1724 struct sk_buff *skb;
1726 noblock = flags & MSG_DONTWAIT;
1728 if (flags & MSG_OOB || flags & MSG_ERRQUEUE)
1729 return -EOPNOTSUPP;
1731 /* Retrieve the head sk_buff from the socket's receive queue. */
1732 err = 0;
1733 skb = skb_recv_datagram(&vsk->sk, flags, noblock, &err);
1734 if (!skb)
1735 return err;
1737 dg = (struct vmci_datagram *)skb->data;
1738 if (!dg)
1739 /* err is 0, meaning we read zero bytes. */
1740 goto out;
1742 payload_len = dg->payload_size;
1743 /* Ensure the sk_buff matches the payload size claimed in the packet. */
1744 if (payload_len != skb->len - sizeof(*dg)) {
1745 err = -EINVAL;
1746 goto out;
1749 if (payload_len > len) {
1750 payload_len = len;
1751 msg->msg_flags |= MSG_TRUNC;
1754 /* Place the datagram payload in the user's iovec. */
1755 err = skb_copy_datagram_msg(skb, sizeof(*dg), msg, payload_len);
1756 if (err)
1757 goto out;
1759 if (msg->msg_name) {
1760 /* Provide the address of the sender. */
1761 DECLARE_SOCKADDR(struct sockaddr_vm *, vm_addr, msg->msg_name);
1762 vsock_addr_init(vm_addr, dg->src.context, dg->src.resource);
1763 msg->msg_namelen = sizeof(*vm_addr);
1765 err = payload_len;
1767 out:
1768 skb_free_datagram(&vsk->sk, skb);
1769 return err;
1772 static bool vmci_transport_dgram_allow(u32 cid, u32 port)
1774 if (cid == VMADDR_CID_HYPERVISOR) {
1775 /* Registrations of PBRPC Servers do not modify VMX/Hypervisor
1776 * state and are allowed.
1778 return port == VMCI_UNITY_PBRPC_REGISTER;
1781 return true;
1784 static int vmci_transport_connect(struct vsock_sock *vsk)
1786 int err;
1787 bool old_pkt_proto = false;
1788 struct sock *sk = &vsk->sk;
1790 if (vmci_transport_old_proto_override(&old_pkt_proto) &&
1791 old_pkt_proto) {
1792 err = vmci_transport_send_conn_request(
1793 sk, vmci_trans(vsk)->queue_pair_size);
1794 if (err < 0) {
1795 sk->sk_state = TCP_CLOSE;
1796 return err;
1798 } else {
1799 int supported_proto_versions =
1800 vmci_transport_new_proto_supported_versions();
1801 err = vmci_transport_send_conn_request2(
1802 sk, vmci_trans(vsk)->queue_pair_size,
1803 supported_proto_versions);
1804 if (err < 0) {
1805 sk->sk_state = TCP_CLOSE;
1806 return err;
1809 vsk->sent_request = true;
1812 return err;
1815 static ssize_t vmci_transport_stream_dequeue(
1816 struct vsock_sock *vsk,
1817 struct msghdr *msg,
1818 size_t len,
1819 int flags)
1821 if (flags & MSG_PEEK)
1822 return vmci_qpair_peekv(vmci_trans(vsk)->qpair, msg, len, 0);
1823 else
1824 return vmci_qpair_dequev(vmci_trans(vsk)->qpair, msg, len, 0);
1827 static ssize_t vmci_transport_stream_enqueue(
1828 struct vsock_sock *vsk,
1829 struct msghdr *msg,
1830 size_t len)
1832 return vmci_qpair_enquev(vmci_trans(vsk)->qpair, msg, len, 0);
1835 static s64 vmci_transport_stream_has_data(struct vsock_sock *vsk)
1837 return vmci_qpair_consume_buf_ready(vmci_trans(vsk)->qpair);
1840 static s64 vmci_transport_stream_has_space(struct vsock_sock *vsk)
1842 return vmci_qpair_produce_free_space(vmci_trans(vsk)->qpair);
1845 static u64 vmci_transport_stream_rcvhiwat(struct vsock_sock *vsk)
1847 return vmci_trans(vsk)->consume_size;
1850 static bool vmci_transport_stream_is_active(struct vsock_sock *vsk)
1852 return !vmci_handle_is_invalid(vmci_trans(vsk)->qp_handle);
1855 static u64 vmci_transport_get_buffer_size(struct vsock_sock *vsk)
1857 return vmci_trans(vsk)->queue_pair_size;
1860 static u64 vmci_transport_get_min_buffer_size(struct vsock_sock *vsk)
1862 return vmci_trans(vsk)->queue_pair_min_size;
1865 static u64 vmci_transport_get_max_buffer_size(struct vsock_sock *vsk)
1867 return vmci_trans(vsk)->queue_pair_max_size;
1870 static void vmci_transport_set_buffer_size(struct vsock_sock *vsk, u64 val)
1872 if (val < vmci_trans(vsk)->queue_pair_min_size)
1873 vmci_trans(vsk)->queue_pair_min_size = val;
1874 if (val > vmci_trans(vsk)->queue_pair_max_size)
1875 vmci_trans(vsk)->queue_pair_max_size = val;
1876 vmci_trans(vsk)->queue_pair_size = val;
1879 static void vmci_transport_set_min_buffer_size(struct vsock_sock *vsk,
1880 u64 val)
1882 if (val > vmci_trans(vsk)->queue_pair_size)
1883 vmci_trans(vsk)->queue_pair_size = val;
1884 vmci_trans(vsk)->queue_pair_min_size = val;
1887 static void vmci_transport_set_max_buffer_size(struct vsock_sock *vsk,
1888 u64 val)
1890 if (val < vmci_trans(vsk)->queue_pair_size)
1891 vmci_trans(vsk)->queue_pair_size = val;
1892 vmci_trans(vsk)->queue_pair_max_size = val;
1895 static int vmci_transport_notify_poll_in(
1896 struct vsock_sock *vsk,
1897 size_t target,
1898 bool *data_ready_now)
1900 return vmci_trans(vsk)->notify_ops->poll_in(
1901 &vsk->sk, target, data_ready_now);
1904 static int vmci_transport_notify_poll_out(
1905 struct vsock_sock *vsk,
1906 size_t target,
1907 bool *space_available_now)
1909 return vmci_trans(vsk)->notify_ops->poll_out(
1910 &vsk->sk, target, space_available_now);
1913 static int vmci_transport_notify_recv_init(
1914 struct vsock_sock *vsk,
1915 size_t target,
1916 struct vsock_transport_recv_notify_data *data)
1918 return vmci_trans(vsk)->notify_ops->recv_init(
1919 &vsk->sk, target,
1920 (struct vmci_transport_recv_notify_data *)data);
1923 static int vmci_transport_notify_recv_pre_block(
1924 struct vsock_sock *vsk,
1925 size_t target,
1926 struct vsock_transport_recv_notify_data *data)
1928 return vmci_trans(vsk)->notify_ops->recv_pre_block(
1929 &vsk->sk, target,
1930 (struct vmci_transport_recv_notify_data *)data);
1933 static int vmci_transport_notify_recv_pre_dequeue(
1934 struct vsock_sock *vsk,
1935 size_t target,
1936 struct vsock_transport_recv_notify_data *data)
1938 return vmci_trans(vsk)->notify_ops->recv_pre_dequeue(
1939 &vsk->sk, target,
1940 (struct vmci_transport_recv_notify_data *)data);
1943 static int vmci_transport_notify_recv_post_dequeue(
1944 struct vsock_sock *vsk,
1945 size_t target,
1946 ssize_t copied,
1947 bool data_read,
1948 struct vsock_transport_recv_notify_data *data)
1950 return vmci_trans(vsk)->notify_ops->recv_post_dequeue(
1951 &vsk->sk, target, copied, data_read,
1952 (struct vmci_transport_recv_notify_data *)data);
1955 static int vmci_transport_notify_send_init(
1956 struct vsock_sock *vsk,
1957 struct vsock_transport_send_notify_data *data)
1959 return vmci_trans(vsk)->notify_ops->send_init(
1960 &vsk->sk,
1961 (struct vmci_transport_send_notify_data *)data);
1964 static int vmci_transport_notify_send_pre_block(
1965 struct vsock_sock *vsk,
1966 struct vsock_transport_send_notify_data *data)
1968 return vmci_trans(vsk)->notify_ops->send_pre_block(
1969 &vsk->sk,
1970 (struct vmci_transport_send_notify_data *)data);
1973 static int vmci_transport_notify_send_pre_enqueue(
1974 struct vsock_sock *vsk,
1975 struct vsock_transport_send_notify_data *data)
1977 return vmci_trans(vsk)->notify_ops->send_pre_enqueue(
1978 &vsk->sk,
1979 (struct vmci_transport_send_notify_data *)data);
1982 static int vmci_transport_notify_send_post_enqueue(
1983 struct vsock_sock *vsk,
1984 ssize_t written,
1985 struct vsock_transport_send_notify_data *data)
1987 return vmci_trans(vsk)->notify_ops->send_post_enqueue(
1988 &vsk->sk, written,
1989 (struct vmci_transport_send_notify_data *)data);
1992 static bool vmci_transport_old_proto_override(bool *old_pkt_proto)
1994 if (PROTOCOL_OVERRIDE != -1) {
1995 if (PROTOCOL_OVERRIDE == 0)
1996 *old_pkt_proto = true;
1997 else
1998 *old_pkt_proto = false;
2000 pr_info("Proto override in use\n");
2001 return true;
2004 return false;
2007 static bool vmci_transport_proto_to_notify_struct(struct sock *sk,
2008 u16 *proto,
2009 bool old_pkt_proto)
2011 struct vsock_sock *vsk = vsock_sk(sk);
2013 if (old_pkt_proto) {
2014 if (*proto != VSOCK_PROTO_INVALID) {
2015 pr_err("Can't set both an old and new protocol\n");
2016 return false;
2018 vmci_trans(vsk)->notify_ops = &vmci_transport_notify_pkt_ops;
2019 goto exit;
2022 switch (*proto) {
2023 case VSOCK_PROTO_PKT_ON_NOTIFY:
2024 vmci_trans(vsk)->notify_ops =
2025 &vmci_transport_notify_pkt_q_state_ops;
2026 break;
2027 default:
2028 pr_err("Unknown notify protocol version\n");
2029 return false;
2032 exit:
2033 vmci_trans(vsk)->notify_ops->socket_init(sk);
2034 return true;
2037 static u16 vmci_transport_new_proto_supported_versions(void)
2039 if (PROTOCOL_OVERRIDE != -1)
2040 return PROTOCOL_OVERRIDE;
2042 return VSOCK_PROTO_ALL_SUPPORTED;
2045 static u32 vmci_transport_get_local_cid(void)
2047 return vmci_get_context_id();
2050 static const struct vsock_transport vmci_transport = {
2051 .init = vmci_transport_socket_init,
2052 .destruct = vmci_transport_destruct,
2053 .release = vmci_transport_release,
2054 .connect = vmci_transport_connect,
2055 .dgram_bind = vmci_transport_dgram_bind,
2056 .dgram_dequeue = vmci_transport_dgram_dequeue,
2057 .dgram_enqueue = vmci_transport_dgram_enqueue,
2058 .dgram_allow = vmci_transport_dgram_allow,
2059 .stream_dequeue = vmci_transport_stream_dequeue,
2060 .stream_enqueue = vmci_transport_stream_enqueue,
2061 .stream_has_data = vmci_transport_stream_has_data,
2062 .stream_has_space = vmci_transport_stream_has_space,
2063 .stream_rcvhiwat = vmci_transport_stream_rcvhiwat,
2064 .stream_is_active = vmci_transport_stream_is_active,
2065 .stream_allow = vmci_transport_stream_allow,
2066 .notify_poll_in = vmci_transport_notify_poll_in,
2067 .notify_poll_out = vmci_transport_notify_poll_out,
2068 .notify_recv_init = vmci_transport_notify_recv_init,
2069 .notify_recv_pre_block = vmci_transport_notify_recv_pre_block,
2070 .notify_recv_pre_dequeue = vmci_transport_notify_recv_pre_dequeue,
2071 .notify_recv_post_dequeue = vmci_transport_notify_recv_post_dequeue,
2072 .notify_send_init = vmci_transport_notify_send_init,
2073 .notify_send_pre_block = vmci_transport_notify_send_pre_block,
2074 .notify_send_pre_enqueue = vmci_transport_notify_send_pre_enqueue,
2075 .notify_send_post_enqueue = vmci_transport_notify_send_post_enqueue,
2076 .shutdown = vmci_transport_shutdown,
2077 .set_buffer_size = vmci_transport_set_buffer_size,
2078 .set_min_buffer_size = vmci_transport_set_min_buffer_size,
2079 .set_max_buffer_size = vmci_transport_set_max_buffer_size,
2080 .get_buffer_size = vmci_transport_get_buffer_size,
2081 .get_min_buffer_size = vmci_transport_get_min_buffer_size,
2082 .get_max_buffer_size = vmci_transport_get_max_buffer_size,
2083 .get_local_cid = vmci_transport_get_local_cid,
2086 static int __init vmci_transport_init(void)
2088 int err;
2090 /* Create the datagram handle that we will use to send and receive all
2091 * VSocket control messages for this context.
2093 err = vmci_transport_datagram_create_hnd(VMCI_TRANSPORT_PACKET_RID,
2094 VMCI_FLAG_ANYCID_DG_HND,
2095 vmci_transport_recv_stream_cb,
2096 NULL,
2097 &vmci_transport_stream_handle);
2098 if (err < VMCI_SUCCESS) {
2099 pr_err("Unable to create datagram handle. (%d)\n", err);
2100 return vmci_transport_error_to_vsock_error(err);
2103 err = vmci_event_subscribe(VMCI_EVENT_QP_RESUMED,
2104 vmci_transport_qp_resumed_cb,
2105 NULL, &vmci_transport_qp_resumed_sub_id);
2106 if (err < VMCI_SUCCESS) {
2107 pr_err("Unable to subscribe to resumed event. (%d)\n", err);
2108 err = vmci_transport_error_to_vsock_error(err);
2109 vmci_transport_qp_resumed_sub_id = VMCI_INVALID_ID;
2110 goto err_destroy_stream_handle;
2113 err = vsock_core_init(&vmci_transport);
2114 if (err < 0)
2115 goto err_unsubscribe;
2117 return 0;
2119 err_unsubscribe:
2120 vmci_event_unsubscribe(vmci_transport_qp_resumed_sub_id);
2121 err_destroy_stream_handle:
2122 vmci_datagram_destroy_handle(vmci_transport_stream_handle);
2123 return err;
2125 module_init(vmci_transport_init);
2127 static void __exit vmci_transport_exit(void)
2129 cancel_work_sync(&vmci_transport_cleanup_work);
2130 vmci_transport_free_resources(&vmci_transport_cleanup_list);
2132 if (!vmci_handle_is_invalid(vmci_transport_stream_handle)) {
2133 if (vmci_datagram_destroy_handle(
2134 vmci_transport_stream_handle) != VMCI_SUCCESS)
2135 pr_err("Couldn't destroy datagram handle\n");
2136 vmci_transport_stream_handle = VMCI_INVALID_HANDLE;
2139 if (vmci_transport_qp_resumed_sub_id != VMCI_INVALID_ID) {
2140 vmci_event_unsubscribe(vmci_transport_qp_resumed_sub_id);
2141 vmci_transport_qp_resumed_sub_id = VMCI_INVALID_ID;
2144 vsock_core_exit();
2146 module_exit(vmci_transport_exit);
2148 MODULE_AUTHOR("VMware, Inc.");
2149 MODULE_DESCRIPTION("VMCI transport for Virtual Sockets");
2150 MODULE_VERSION("1.0.5.0-k");
2151 MODULE_LICENSE("GPL v2");
2152 MODULE_ALIAS("vmware_vsock");
2153 MODULE_ALIAS_NETPROTO(PF_VSOCK);