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