Linux 5.1.15
[linux/fpc-iii.git] / net / vmw_vsock / vmci_transport.c
blobc3d5ab01fba7ba280a752628788f0f175bf1cd83
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_alloc_send_control_pkt(struct sockaddr_vm *src,
268 struct sockaddr_vm *dst,
269 enum vmci_transport_packet_type type,
270 u64 size,
271 u64 mode,
272 struct vmci_transport_waiting_info *wait,
273 u16 proto,
274 struct vmci_handle handle)
276 struct vmci_transport_packet *pkt;
277 int err;
279 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
280 if (!pkt)
281 return -ENOMEM;
283 err = __vmci_transport_send_control_pkt(pkt, src, dst, type, size,
284 mode, wait, proto, handle,
285 true);
286 kfree(pkt);
288 return err;
291 static int
292 vmci_transport_send_control_pkt(struct sock *sk,
293 enum vmci_transport_packet_type type,
294 u64 size,
295 u64 mode,
296 struct vmci_transport_waiting_info *wait,
297 u16 proto,
298 struct vmci_handle handle)
300 struct vsock_sock *vsk;
302 vsk = vsock_sk(sk);
304 if (!vsock_addr_bound(&vsk->local_addr))
305 return -EINVAL;
307 if (!vsock_addr_bound(&vsk->remote_addr))
308 return -EINVAL;
310 return vmci_transport_alloc_send_control_pkt(&vsk->local_addr,
311 &vsk->remote_addr,
312 type, size, mode,
313 wait, proto, handle);
316 static int vmci_transport_send_reset_bh(struct sockaddr_vm *dst,
317 struct sockaddr_vm *src,
318 struct vmci_transport_packet *pkt)
320 if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_RST)
321 return 0;
322 return vmci_transport_send_control_pkt_bh(
323 dst, src,
324 VMCI_TRANSPORT_PACKET_TYPE_RST, 0,
325 0, NULL, VMCI_INVALID_HANDLE);
328 static int vmci_transport_send_reset(struct sock *sk,
329 struct vmci_transport_packet *pkt)
331 struct sockaddr_vm *dst_ptr;
332 struct sockaddr_vm dst;
333 struct vsock_sock *vsk;
335 if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_RST)
336 return 0;
338 vsk = vsock_sk(sk);
340 if (!vsock_addr_bound(&vsk->local_addr))
341 return -EINVAL;
343 if (vsock_addr_bound(&vsk->remote_addr)) {
344 dst_ptr = &vsk->remote_addr;
345 } else {
346 vsock_addr_init(&dst, pkt->dg.src.context,
347 pkt->src_port);
348 dst_ptr = &dst;
350 return vmci_transport_alloc_send_control_pkt(&vsk->local_addr, dst_ptr,
351 VMCI_TRANSPORT_PACKET_TYPE_RST,
352 0, 0, NULL, VSOCK_PROTO_INVALID,
353 VMCI_INVALID_HANDLE);
356 static int vmci_transport_send_negotiate(struct sock *sk, size_t size)
358 return vmci_transport_send_control_pkt(
360 VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE,
361 size, 0, NULL,
362 VSOCK_PROTO_INVALID,
363 VMCI_INVALID_HANDLE);
366 static int vmci_transport_send_negotiate2(struct sock *sk, size_t size,
367 u16 version)
369 return vmci_transport_send_control_pkt(
371 VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2,
372 size, 0, NULL, version,
373 VMCI_INVALID_HANDLE);
376 static int vmci_transport_send_qp_offer(struct sock *sk,
377 struct vmci_handle handle)
379 return vmci_transport_send_control_pkt(
380 sk, VMCI_TRANSPORT_PACKET_TYPE_OFFER, 0,
381 0, NULL,
382 VSOCK_PROTO_INVALID, handle);
385 static int vmci_transport_send_attach(struct sock *sk,
386 struct vmci_handle handle)
388 return vmci_transport_send_control_pkt(
389 sk, VMCI_TRANSPORT_PACKET_TYPE_ATTACH,
390 0, 0, NULL, VSOCK_PROTO_INVALID,
391 handle);
394 static int vmci_transport_reply_reset(struct vmci_transport_packet *pkt)
396 return vmci_transport_reply_control_pkt_fast(
397 pkt,
398 VMCI_TRANSPORT_PACKET_TYPE_RST,
399 0, 0, NULL,
400 VMCI_INVALID_HANDLE);
403 static int vmci_transport_send_invalid_bh(struct sockaddr_vm *dst,
404 struct sockaddr_vm *src)
406 return vmci_transport_send_control_pkt_bh(
407 dst, src,
408 VMCI_TRANSPORT_PACKET_TYPE_INVALID,
409 0, 0, NULL, VMCI_INVALID_HANDLE);
412 int vmci_transport_send_wrote_bh(struct sockaddr_vm *dst,
413 struct sockaddr_vm *src)
415 return vmci_transport_send_control_pkt_bh(
416 dst, src,
417 VMCI_TRANSPORT_PACKET_TYPE_WROTE, 0,
418 0, NULL, VMCI_INVALID_HANDLE);
421 int vmci_transport_send_read_bh(struct sockaddr_vm *dst,
422 struct sockaddr_vm *src)
424 return vmci_transport_send_control_pkt_bh(
425 dst, src,
426 VMCI_TRANSPORT_PACKET_TYPE_READ, 0,
427 0, NULL, VMCI_INVALID_HANDLE);
430 int vmci_transport_send_wrote(struct sock *sk)
432 return vmci_transport_send_control_pkt(
433 sk, VMCI_TRANSPORT_PACKET_TYPE_WROTE, 0,
434 0, NULL, VSOCK_PROTO_INVALID,
435 VMCI_INVALID_HANDLE);
438 int vmci_transport_send_read(struct sock *sk)
440 return vmci_transport_send_control_pkt(
441 sk, VMCI_TRANSPORT_PACKET_TYPE_READ, 0,
442 0, NULL, VSOCK_PROTO_INVALID,
443 VMCI_INVALID_HANDLE);
446 int vmci_transport_send_waiting_write(struct sock *sk,
447 struct vmci_transport_waiting_info *wait)
449 return vmci_transport_send_control_pkt(
450 sk, VMCI_TRANSPORT_PACKET_TYPE_WAITING_WRITE,
451 0, 0, wait, VSOCK_PROTO_INVALID,
452 VMCI_INVALID_HANDLE);
455 int vmci_transport_send_waiting_read(struct sock *sk,
456 struct vmci_transport_waiting_info *wait)
458 return vmci_transport_send_control_pkt(
459 sk, VMCI_TRANSPORT_PACKET_TYPE_WAITING_READ,
460 0, 0, wait, VSOCK_PROTO_INVALID,
461 VMCI_INVALID_HANDLE);
464 static int vmci_transport_shutdown(struct vsock_sock *vsk, int mode)
466 return vmci_transport_send_control_pkt(
467 &vsk->sk,
468 VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN,
469 0, mode, NULL,
470 VSOCK_PROTO_INVALID,
471 VMCI_INVALID_HANDLE);
474 static int vmci_transport_send_conn_request(struct sock *sk, size_t size)
476 return vmci_transport_send_control_pkt(sk,
477 VMCI_TRANSPORT_PACKET_TYPE_REQUEST,
478 size, 0, NULL,
479 VSOCK_PROTO_INVALID,
480 VMCI_INVALID_HANDLE);
483 static int vmci_transport_send_conn_request2(struct sock *sk, size_t size,
484 u16 version)
486 return vmci_transport_send_control_pkt(
487 sk, VMCI_TRANSPORT_PACKET_TYPE_REQUEST2,
488 size, 0, NULL, version,
489 VMCI_INVALID_HANDLE);
492 static struct sock *vmci_transport_get_pending(
493 struct sock *listener,
494 struct vmci_transport_packet *pkt)
496 struct vsock_sock *vlistener;
497 struct vsock_sock *vpending;
498 struct sock *pending;
499 struct sockaddr_vm src;
501 vsock_addr_init(&src, pkt->dg.src.context, pkt->src_port);
503 vlistener = vsock_sk(listener);
505 list_for_each_entry(vpending, &vlistener->pending_links,
506 pending_links) {
507 if (vsock_addr_equals_addr(&src, &vpending->remote_addr) &&
508 pkt->dst_port == vpending->local_addr.svm_port) {
509 pending = sk_vsock(vpending);
510 sock_hold(pending);
511 goto found;
515 pending = NULL;
516 found:
517 return pending;
521 static void vmci_transport_release_pending(struct sock *pending)
523 sock_put(pending);
526 /* We allow two kinds of sockets to communicate with a restricted VM: 1)
527 * trusted sockets 2) sockets from applications running as the same user as the
528 * VM (this is only true for the host side and only when using hosted products)
531 static bool vmci_transport_is_trusted(struct vsock_sock *vsock, u32 peer_cid)
533 return vsock->trusted ||
534 vmci_is_context_owner(peer_cid, vsock->owner->uid);
537 /* We allow sending datagrams to and receiving datagrams from a restricted VM
538 * only if it is trusted as described in vmci_transport_is_trusted.
541 static bool vmci_transport_allow_dgram(struct vsock_sock *vsock, u32 peer_cid)
543 if (VMADDR_CID_HYPERVISOR == peer_cid)
544 return true;
546 if (vsock->cached_peer != peer_cid) {
547 vsock->cached_peer = peer_cid;
548 if (!vmci_transport_is_trusted(vsock, peer_cid) &&
549 (vmci_context_get_priv_flags(peer_cid) &
550 VMCI_PRIVILEGE_FLAG_RESTRICTED)) {
551 vsock->cached_peer_allow_dgram = false;
552 } else {
553 vsock->cached_peer_allow_dgram = true;
557 return vsock->cached_peer_allow_dgram;
560 static int
561 vmci_transport_queue_pair_alloc(struct vmci_qp **qpair,
562 struct vmci_handle *handle,
563 u64 produce_size,
564 u64 consume_size,
565 u32 peer, u32 flags, bool trusted)
567 int err = 0;
569 if (trusted) {
570 /* Try to allocate our queue pair as trusted. This will only
571 * work if vsock is running in the host.
574 err = vmci_qpair_alloc(qpair, handle, produce_size,
575 consume_size,
576 peer, flags,
577 VMCI_PRIVILEGE_FLAG_TRUSTED);
578 if (err != VMCI_ERROR_NO_ACCESS)
579 goto out;
583 err = vmci_qpair_alloc(qpair, handle, produce_size, consume_size,
584 peer, flags, VMCI_NO_PRIVILEGE_FLAGS);
585 out:
586 if (err < 0) {
587 pr_err("Could not attach to queue pair with %d\n",
588 err);
589 err = vmci_transport_error_to_vsock_error(err);
592 return err;
595 static int
596 vmci_transport_datagram_create_hnd(u32 resource_id,
597 u32 flags,
598 vmci_datagram_recv_cb recv_cb,
599 void *client_data,
600 struct vmci_handle *out_handle)
602 int err = 0;
604 /* Try to allocate our datagram handler as trusted. This will only work
605 * if vsock is running in the host.
608 err = vmci_datagram_create_handle_priv(resource_id, flags,
609 VMCI_PRIVILEGE_FLAG_TRUSTED,
610 recv_cb,
611 client_data, out_handle);
613 if (err == VMCI_ERROR_NO_ACCESS)
614 err = vmci_datagram_create_handle(resource_id, flags,
615 recv_cb, client_data,
616 out_handle);
618 return err;
621 /* This is invoked as part of a tasklet that's scheduled when the VMCI
622 * interrupt fires. This is run in bottom-half context and if it ever needs to
623 * sleep it should defer that work to a work queue.
626 static int vmci_transport_recv_dgram_cb(void *data, struct vmci_datagram *dg)
628 struct sock *sk;
629 size_t size;
630 struct sk_buff *skb;
631 struct vsock_sock *vsk;
633 sk = (struct sock *)data;
635 /* This handler is privileged when this module is running on the host.
636 * We will get datagrams from all endpoints (even VMs that are in a
637 * restricted context). If we get one from a restricted context then
638 * the destination socket must be trusted.
640 * NOTE: We access the socket struct without holding the lock here.
641 * This is ok because the field we are interested is never modified
642 * outside of the create and destruct socket functions.
644 vsk = vsock_sk(sk);
645 if (!vmci_transport_allow_dgram(vsk, dg->src.context))
646 return VMCI_ERROR_NO_ACCESS;
648 size = VMCI_DG_SIZE(dg);
650 /* Attach the packet to the socket's receive queue as an sk_buff. */
651 skb = alloc_skb(size, GFP_ATOMIC);
652 if (!skb)
653 return VMCI_ERROR_NO_MEM;
655 /* sk_receive_skb() will do a sock_put(), so hold here. */
656 sock_hold(sk);
657 skb_put(skb, size);
658 memcpy(skb->data, dg, size);
659 sk_receive_skb(sk, skb, 0);
661 return VMCI_SUCCESS;
664 static bool vmci_transport_stream_allow(u32 cid, u32 port)
666 static const u32 non_socket_contexts[] = {
667 VMADDR_CID_RESERVED,
669 int i;
671 BUILD_BUG_ON(sizeof(cid) != sizeof(*non_socket_contexts));
673 for (i = 0; i < ARRAY_SIZE(non_socket_contexts); i++) {
674 if (cid == non_socket_contexts[i])
675 return false;
678 return true;
681 /* This is invoked as part of a tasklet that's scheduled when the VMCI
682 * interrupt fires. This is run in bottom-half context but it defers most of
683 * its work to the packet handling work queue.
686 static int vmci_transport_recv_stream_cb(void *data, struct vmci_datagram *dg)
688 struct sock *sk;
689 struct sockaddr_vm dst;
690 struct sockaddr_vm src;
691 struct vmci_transport_packet *pkt;
692 struct vsock_sock *vsk;
693 bool bh_process_pkt;
694 int err;
696 sk = NULL;
697 err = VMCI_SUCCESS;
698 bh_process_pkt = false;
700 /* Ignore incoming packets from contexts without sockets, or resources
701 * that aren't vsock implementations.
704 if (!vmci_transport_stream_allow(dg->src.context, -1)
705 || vmci_transport_peer_rid(dg->src.context) != dg->src.resource)
706 return VMCI_ERROR_NO_ACCESS;
708 if (VMCI_DG_SIZE(dg) < sizeof(*pkt))
709 /* Drop datagrams that do not contain full VSock packets. */
710 return VMCI_ERROR_INVALID_ARGS;
712 pkt = (struct vmci_transport_packet *)dg;
714 /* Find the socket that should handle this packet. First we look for a
715 * connected socket and if there is none we look for a socket bound to
716 * the destintation address.
718 vsock_addr_init(&src, pkt->dg.src.context, pkt->src_port);
719 vsock_addr_init(&dst, pkt->dg.dst.context, pkt->dst_port);
721 sk = vsock_find_connected_socket(&src, &dst);
722 if (!sk) {
723 sk = vsock_find_bound_socket(&dst);
724 if (!sk) {
725 /* We could not find a socket for this specified
726 * address. If this packet is a RST, we just drop it.
727 * If it is another packet, we send a RST. Note that
728 * we do not send a RST reply to RSTs so that we do not
729 * continually send RSTs between two endpoints.
731 * Note that since this is a reply, dst is src and src
732 * is dst.
734 if (vmci_transport_send_reset_bh(&dst, &src, pkt) < 0)
735 pr_err("unable to send reset\n");
737 err = VMCI_ERROR_NOT_FOUND;
738 goto out;
742 /* If the received packet type is beyond all types known to this
743 * implementation, reply with an invalid message. Hopefully this will
744 * help when implementing backwards compatibility in the future.
746 if (pkt->type >= VMCI_TRANSPORT_PACKET_TYPE_MAX) {
747 vmci_transport_send_invalid_bh(&dst, &src);
748 err = VMCI_ERROR_INVALID_ARGS;
749 goto out;
752 /* This handler is privileged when this module is running on the host.
753 * We will get datagram connect requests from all endpoints (even VMs
754 * that are in a restricted context). If we get one from a restricted
755 * context then the destination socket must be trusted.
757 * NOTE: We access the socket struct without holding the lock here.
758 * This is ok because the field we are interested is never modified
759 * outside of the create and destruct socket functions.
761 vsk = vsock_sk(sk);
762 if (!vmci_transport_allow_dgram(vsk, pkt->dg.src.context)) {
763 err = VMCI_ERROR_NO_ACCESS;
764 goto out;
767 /* We do most everything in a work queue, but let's fast path the
768 * notification of reads and writes to help data transfer performance.
769 * We can only do this if there is no process context code executing
770 * for this socket since that may change the state.
772 bh_lock_sock(sk);
774 if (!sock_owned_by_user(sk)) {
775 /* The local context ID may be out of date, update it. */
776 vsk->local_addr.svm_cid = dst.svm_cid;
778 if (sk->sk_state == TCP_ESTABLISHED)
779 vmci_trans(vsk)->notify_ops->handle_notify_pkt(
780 sk, pkt, true, &dst, &src,
781 &bh_process_pkt);
784 bh_unlock_sock(sk);
786 if (!bh_process_pkt) {
787 struct vmci_transport_recv_pkt_info *recv_pkt_info;
789 recv_pkt_info = kmalloc(sizeof(*recv_pkt_info), GFP_ATOMIC);
790 if (!recv_pkt_info) {
791 if (vmci_transport_send_reset_bh(&dst, &src, pkt) < 0)
792 pr_err("unable to send reset\n");
794 err = VMCI_ERROR_NO_MEM;
795 goto out;
798 recv_pkt_info->sk = sk;
799 memcpy(&recv_pkt_info->pkt, pkt, sizeof(recv_pkt_info->pkt));
800 INIT_WORK(&recv_pkt_info->work, vmci_transport_recv_pkt_work);
802 schedule_work(&recv_pkt_info->work);
803 /* Clear sk so that the reference count incremented by one of
804 * the Find functions above is not decremented below. We need
805 * that reference count for the packet handler we've scheduled
806 * to run.
808 sk = NULL;
811 out:
812 if (sk)
813 sock_put(sk);
815 return err;
818 static void vmci_transport_handle_detach(struct sock *sk)
820 struct vsock_sock *vsk;
822 vsk = vsock_sk(sk);
823 if (!vmci_handle_is_invalid(vmci_trans(vsk)->qp_handle)) {
824 sock_set_flag(sk, SOCK_DONE);
826 /* On a detach the peer will not be sending or receiving
827 * anymore.
829 vsk->peer_shutdown = SHUTDOWN_MASK;
831 /* We should not be sending anymore since the peer won't be
832 * there to receive, but we can still receive if there is data
833 * left in our consume queue. If the local endpoint is a host,
834 * we can't call vsock_stream_has_data, since that may block,
835 * but a host endpoint can't read data once the VM has
836 * detached, so there is no available data in that case.
838 if (vsk->local_addr.svm_cid == VMADDR_CID_HOST ||
839 vsock_stream_has_data(vsk) <= 0) {
840 if (sk->sk_state == TCP_SYN_SENT) {
841 /* The peer may detach from a queue pair while
842 * we are still in the connecting state, i.e.,
843 * if the peer VM is killed after attaching to
844 * a queue pair, but before we complete the
845 * handshake. In that case, we treat the detach
846 * event like a reset.
849 sk->sk_state = TCP_CLOSE;
850 sk->sk_err = ECONNRESET;
851 sk->sk_error_report(sk);
852 return;
854 sk->sk_state = TCP_CLOSE;
856 sk->sk_state_change(sk);
860 static void vmci_transport_peer_detach_cb(u32 sub_id,
861 const struct vmci_event_data *e_data,
862 void *client_data)
864 struct vmci_transport *trans = client_data;
865 const struct vmci_event_payload_qp *e_payload;
867 e_payload = vmci_event_data_const_payload(e_data);
869 /* XXX This is lame, we should provide a way to lookup sockets by
870 * qp_handle.
872 if (vmci_handle_is_invalid(e_payload->handle) ||
873 !vmci_handle_is_equal(trans->qp_handle, e_payload->handle))
874 return;
876 /* We don't ask for delayed CBs when we subscribe to this event (we
877 * pass 0 as flags to vmci_event_subscribe()). VMCI makes no
878 * guarantees in that case about what context we might be running in,
879 * so it could be BH or process, blockable or non-blockable. So we
880 * need to account for all possible contexts here.
882 spin_lock_bh(&trans->lock);
883 if (!trans->sk)
884 goto out;
886 /* Apart from here, trans->lock is only grabbed as part of sk destruct,
887 * where trans->sk isn't locked.
889 bh_lock_sock(trans->sk);
891 vmci_transport_handle_detach(trans->sk);
893 bh_unlock_sock(trans->sk);
894 out:
895 spin_unlock_bh(&trans->lock);
898 static void vmci_transport_qp_resumed_cb(u32 sub_id,
899 const struct vmci_event_data *e_data,
900 void *client_data)
902 vsock_for_each_connected_socket(vmci_transport_handle_detach);
905 static void vmci_transport_recv_pkt_work(struct work_struct *work)
907 struct vmci_transport_recv_pkt_info *recv_pkt_info;
908 struct vmci_transport_packet *pkt;
909 struct sock *sk;
911 recv_pkt_info =
912 container_of(work, struct vmci_transport_recv_pkt_info, work);
913 sk = recv_pkt_info->sk;
914 pkt = &recv_pkt_info->pkt;
916 lock_sock(sk);
918 /* The local context ID may be out of date. */
919 vsock_sk(sk)->local_addr.svm_cid = pkt->dg.dst.context;
921 switch (sk->sk_state) {
922 case TCP_LISTEN:
923 vmci_transport_recv_listen(sk, pkt);
924 break;
925 case TCP_SYN_SENT:
926 /* Processing of pending connections for servers goes through
927 * the listening socket, so see vmci_transport_recv_listen()
928 * for that path.
930 vmci_transport_recv_connecting_client(sk, pkt);
931 break;
932 case TCP_ESTABLISHED:
933 vmci_transport_recv_connected(sk, pkt);
934 break;
935 default:
936 /* Because this function does not run in the same context as
937 * vmci_transport_recv_stream_cb it is possible that the
938 * socket has closed. We need to let the other side know or it
939 * could be sitting in a connect and hang forever. Send a
940 * reset to prevent that.
942 vmci_transport_send_reset(sk, pkt);
943 break;
946 release_sock(sk);
947 kfree(recv_pkt_info);
948 /* Release reference obtained in the stream callback when we fetched
949 * this socket out of the bound or connected list.
951 sock_put(sk);
954 static int vmci_transport_recv_listen(struct sock *sk,
955 struct vmci_transport_packet *pkt)
957 struct sock *pending;
958 struct vsock_sock *vpending;
959 int err;
960 u64 qp_size;
961 bool old_request = false;
962 bool old_pkt_proto = false;
964 err = 0;
966 /* Because we are in the listen state, we could be receiving a packet
967 * for ourself or any previous connection requests that we received.
968 * If it's the latter, we try to find a socket in our list of pending
969 * connections and, if we do, call the appropriate handler for the
970 * state that that socket is in. Otherwise we try to service the
971 * connection request.
973 pending = vmci_transport_get_pending(sk, pkt);
974 if (pending) {
975 lock_sock(pending);
977 /* The local context ID may be out of date. */
978 vsock_sk(pending)->local_addr.svm_cid = pkt->dg.dst.context;
980 switch (pending->sk_state) {
981 case TCP_SYN_SENT:
982 err = vmci_transport_recv_connecting_server(sk,
983 pending,
984 pkt);
985 break;
986 default:
987 vmci_transport_send_reset(pending, pkt);
988 err = -EINVAL;
991 if (err < 0)
992 vsock_remove_pending(sk, pending);
994 release_sock(pending);
995 vmci_transport_release_pending(pending);
997 return err;
1000 /* The listen state only accepts connection requests. Reply with a
1001 * reset unless we received a reset.
1004 if (!(pkt->type == VMCI_TRANSPORT_PACKET_TYPE_REQUEST ||
1005 pkt->type == VMCI_TRANSPORT_PACKET_TYPE_REQUEST2)) {
1006 vmci_transport_reply_reset(pkt);
1007 return -EINVAL;
1010 if (pkt->u.size == 0) {
1011 vmci_transport_reply_reset(pkt);
1012 return -EINVAL;
1015 /* If this socket can't accommodate this connection request, we send a
1016 * reset. Otherwise we create and initialize a child socket and reply
1017 * with a connection negotiation.
1019 if (sk->sk_ack_backlog >= sk->sk_max_ack_backlog) {
1020 vmci_transport_reply_reset(pkt);
1021 return -ECONNREFUSED;
1024 pending = __vsock_create(sock_net(sk), NULL, sk, GFP_KERNEL,
1025 sk->sk_type, 0);
1026 if (!pending) {
1027 vmci_transport_send_reset(sk, pkt);
1028 return -ENOMEM;
1031 vpending = vsock_sk(pending);
1033 vsock_addr_init(&vpending->local_addr, pkt->dg.dst.context,
1034 pkt->dst_port);
1035 vsock_addr_init(&vpending->remote_addr, pkt->dg.src.context,
1036 pkt->src_port);
1038 /* If the proposed size fits within our min/max, accept it. Otherwise
1039 * propose our own size.
1041 if (pkt->u.size >= vmci_trans(vpending)->queue_pair_min_size &&
1042 pkt->u.size <= vmci_trans(vpending)->queue_pair_max_size) {
1043 qp_size = pkt->u.size;
1044 } else {
1045 qp_size = vmci_trans(vpending)->queue_pair_size;
1048 /* Figure out if we are using old or new requests based on the
1049 * overrides pkt types sent by our peer.
1051 if (vmci_transport_old_proto_override(&old_pkt_proto)) {
1052 old_request = old_pkt_proto;
1053 } else {
1054 if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_REQUEST)
1055 old_request = true;
1056 else if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_REQUEST2)
1057 old_request = false;
1061 if (old_request) {
1062 /* Handle a REQUEST (or override) */
1063 u16 version = VSOCK_PROTO_INVALID;
1064 if (vmci_transport_proto_to_notify_struct(
1065 pending, &version, true))
1066 err = vmci_transport_send_negotiate(pending, qp_size);
1067 else
1068 err = -EINVAL;
1070 } else {
1071 /* Handle a REQUEST2 (or override) */
1072 int proto_int = pkt->proto;
1073 int pos;
1074 u16 active_proto_version = 0;
1076 /* The list of possible protocols is the intersection of all
1077 * protocols the client supports ... plus all the protocols we
1078 * support.
1080 proto_int &= vmci_transport_new_proto_supported_versions();
1082 /* We choose the highest possible protocol version and use that
1083 * one.
1085 pos = fls(proto_int);
1086 if (pos) {
1087 active_proto_version = (1 << (pos - 1));
1088 if (vmci_transport_proto_to_notify_struct(
1089 pending, &active_proto_version, false))
1090 err = vmci_transport_send_negotiate2(pending,
1091 qp_size,
1092 active_proto_version);
1093 else
1094 err = -EINVAL;
1096 } else {
1097 err = -EINVAL;
1101 if (err < 0) {
1102 vmci_transport_send_reset(sk, pkt);
1103 sock_put(pending);
1104 err = vmci_transport_error_to_vsock_error(err);
1105 goto out;
1108 vsock_add_pending(sk, pending);
1109 sk->sk_ack_backlog++;
1111 pending->sk_state = TCP_SYN_SENT;
1112 vmci_trans(vpending)->produce_size =
1113 vmci_trans(vpending)->consume_size = qp_size;
1114 vmci_trans(vpending)->queue_pair_size = qp_size;
1116 vmci_trans(vpending)->notify_ops->process_request(pending);
1118 /* We might never receive another message for this socket and it's not
1119 * connected to any process, so we have to ensure it gets cleaned up
1120 * ourself. Our delayed work function will take care of that. Note
1121 * that we do not ever cancel this function since we have few
1122 * guarantees about its state when calling cancel_delayed_work().
1123 * Instead we hold a reference on the socket for that function and make
1124 * it capable of handling cases where it needs to do nothing but
1125 * release that reference.
1127 vpending->listener = sk;
1128 sock_hold(sk);
1129 sock_hold(pending);
1130 schedule_delayed_work(&vpending->pending_work, HZ);
1132 out:
1133 return err;
1136 static int
1137 vmci_transport_recv_connecting_server(struct sock *listener,
1138 struct sock *pending,
1139 struct vmci_transport_packet *pkt)
1141 struct vsock_sock *vpending;
1142 struct vmci_handle handle;
1143 struct vmci_qp *qpair;
1144 bool is_local;
1145 u32 flags;
1146 u32 detach_sub_id;
1147 int err;
1148 int skerr;
1150 vpending = vsock_sk(pending);
1151 detach_sub_id = VMCI_INVALID_ID;
1153 switch (pkt->type) {
1154 case VMCI_TRANSPORT_PACKET_TYPE_OFFER:
1155 if (vmci_handle_is_invalid(pkt->u.handle)) {
1156 vmci_transport_send_reset(pending, pkt);
1157 skerr = EPROTO;
1158 err = -EINVAL;
1159 goto destroy;
1161 break;
1162 default:
1163 /* Close and cleanup the connection. */
1164 vmci_transport_send_reset(pending, pkt);
1165 skerr = EPROTO;
1166 err = pkt->type == VMCI_TRANSPORT_PACKET_TYPE_RST ? 0 : -EINVAL;
1167 goto destroy;
1170 /* In order to complete the connection we need to attach to the offered
1171 * queue pair and send an attach notification. We also subscribe to the
1172 * detach event so we know when our peer goes away, and we do that
1173 * before attaching so we don't miss an event. If all this succeeds,
1174 * we update our state and wakeup anything waiting in accept() for a
1175 * connection.
1178 /* We don't care about attach since we ensure the other side has
1179 * attached by specifying the ATTACH_ONLY flag below.
1181 err = vmci_event_subscribe(VMCI_EVENT_QP_PEER_DETACH,
1182 vmci_transport_peer_detach_cb,
1183 vmci_trans(vpending), &detach_sub_id);
1184 if (err < VMCI_SUCCESS) {
1185 vmci_transport_send_reset(pending, pkt);
1186 err = vmci_transport_error_to_vsock_error(err);
1187 skerr = -err;
1188 goto destroy;
1191 vmci_trans(vpending)->detach_sub_id = detach_sub_id;
1193 /* Now attach to the queue pair the client created. */
1194 handle = pkt->u.handle;
1196 /* vpending->local_addr always has a context id so we do not need to
1197 * worry about VMADDR_CID_ANY in this case.
1199 is_local =
1200 vpending->remote_addr.svm_cid == vpending->local_addr.svm_cid;
1201 flags = VMCI_QPFLAG_ATTACH_ONLY;
1202 flags |= is_local ? VMCI_QPFLAG_LOCAL : 0;
1204 err = vmci_transport_queue_pair_alloc(
1205 &qpair,
1206 &handle,
1207 vmci_trans(vpending)->produce_size,
1208 vmci_trans(vpending)->consume_size,
1209 pkt->dg.src.context,
1210 flags,
1211 vmci_transport_is_trusted(
1212 vpending,
1213 vpending->remote_addr.svm_cid));
1214 if (err < 0) {
1215 vmci_transport_send_reset(pending, pkt);
1216 skerr = -err;
1217 goto destroy;
1220 vmci_trans(vpending)->qp_handle = handle;
1221 vmci_trans(vpending)->qpair = qpair;
1223 /* When we send the attach message, we must be ready to handle incoming
1224 * control messages on the newly connected socket. So we move the
1225 * pending socket to the connected state before sending the attach
1226 * message. Otherwise, an incoming packet triggered by the attach being
1227 * received by the peer may be processed concurrently with what happens
1228 * below after sending the attach message, and that incoming packet
1229 * will find the listening socket instead of the (currently) pending
1230 * socket. Note that enqueueing the socket increments the reference
1231 * count, so even if a reset comes before the connection is accepted,
1232 * the socket will be valid until it is removed from the queue.
1234 * If we fail sending the attach below, we remove the socket from the
1235 * connected list and move the socket to TCP_CLOSE before
1236 * releasing the lock, so a pending slow path processing of an incoming
1237 * packet will not see the socket in the connected state in that case.
1239 pending->sk_state = TCP_ESTABLISHED;
1241 vsock_insert_connected(vpending);
1243 /* Notify our peer of our attach. */
1244 err = vmci_transport_send_attach(pending, handle);
1245 if (err < 0) {
1246 vsock_remove_connected(vpending);
1247 pr_err("Could not send attach\n");
1248 vmci_transport_send_reset(pending, pkt);
1249 err = vmci_transport_error_to_vsock_error(err);
1250 skerr = -err;
1251 goto destroy;
1254 /* We have a connection. Move the now connected socket from the
1255 * listener's pending list to the accept queue so callers of accept()
1256 * can find it.
1258 vsock_remove_pending(listener, pending);
1259 vsock_enqueue_accept(listener, pending);
1261 /* Callers of accept() will be be waiting on the listening socket, not
1262 * the pending socket.
1264 listener->sk_data_ready(listener);
1266 return 0;
1268 destroy:
1269 pending->sk_err = skerr;
1270 pending->sk_state = TCP_CLOSE;
1271 /* As long as we drop our reference, all necessary cleanup will handle
1272 * when the cleanup function drops its reference and our destruct
1273 * implementation is called. Note that since the listen handler will
1274 * remove pending from the pending list upon our failure, the cleanup
1275 * function won't drop the additional reference, which is why we do it
1276 * here.
1278 sock_put(pending);
1280 return err;
1283 static int
1284 vmci_transport_recv_connecting_client(struct sock *sk,
1285 struct vmci_transport_packet *pkt)
1287 struct vsock_sock *vsk;
1288 int err;
1289 int skerr;
1291 vsk = vsock_sk(sk);
1293 switch (pkt->type) {
1294 case VMCI_TRANSPORT_PACKET_TYPE_ATTACH:
1295 if (vmci_handle_is_invalid(pkt->u.handle) ||
1296 !vmci_handle_is_equal(pkt->u.handle,
1297 vmci_trans(vsk)->qp_handle)) {
1298 skerr = EPROTO;
1299 err = -EINVAL;
1300 goto destroy;
1303 /* Signify the socket is connected and wakeup the waiter in
1304 * connect(). Also place the socket in the connected table for
1305 * accounting (it can already be found since it's in the bound
1306 * table).
1308 sk->sk_state = TCP_ESTABLISHED;
1309 sk->sk_socket->state = SS_CONNECTED;
1310 vsock_insert_connected(vsk);
1311 sk->sk_state_change(sk);
1313 break;
1314 case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE:
1315 case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2:
1316 if (pkt->u.size == 0
1317 || pkt->dg.src.context != vsk->remote_addr.svm_cid
1318 || pkt->src_port != vsk->remote_addr.svm_port
1319 || !vmci_handle_is_invalid(vmci_trans(vsk)->qp_handle)
1320 || vmci_trans(vsk)->qpair
1321 || vmci_trans(vsk)->produce_size != 0
1322 || vmci_trans(vsk)->consume_size != 0
1323 || vmci_trans(vsk)->detach_sub_id != VMCI_INVALID_ID) {
1324 skerr = EPROTO;
1325 err = -EINVAL;
1327 goto destroy;
1330 err = vmci_transport_recv_connecting_client_negotiate(sk, pkt);
1331 if (err) {
1332 skerr = -err;
1333 goto destroy;
1336 break;
1337 case VMCI_TRANSPORT_PACKET_TYPE_INVALID:
1338 err = vmci_transport_recv_connecting_client_invalid(sk, pkt);
1339 if (err) {
1340 skerr = -err;
1341 goto destroy;
1344 break;
1345 case VMCI_TRANSPORT_PACKET_TYPE_RST:
1346 /* Older versions of the linux code (WS 6.5 / ESX 4.0) used to
1347 * continue processing here after they sent an INVALID packet.
1348 * This meant that we got a RST after the INVALID. We ignore a
1349 * RST after an INVALID. The common code doesn't send the RST
1350 * ... so we can hang if an old version of the common code
1351 * fails between getting a REQUEST and sending an OFFER back.
1352 * Not much we can do about it... except hope that it doesn't
1353 * happen.
1355 if (vsk->ignore_connecting_rst) {
1356 vsk->ignore_connecting_rst = false;
1357 } else {
1358 skerr = ECONNRESET;
1359 err = 0;
1360 goto destroy;
1363 break;
1364 default:
1365 /* Close and cleanup the connection. */
1366 skerr = EPROTO;
1367 err = -EINVAL;
1368 goto destroy;
1371 return 0;
1373 destroy:
1374 vmci_transport_send_reset(sk, pkt);
1376 sk->sk_state = TCP_CLOSE;
1377 sk->sk_err = skerr;
1378 sk->sk_error_report(sk);
1379 return err;
1382 static int vmci_transport_recv_connecting_client_negotiate(
1383 struct sock *sk,
1384 struct vmci_transport_packet *pkt)
1386 int err;
1387 struct vsock_sock *vsk;
1388 struct vmci_handle handle;
1389 struct vmci_qp *qpair;
1390 u32 detach_sub_id;
1391 bool is_local;
1392 u32 flags;
1393 bool old_proto = true;
1394 bool old_pkt_proto;
1395 u16 version;
1397 vsk = vsock_sk(sk);
1398 handle = VMCI_INVALID_HANDLE;
1399 detach_sub_id = VMCI_INVALID_ID;
1401 /* If we have gotten here then we should be past the point where old
1402 * linux vsock could have sent the bogus rst.
1404 vsk->sent_request = false;
1405 vsk->ignore_connecting_rst = false;
1407 /* Verify that we're OK with the proposed queue pair size */
1408 if (pkt->u.size < vmci_trans(vsk)->queue_pair_min_size ||
1409 pkt->u.size > vmci_trans(vsk)->queue_pair_max_size) {
1410 err = -EINVAL;
1411 goto destroy;
1414 /* At this point we know the CID the peer is using to talk to us. */
1416 if (vsk->local_addr.svm_cid == VMADDR_CID_ANY)
1417 vsk->local_addr.svm_cid = pkt->dg.dst.context;
1419 /* Setup the notify ops to be the highest supported version that both
1420 * the server and the client support.
1423 if (vmci_transport_old_proto_override(&old_pkt_proto)) {
1424 old_proto = old_pkt_proto;
1425 } else {
1426 if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE)
1427 old_proto = true;
1428 else if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2)
1429 old_proto = false;
1433 if (old_proto)
1434 version = VSOCK_PROTO_INVALID;
1435 else
1436 version = pkt->proto;
1438 if (!vmci_transport_proto_to_notify_struct(sk, &version, old_proto)) {
1439 err = -EINVAL;
1440 goto destroy;
1443 /* Subscribe to detach events first.
1445 * XXX We attach once for each queue pair created for now so it is easy
1446 * to find the socket (it's provided), but later we should only
1447 * subscribe once and add a way to lookup sockets by queue pair handle.
1449 err = vmci_event_subscribe(VMCI_EVENT_QP_PEER_DETACH,
1450 vmci_transport_peer_detach_cb,
1451 vmci_trans(vsk), &detach_sub_id);
1452 if (err < VMCI_SUCCESS) {
1453 err = vmci_transport_error_to_vsock_error(err);
1454 goto destroy;
1457 /* Make VMCI select the handle for us. */
1458 handle = VMCI_INVALID_HANDLE;
1459 is_local = vsk->remote_addr.svm_cid == vsk->local_addr.svm_cid;
1460 flags = is_local ? VMCI_QPFLAG_LOCAL : 0;
1462 err = vmci_transport_queue_pair_alloc(&qpair,
1463 &handle,
1464 pkt->u.size,
1465 pkt->u.size,
1466 vsk->remote_addr.svm_cid,
1467 flags,
1468 vmci_transport_is_trusted(
1469 vsk,
1470 vsk->
1471 remote_addr.svm_cid));
1472 if (err < 0)
1473 goto destroy;
1475 err = vmci_transport_send_qp_offer(sk, handle);
1476 if (err < 0) {
1477 err = vmci_transport_error_to_vsock_error(err);
1478 goto destroy;
1481 vmci_trans(vsk)->qp_handle = handle;
1482 vmci_trans(vsk)->qpair = qpair;
1484 vmci_trans(vsk)->produce_size = vmci_trans(vsk)->consume_size =
1485 pkt->u.size;
1487 vmci_trans(vsk)->detach_sub_id = detach_sub_id;
1489 vmci_trans(vsk)->notify_ops->process_negotiate(sk);
1491 return 0;
1493 destroy:
1494 if (detach_sub_id != VMCI_INVALID_ID)
1495 vmci_event_unsubscribe(detach_sub_id);
1497 if (!vmci_handle_is_invalid(handle))
1498 vmci_qpair_detach(&qpair);
1500 return err;
1503 static int
1504 vmci_transport_recv_connecting_client_invalid(struct sock *sk,
1505 struct vmci_transport_packet *pkt)
1507 int err = 0;
1508 struct vsock_sock *vsk = vsock_sk(sk);
1510 if (vsk->sent_request) {
1511 vsk->sent_request = false;
1512 vsk->ignore_connecting_rst = true;
1514 err = vmci_transport_send_conn_request(
1515 sk, vmci_trans(vsk)->queue_pair_size);
1516 if (err < 0)
1517 err = vmci_transport_error_to_vsock_error(err);
1518 else
1519 err = 0;
1523 return err;
1526 static int vmci_transport_recv_connected(struct sock *sk,
1527 struct vmci_transport_packet *pkt)
1529 struct vsock_sock *vsk;
1530 bool pkt_processed = false;
1532 /* In cases where we are closing the connection, it's sufficient to
1533 * mark the state change (and maybe error) and wake up any waiting
1534 * threads. Since this is a connected socket, it's owned by a user
1535 * process and will be cleaned up when the failure is passed back on
1536 * the current or next system call. Our system call implementations
1537 * must therefore check for error and state changes on entry and when
1538 * being awoken.
1540 switch (pkt->type) {
1541 case VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN:
1542 if (pkt->u.mode) {
1543 vsk = vsock_sk(sk);
1545 vsk->peer_shutdown |= pkt->u.mode;
1546 sk->sk_state_change(sk);
1548 break;
1550 case VMCI_TRANSPORT_PACKET_TYPE_RST:
1551 vsk = vsock_sk(sk);
1552 /* It is possible that we sent our peer a message (e.g a
1553 * WAITING_READ) right before we got notified that the peer had
1554 * detached. If that happens then we can get a RST pkt back
1555 * from our peer even though there is data available for us to
1556 * read. In that case, don't shutdown the socket completely but
1557 * instead allow the local client to finish reading data off
1558 * the queuepair. Always treat a RST pkt in connected mode like
1559 * a clean shutdown.
1561 sock_set_flag(sk, SOCK_DONE);
1562 vsk->peer_shutdown = SHUTDOWN_MASK;
1563 if (vsock_stream_has_data(vsk) <= 0)
1564 sk->sk_state = TCP_CLOSING;
1566 sk->sk_state_change(sk);
1567 break;
1569 default:
1570 vsk = vsock_sk(sk);
1571 vmci_trans(vsk)->notify_ops->handle_notify_pkt(
1572 sk, pkt, false, NULL, NULL,
1573 &pkt_processed);
1574 if (!pkt_processed)
1575 return -EINVAL;
1577 break;
1580 return 0;
1583 static int vmci_transport_socket_init(struct vsock_sock *vsk,
1584 struct vsock_sock *psk)
1586 vsk->trans = kmalloc(sizeof(struct vmci_transport), GFP_KERNEL);
1587 if (!vsk->trans)
1588 return -ENOMEM;
1590 vmci_trans(vsk)->dg_handle = VMCI_INVALID_HANDLE;
1591 vmci_trans(vsk)->qp_handle = VMCI_INVALID_HANDLE;
1592 vmci_trans(vsk)->qpair = NULL;
1593 vmci_trans(vsk)->produce_size = vmci_trans(vsk)->consume_size = 0;
1594 vmci_trans(vsk)->detach_sub_id = VMCI_INVALID_ID;
1595 vmci_trans(vsk)->notify_ops = NULL;
1596 INIT_LIST_HEAD(&vmci_trans(vsk)->elem);
1597 vmci_trans(vsk)->sk = &vsk->sk;
1598 spin_lock_init(&vmci_trans(vsk)->lock);
1599 if (psk) {
1600 vmci_trans(vsk)->queue_pair_size =
1601 vmci_trans(psk)->queue_pair_size;
1602 vmci_trans(vsk)->queue_pair_min_size =
1603 vmci_trans(psk)->queue_pair_min_size;
1604 vmci_trans(vsk)->queue_pair_max_size =
1605 vmci_trans(psk)->queue_pair_max_size;
1606 } else {
1607 vmci_trans(vsk)->queue_pair_size =
1608 VMCI_TRANSPORT_DEFAULT_QP_SIZE;
1609 vmci_trans(vsk)->queue_pair_min_size =
1610 VMCI_TRANSPORT_DEFAULT_QP_SIZE_MIN;
1611 vmci_trans(vsk)->queue_pair_max_size =
1612 VMCI_TRANSPORT_DEFAULT_QP_SIZE_MAX;
1615 return 0;
1618 static void vmci_transport_free_resources(struct list_head *transport_list)
1620 while (!list_empty(transport_list)) {
1621 struct vmci_transport *transport =
1622 list_first_entry(transport_list, struct vmci_transport,
1623 elem);
1624 list_del(&transport->elem);
1626 if (transport->detach_sub_id != VMCI_INVALID_ID) {
1627 vmci_event_unsubscribe(transport->detach_sub_id);
1628 transport->detach_sub_id = VMCI_INVALID_ID;
1631 if (!vmci_handle_is_invalid(transport->qp_handle)) {
1632 vmci_qpair_detach(&transport->qpair);
1633 transport->qp_handle = VMCI_INVALID_HANDLE;
1634 transport->produce_size = 0;
1635 transport->consume_size = 0;
1638 kfree(transport);
1642 static void vmci_transport_cleanup(struct work_struct *work)
1644 LIST_HEAD(pending);
1646 spin_lock_bh(&vmci_transport_cleanup_lock);
1647 list_replace_init(&vmci_transport_cleanup_list, &pending);
1648 spin_unlock_bh(&vmci_transport_cleanup_lock);
1649 vmci_transport_free_resources(&pending);
1652 static void vmci_transport_destruct(struct vsock_sock *vsk)
1654 /* transport can be NULL if we hit a failure at init() time */
1655 if (!vmci_trans(vsk))
1656 return;
1658 /* Ensure that the detach callback doesn't use the sk/vsk
1659 * we are about to destruct.
1661 spin_lock_bh(&vmci_trans(vsk)->lock);
1662 vmci_trans(vsk)->sk = NULL;
1663 spin_unlock_bh(&vmci_trans(vsk)->lock);
1665 if (vmci_trans(vsk)->notify_ops)
1666 vmci_trans(vsk)->notify_ops->socket_destruct(vsk);
1668 spin_lock_bh(&vmci_transport_cleanup_lock);
1669 list_add(&vmci_trans(vsk)->elem, &vmci_transport_cleanup_list);
1670 spin_unlock_bh(&vmci_transport_cleanup_lock);
1671 schedule_work(&vmci_transport_cleanup_work);
1673 vsk->trans = NULL;
1676 static void vmci_transport_release(struct vsock_sock *vsk)
1678 vsock_remove_sock(vsk);
1680 if (!vmci_handle_is_invalid(vmci_trans(vsk)->dg_handle)) {
1681 vmci_datagram_destroy_handle(vmci_trans(vsk)->dg_handle);
1682 vmci_trans(vsk)->dg_handle = VMCI_INVALID_HANDLE;
1686 static int vmci_transport_dgram_bind(struct vsock_sock *vsk,
1687 struct sockaddr_vm *addr)
1689 u32 port;
1690 u32 flags;
1691 int err;
1693 /* VMCI will select a resource ID for us if we provide
1694 * VMCI_INVALID_ID.
1696 port = addr->svm_port == VMADDR_PORT_ANY ?
1697 VMCI_INVALID_ID : addr->svm_port;
1699 if (port <= LAST_RESERVED_PORT && !capable(CAP_NET_BIND_SERVICE))
1700 return -EACCES;
1702 flags = addr->svm_cid == VMADDR_CID_ANY ?
1703 VMCI_FLAG_ANYCID_DG_HND : 0;
1705 err = vmci_transport_datagram_create_hnd(port, flags,
1706 vmci_transport_recv_dgram_cb,
1707 &vsk->sk,
1708 &vmci_trans(vsk)->dg_handle);
1709 if (err < VMCI_SUCCESS)
1710 return vmci_transport_error_to_vsock_error(err);
1711 vsock_addr_init(&vsk->local_addr, addr->svm_cid,
1712 vmci_trans(vsk)->dg_handle.resource);
1714 return 0;
1717 static int vmci_transport_dgram_enqueue(
1718 struct vsock_sock *vsk,
1719 struct sockaddr_vm *remote_addr,
1720 struct msghdr *msg,
1721 size_t len)
1723 int err;
1724 struct vmci_datagram *dg;
1726 if (len > VMCI_MAX_DG_PAYLOAD_SIZE)
1727 return -EMSGSIZE;
1729 if (!vmci_transport_allow_dgram(vsk, remote_addr->svm_cid))
1730 return -EPERM;
1732 /* Allocate a buffer for the user's message and our packet header. */
1733 dg = kmalloc(len + sizeof(*dg), GFP_KERNEL);
1734 if (!dg)
1735 return -ENOMEM;
1737 memcpy_from_msg(VMCI_DG_PAYLOAD(dg), msg, len);
1739 dg->dst = vmci_make_handle(remote_addr->svm_cid,
1740 remote_addr->svm_port);
1741 dg->src = vmci_make_handle(vsk->local_addr.svm_cid,
1742 vsk->local_addr.svm_port);
1743 dg->payload_size = len;
1745 err = vmci_datagram_send(dg);
1746 kfree(dg);
1747 if (err < 0)
1748 return vmci_transport_error_to_vsock_error(err);
1750 return err - sizeof(*dg);
1753 static int vmci_transport_dgram_dequeue(struct vsock_sock *vsk,
1754 struct msghdr *msg, size_t len,
1755 int flags)
1757 int err;
1758 int noblock;
1759 struct vmci_datagram *dg;
1760 size_t payload_len;
1761 struct sk_buff *skb;
1763 noblock = flags & MSG_DONTWAIT;
1765 if (flags & MSG_OOB || flags & MSG_ERRQUEUE)
1766 return -EOPNOTSUPP;
1768 /* Retrieve the head sk_buff from the socket's receive queue. */
1769 err = 0;
1770 skb = skb_recv_datagram(&vsk->sk, flags, noblock, &err);
1771 if (!skb)
1772 return err;
1774 dg = (struct vmci_datagram *)skb->data;
1775 if (!dg)
1776 /* err is 0, meaning we read zero bytes. */
1777 goto out;
1779 payload_len = dg->payload_size;
1780 /* Ensure the sk_buff matches the payload size claimed in the packet. */
1781 if (payload_len != skb->len - sizeof(*dg)) {
1782 err = -EINVAL;
1783 goto out;
1786 if (payload_len > len) {
1787 payload_len = len;
1788 msg->msg_flags |= MSG_TRUNC;
1791 /* Place the datagram payload in the user's iovec. */
1792 err = skb_copy_datagram_msg(skb, sizeof(*dg), msg, payload_len);
1793 if (err)
1794 goto out;
1796 if (msg->msg_name) {
1797 /* Provide the address of the sender. */
1798 DECLARE_SOCKADDR(struct sockaddr_vm *, vm_addr, msg->msg_name);
1799 vsock_addr_init(vm_addr, dg->src.context, dg->src.resource);
1800 msg->msg_namelen = sizeof(*vm_addr);
1802 err = payload_len;
1804 out:
1805 skb_free_datagram(&vsk->sk, skb);
1806 return err;
1809 static bool vmci_transport_dgram_allow(u32 cid, u32 port)
1811 if (cid == VMADDR_CID_HYPERVISOR) {
1812 /* Registrations of PBRPC Servers do not modify VMX/Hypervisor
1813 * state and are allowed.
1815 return port == VMCI_UNITY_PBRPC_REGISTER;
1818 return true;
1821 static int vmci_transport_connect(struct vsock_sock *vsk)
1823 int err;
1824 bool old_pkt_proto = false;
1825 struct sock *sk = &vsk->sk;
1827 if (vmci_transport_old_proto_override(&old_pkt_proto) &&
1828 old_pkt_proto) {
1829 err = vmci_transport_send_conn_request(
1830 sk, vmci_trans(vsk)->queue_pair_size);
1831 if (err < 0) {
1832 sk->sk_state = TCP_CLOSE;
1833 return err;
1835 } else {
1836 int supported_proto_versions =
1837 vmci_transport_new_proto_supported_versions();
1838 err = vmci_transport_send_conn_request2(
1839 sk, vmci_trans(vsk)->queue_pair_size,
1840 supported_proto_versions);
1841 if (err < 0) {
1842 sk->sk_state = TCP_CLOSE;
1843 return err;
1846 vsk->sent_request = true;
1849 return err;
1852 static ssize_t vmci_transport_stream_dequeue(
1853 struct vsock_sock *vsk,
1854 struct msghdr *msg,
1855 size_t len,
1856 int flags)
1858 if (flags & MSG_PEEK)
1859 return vmci_qpair_peekv(vmci_trans(vsk)->qpair, msg, len, 0);
1860 else
1861 return vmci_qpair_dequev(vmci_trans(vsk)->qpair, msg, len, 0);
1864 static ssize_t vmci_transport_stream_enqueue(
1865 struct vsock_sock *vsk,
1866 struct msghdr *msg,
1867 size_t len)
1869 return vmci_qpair_enquev(vmci_trans(vsk)->qpair, msg, len, 0);
1872 static s64 vmci_transport_stream_has_data(struct vsock_sock *vsk)
1874 return vmci_qpair_consume_buf_ready(vmci_trans(vsk)->qpair);
1877 static s64 vmci_transport_stream_has_space(struct vsock_sock *vsk)
1879 return vmci_qpair_produce_free_space(vmci_trans(vsk)->qpair);
1882 static u64 vmci_transport_stream_rcvhiwat(struct vsock_sock *vsk)
1884 return vmci_trans(vsk)->consume_size;
1887 static bool vmci_transport_stream_is_active(struct vsock_sock *vsk)
1889 return !vmci_handle_is_invalid(vmci_trans(vsk)->qp_handle);
1892 static u64 vmci_transport_get_buffer_size(struct vsock_sock *vsk)
1894 return vmci_trans(vsk)->queue_pair_size;
1897 static u64 vmci_transport_get_min_buffer_size(struct vsock_sock *vsk)
1899 return vmci_trans(vsk)->queue_pair_min_size;
1902 static u64 vmci_transport_get_max_buffer_size(struct vsock_sock *vsk)
1904 return vmci_trans(vsk)->queue_pair_max_size;
1907 static void vmci_transport_set_buffer_size(struct vsock_sock *vsk, u64 val)
1909 if (val < vmci_trans(vsk)->queue_pair_min_size)
1910 vmci_trans(vsk)->queue_pair_min_size = val;
1911 if (val > vmci_trans(vsk)->queue_pair_max_size)
1912 vmci_trans(vsk)->queue_pair_max_size = val;
1913 vmci_trans(vsk)->queue_pair_size = val;
1916 static void vmci_transport_set_min_buffer_size(struct vsock_sock *vsk,
1917 u64 val)
1919 if (val > vmci_trans(vsk)->queue_pair_size)
1920 vmci_trans(vsk)->queue_pair_size = val;
1921 vmci_trans(vsk)->queue_pair_min_size = val;
1924 static void vmci_transport_set_max_buffer_size(struct vsock_sock *vsk,
1925 u64 val)
1927 if (val < vmci_trans(vsk)->queue_pair_size)
1928 vmci_trans(vsk)->queue_pair_size = val;
1929 vmci_trans(vsk)->queue_pair_max_size = val;
1932 static int vmci_transport_notify_poll_in(
1933 struct vsock_sock *vsk,
1934 size_t target,
1935 bool *data_ready_now)
1937 return vmci_trans(vsk)->notify_ops->poll_in(
1938 &vsk->sk, target, data_ready_now);
1941 static int vmci_transport_notify_poll_out(
1942 struct vsock_sock *vsk,
1943 size_t target,
1944 bool *space_available_now)
1946 return vmci_trans(vsk)->notify_ops->poll_out(
1947 &vsk->sk, target, space_available_now);
1950 static int vmci_transport_notify_recv_init(
1951 struct vsock_sock *vsk,
1952 size_t target,
1953 struct vsock_transport_recv_notify_data *data)
1955 return vmci_trans(vsk)->notify_ops->recv_init(
1956 &vsk->sk, target,
1957 (struct vmci_transport_recv_notify_data *)data);
1960 static int vmci_transport_notify_recv_pre_block(
1961 struct vsock_sock *vsk,
1962 size_t target,
1963 struct vsock_transport_recv_notify_data *data)
1965 return vmci_trans(vsk)->notify_ops->recv_pre_block(
1966 &vsk->sk, target,
1967 (struct vmci_transport_recv_notify_data *)data);
1970 static int vmci_transport_notify_recv_pre_dequeue(
1971 struct vsock_sock *vsk,
1972 size_t target,
1973 struct vsock_transport_recv_notify_data *data)
1975 return vmci_trans(vsk)->notify_ops->recv_pre_dequeue(
1976 &vsk->sk, target,
1977 (struct vmci_transport_recv_notify_data *)data);
1980 static int vmci_transport_notify_recv_post_dequeue(
1981 struct vsock_sock *vsk,
1982 size_t target,
1983 ssize_t copied,
1984 bool data_read,
1985 struct vsock_transport_recv_notify_data *data)
1987 return vmci_trans(vsk)->notify_ops->recv_post_dequeue(
1988 &vsk->sk, target, copied, data_read,
1989 (struct vmci_transport_recv_notify_data *)data);
1992 static int vmci_transport_notify_send_init(
1993 struct vsock_sock *vsk,
1994 struct vsock_transport_send_notify_data *data)
1996 return vmci_trans(vsk)->notify_ops->send_init(
1997 &vsk->sk,
1998 (struct vmci_transport_send_notify_data *)data);
2001 static int vmci_transport_notify_send_pre_block(
2002 struct vsock_sock *vsk,
2003 struct vsock_transport_send_notify_data *data)
2005 return vmci_trans(vsk)->notify_ops->send_pre_block(
2006 &vsk->sk,
2007 (struct vmci_transport_send_notify_data *)data);
2010 static int vmci_transport_notify_send_pre_enqueue(
2011 struct vsock_sock *vsk,
2012 struct vsock_transport_send_notify_data *data)
2014 return vmci_trans(vsk)->notify_ops->send_pre_enqueue(
2015 &vsk->sk,
2016 (struct vmci_transport_send_notify_data *)data);
2019 static int vmci_transport_notify_send_post_enqueue(
2020 struct vsock_sock *vsk,
2021 ssize_t written,
2022 struct vsock_transport_send_notify_data *data)
2024 return vmci_trans(vsk)->notify_ops->send_post_enqueue(
2025 &vsk->sk, written,
2026 (struct vmci_transport_send_notify_data *)data);
2029 static bool vmci_transport_old_proto_override(bool *old_pkt_proto)
2031 if (PROTOCOL_OVERRIDE != -1) {
2032 if (PROTOCOL_OVERRIDE == 0)
2033 *old_pkt_proto = true;
2034 else
2035 *old_pkt_proto = false;
2037 pr_info("Proto override in use\n");
2038 return true;
2041 return false;
2044 static bool vmci_transport_proto_to_notify_struct(struct sock *sk,
2045 u16 *proto,
2046 bool old_pkt_proto)
2048 struct vsock_sock *vsk = vsock_sk(sk);
2050 if (old_pkt_proto) {
2051 if (*proto != VSOCK_PROTO_INVALID) {
2052 pr_err("Can't set both an old and new protocol\n");
2053 return false;
2055 vmci_trans(vsk)->notify_ops = &vmci_transport_notify_pkt_ops;
2056 goto exit;
2059 switch (*proto) {
2060 case VSOCK_PROTO_PKT_ON_NOTIFY:
2061 vmci_trans(vsk)->notify_ops =
2062 &vmci_transport_notify_pkt_q_state_ops;
2063 break;
2064 default:
2065 pr_err("Unknown notify protocol version\n");
2066 return false;
2069 exit:
2070 vmci_trans(vsk)->notify_ops->socket_init(sk);
2071 return true;
2074 static u16 vmci_transport_new_proto_supported_versions(void)
2076 if (PROTOCOL_OVERRIDE != -1)
2077 return PROTOCOL_OVERRIDE;
2079 return VSOCK_PROTO_ALL_SUPPORTED;
2082 static u32 vmci_transport_get_local_cid(void)
2084 return vmci_get_context_id();
2087 static const struct vsock_transport vmci_transport = {
2088 .init = vmci_transport_socket_init,
2089 .destruct = vmci_transport_destruct,
2090 .release = vmci_transport_release,
2091 .connect = vmci_transport_connect,
2092 .dgram_bind = vmci_transport_dgram_bind,
2093 .dgram_dequeue = vmci_transport_dgram_dequeue,
2094 .dgram_enqueue = vmci_transport_dgram_enqueue,
2095 .dgram_allow = vmci_transport_dgram_allow,
2096 .stream_dequeue = vmci_transport_stream_dequeue,
2097 .stream_enqueue = vmci_transport_stream_enqueue,
2098 .stream_has_data = vmci_transport_stream_has_data,
2099 .stream_has_space = vmci_transport_stream_has_space,
2100 .stream_rcvhiwat = vmci_transport_stream_rcvhiwat,
2101 .stream_is_active = vmci_transport_stream_is_active,
2102 .stream_allow = vmci_transport_stream_allow,
2103 .notify_poll_in = vmci_transport_notify_poll_in,
2104 .notify_poll_out = vmci_transport_notify_poll_out,
2105 .notify_recv_init = vmci_transport_notify_recv_init,
2106 .notify_recv_pre_block = vmci_transport_notify_recv_pre_block,
2107 .notify_recv_pre_dequeue = vmci_transport_notify_recv_pre_dequeue,
2108 .notify_recv_post_dequeue = vmci_transport_notify_recv_post_dequeue,
2109 .notify_send_init = vmci_transport_notify_send_init,
2110 .notify_send_pre_block = vmci_transport_notify_send_pre_block,
2111 .notify_send_pre_enqueue = vmci_transport_notify_send_pre_enqueue,
2112 .notify_send_post_enqueue = vmci_transport_notify_send_post_enqueue,
2113 .shutdown = vmci_transport_shutdown,
2114 .set_buffer_size = vmci_transport_set_buffer_size,
2115 .set_min_buffer_size = vmci_transport_set_min_buffer_size,
2116 .set_max_buffer_size = vmci_transport_set_max_buffer_size,
2117 .get_buffer_size = vmci_transport_get_buffer_size,
2118 .get_min_buffer_size = vmci_transport_get_min_buffer_size,
2119 .get_max_buffer_size = vmci_transport_get_max_buffer_size,
2120 .get_local_cid = vmci_transport_get_local_cid,
2123 static int __init vmci_transport_init(void)
2125 int err;
2127 /* Create the datagram handle that we will use to send and receive all
2128 * VSocket control messages for this context.
2130 err = vmci_transport_datagram_create_hnd(VMCI_TRANSPORT_PACKET_RID,
2131 VMCI_FLAG_ANYCID_DG_HND,
2132 vmci_transport_recv_stream_cb,
2133 NULL,
2134 &vmci_transport_stream_handle);
2135 if (err < VMCI_SUCCESS) {
2136 pr_err("Unable to create datagram handle. (%d)\n", err);
2137 return vmci_transport_error_to_vsock_error(err);
2140 err = vmci_event_subscribe(VMCI_EVENT_QP_RESUMED,
2141 vmci_transport_qp_resumed_cb,
2142 NULL, &vmci_transport_qp_resumed_sub_id);
2143 if (err < VMCI_SUCCESS) {
2144 pr_err("Unable to subscribe to resumed event. (%d)\n", err);
2145 err = vmci_transport_error_to_vsock_error(err);
2146 vmci_transport_qp_resumed_sub_id = VMCI_INVALID_ID;
2147 goto err_destroy_stream_handle;
2150 err = vsock_core_init(&vmci_transport);
2151 if (err < 0)
2152 goto err_unsubscribe;
2154 return 0;
2156 err_unsubscribe:
2157 vmci_event_unsubscribe(vmci_transport_qp_resumed_sub_id);
2158 err_destroy_stream_handle:
2159 vmci_datagram_destroy_handle(vmci_transport_stream_handle);
2160 return err;
2162 module_init(vmci_transport_init);
2164 static void __exit vmci_transport_exit(void)
2166 cancel_work_sync(&vmci_transport_cleanup_work);
2167 vmci_transport_free_resources(&vmci_transport_cleanup_list);
2169 if (!vmci_handle_is_invalid(vmci_transport_stream_handle)) {
2170 if (vmci_datagram_destroy_handle(
2171 vmci_transport_stream_handle) != VMCI_SUCCESS)
2172 pr_err("Couldn't destroy datagram handle\n");
2173 vmci_transport_stream_handle = VMCI_INVALID_HANDLE;
2176 if (vmci_transport_qp_resumed_sub_id != VMCI_INVALID_ID) {
2177 vmci_event_unsubscribe(vmci_transport_qp_resumed_sub_id);
2178 vmci_transport_qp_resumed_sub_id = VMCI_INVALID_ID;
2181 vsock_core_exit();
2183 module_exit(vmci_transport_exit);
2185 MODULE_AUTHOR("VMware, Inc.");
2186 MODULE_DESCRIPTION("VMCI transport for Virtual Sockets");
2187 MODULE_VERSION("1.0.5.0-k");
2188 MODULE_LICENSE("GPL v2");
2189 MODULE_ALIAS("vmware_vsock");
2190 MODULE_ALIAS_NETPROTO(PF_VSOCK);