Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * VMware vSockets Driver
0004  *
0005  * Copyright (C) 2007-2013 VMware, Inc. All rights reserved.
0006  */
0007 
0008 #include <linux/types.h>
0009 #include <linux/bitops.h>
0010 #include <linux/cred.h>
0011 #include <linux/init.h>
0012 #include <linux/io.h>
0013 #include <linux/kernel.h>
0014 #include <linux/kmod.h>
0015 #include <linux/list.h>
0016 #include <linux/module.h>
0017 #include <linux/mutex.h>
0018 #include <linux/net.h>
0019 #include <linux/poll.h>
0020 #include <linux/skbuff.h>
0021 #include <linux/smp.h>
0022 #include <linux/socket.h>
0023 #include <linux/stddef.h>
0024 #include <linux/unistd.h>
0025 #include <linux/wait.h>
0026 #include <linux/workqueue.h>
0027 #include <net/sock.h>
0028 #include <net/af_vsock.h>
0029 
0030 #include "vmci_transport_notify.h"
0031 
0032 static int vmci_transport_recv_dgram_cb(void *data, struct vmci_datagram *dg);
0033 static int vmci_transport_recv_stream_cb(void *data, struct vmci_datagram *dg);
0034 static void vmci_transport_peer_detach_cb(u32 sub_id,
0035                       const struct vmci_event_data *ed,
0036                       void *client_data);
0037 static void vmci_transport_recv_pkt_work(struct work_struct *work);
0038 static void vmci_transport_cleanup(struct work_struct *work);
0039 static int vmci_transport_recv_listen(struct sock *sk,
0040                       struct vmci_transport_packet *pkt);
0041 static int vmci_transport_recv_connecting_server(
0042                     struct sock *sk,
0043                     struct sock *pending,
0044                     struct vmci_transport_packet *pkt);
0045 static int vmci_transport_recv_connecting_client(
0046                     struct sock *sk,
0047                     struct vmci_transport_packet *pkt);
0048 static int vmci_transport_recv_connecting_client_negotiate(
0049                     struct sock *sk,
0050                     struct vmci_transport_packet *pkt);
0051 static int vmci_transport_recv_connecting_client_invalid(
0052                     struct sock *sk,
0053                     struct vmci_transport_packet *pkt);
0054 static int vmci_transport_recv_connected(struct sock *sk,
0055                      struct vmci_transport_packet *pkt);
0056 static bool vmci_transport_old_proto_override(bool *old_pkt_proto);
0057 static u16 vmci_transport_new_proto_supported_versions(void);
0058 static bool vmci_transport_proto_to_notify_struct(struct sock *sk, u16 *proto,
0059                           bool old_pkt_proto);
0060 static bool vmci_check_transport(struct vsock_sock *vsk);
0061 
0062 struct vmci_transport_recv_pkt_info {
0063     struct work_struct work;
0064     struct sock *sk;
0065     struct vmci_transport_packet pkt;
0066 };
0067 
0068 static LIST_HEAD(vmci_transport_cleanup_list);
0069 static DEFINE_SPINLOCK(vmci_transport_cleanup_lock);
0070 static DECLARE_WORK(vmci_transport_cleanup_work, vmci_transport_cleanup);
0071 
0072 static struct vmci_handle vmci_transport_stream_handle = { VMCI_INVALID_ID,
0073                                VMCI_INVALID_ID };
0074 static u32 vmci_transport_qp_resumed_sub_id = VMCI_INVALID_ID;
0075 
0076 static int PROTOCOL_OVERRIDE = -1;
0077 
0078 static struct vsock_transport vmci_transport; /* forward declaration */
0079 
0080 /* Helper function to convert from a VMCI error code to a VSock error code. */
0081 
0082 static s32 vmci_transport_error_to_vsock_error(s32 vmci_error)
0083 {
0084     switch (vmci_error) {
0085     case VMCI_ERROR_NO_MEM:
0086         return -ENOMEM;
0087     case VMCI_ERROR_DUPLICATE_ENTRY:
0088     case VMCI_ERROR_ALREADY_EXISTS:
0089         return -EADDRINUSE;
0090     case VMCI_ERROR_NO_ACCESS:
0091         return -EPERM;
0092     case VMCI_ERROR_NO_RESOURCES:
0093         return -ENOBUFS;
0094     case VMCI_ERROR_INVALID_RESOURCE:
0095         return -EHOSTUNREACH;
0096     case VMCI_ERROR_INVALID_ARGS:
0097     default:
0098         break;
0099     }
0100     return -EINVAL;
0101 }
0102 
0103 static u32 vmci_transport_peer_rid(u32 peer_cid)
0104 {
0105     if (VMADDR_CID_HYPERVISOR == peer_cid)
0106         return VMCI_TRANSPORT_HYPERVISOR_PACKET_RID;
0107 
0108     return VMCI_TRANSPORT_PACKET_RID;
0109 }
0110 
0111 static inline void
0112 vmci_transport_packet_init(struct vmci_transport_packet *pkt,
0113                struct sockaddr_vm *src,
0114                struct sockaddr_vm *dst,
0115                u8 type,
0116                u64 size,
0117                u64 mode,
0118                struct vmci_transport_waiting_info *wait,
0119                u16 proto,
0120                struct vmci_handle handle)
0121 {
0122     /* We register the stream control handler as an any cid handle so we
0123      * must always send from a source address of VMADDR_CID_ANY
0124      */
0125     pkt->dg.src = vmci_make_handle(VMADDR_CID_ANY,
0126                        VMCI_TRANSPORT_PACKET_RID);
0127     pkt->dg.dst = vmci_make_handle(dst->svm_cid,
0128                        vmci_transport_peer_rid(dst->svm_cid));
0129     pkt->dg.payload_size = sizeof(*pkt) - sizeof(pkt->dg);
0130     pkt->version = VMCI_TRANSPORT_PACKET_VERSION;
0131     pkt->type = type;
0132     pkt->src_port = src->svm_port;
0133     pkt->dst_port = dst->svm_port;
0134     memset(&pkt->proto, 0, sizeof(pkt->proto));
0135     memset(&pkt->_reserved2, 0, sizeof(pkt->_reserved2));
0136 
0137     switch (pkt->type) {
0138     case VMCI_TRANSPORT_PACKET_TYPE_INVALID:
0139         pkt->u.size = 0;
0140         break;
0141 
0142     case VMCI_TRANSPORT_PACKET_TYPE_REQUEST:
0143     case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE:
0144         pkt->u.size = size;
0145         break;
0146 
0147     case VMCI_TRANSPORT_PACKET_TYPE_OFFER:
0148     case VMCI_TRANSPORT_PACKET_TYPE_ATTACH:
0149         pkt->u.handle = handle;
0150         break;
0151 
0152     case VMCI_TRANSPORT_PACKET_TYPE_WROTE:
0153     case VMCI_TRANSPORT_PACKET_TYPE_READ:
0154     case VMCI_TRANSPORT_PACKET_TYPE_RST:
0155         pkt->u.size = 0;
0156         break;
0157 
0158     case VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN:
0159         pkt->u.mode = mode;
0160         break;
0161 
0162     case VMCI_TRANSPORT_PACKET_TYPE_WAITING_READ:
0163     case VMCI_TRANSPORT_PACKET_TYPE_WAITING_WRITE:
0164         memcpy(&pkt->u.wait, wait, sizeof(pkt->u.wait));
0165         break;
0166 
0167     case VMCI_TRANSPORT_PACKET_TYPE_REQUEST2:
0168     case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2:
0169         pkt->u.size = size;
0170         pkt->proto = proto;
0171         break;
0172     }
0173 }
0174 
0175 static inline void
0176 vmci_transport_packet_get_addresses(struct vmci_transport_packet *pkt,
0177                     struct sockaddr_vm *local,
0178                     struct sockaddr_vm *remote)
0179 {
0180     vsock_addr_init(local, pkt->dg.dst.context, pkt->dst_port);
0181     vsock_addr_init(remote, pkt->dg.src.context, pkt->src_port);
0182 }
0183 
0184 static int
0185 __vmci_transport_send_control_pkt(struct vmci_transport_packet *pkt,
0186                   struct sockaddr_vm *src,
0187                   struct sockaddr_vm *dst,
0188                   enum vmci_transport_packet_type type,
0189                   u64 size,
0190                   u64 mode,
0191                   struct vmci_transport_waiting_info *wait,
0192                   u16 proto,
0193                   struct vmci_handle handle,
0194                   bool convert_error)
0195 {
0196     int err;
0197 
0198     vmci_transport_packet_init(pkt, src, dst, type, size, mode, wait,
0199                    proto, handle);
0200     err = vmci_datagram_send(&pkt->dg);
0201     if (convert_error && (err < 0))
0202         return vmci_transport_error_to_vsock_error(err);
0203 
0204     return err;
0205 }
0206 
0207 static int
0208 vmci_transport_reply_control_pkt_fast(struct vmci_transport_packet *pkt,
0209                       enum vmci_transport_packet_type type,
0210                       u64 size,
0211                       u64 mode,
0212                       struct vmci_transport_waiting_info *wait,
0213                       struct vmci_handle handle)
0214 {
0215     struct vmci_transport_packet reply;
0216     struct sockaddr_vm src, dst;
0217 
0218     if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_RST) {
0219         return 0;
0220     } else {
0221         vmci_transport_packet_get_addresses(pkt, &src, &dst);
0222         return __vmci_transport_send_control_pkt(&reply, &src, &dst,
0223                              type,
0224                              size, mode, wait,
0225                              VSOCK_PROTO_INVALID,
0226                              handle, true);
0227     }
0228 }
0229 
0230 static int
0231 vmci_transport_send_control_pkt_bh(struct sockaddr_vm *src,
0232                    struct sockaddr_vm *dst,
0233                    enum vmci_transport_packet_type type,
0234                    u64 size,
0235                    u64 mode,
0236                    struct vmci_transport_waiting_info *wait,
0237                    struct vmci_handle handle)
0238 {
0239     /* Note that it is safe to use a single packet across all CPUs since
0240      * two tasklets of the same type are guaranteed to not ever run
0241      * simultaneously. If that ever changes, or VMCI stops using tasklets,
0242      * we can use per-cpu packets.
0243      */
0244     static struct vmci_transport_packet pkt;
0245 
0246     return __vmci_transport_send_control_pkt(&pkt, src, dst, type,
0247                          size, mode, wait,
0248                          VSOCK_PROTO_INVALID, handle,
0249                          false);
0250 }
0251 
0252 static int
0253 vmci_transport_alloc_send_control_pkt(struct sockaddr_vm *src,
0254                       struct sockaddr_vm *dst,
0255                       enum vmci_transport_packet_type type,
0256                       u64 size,
0257                       u64 mode,
0258                       struct vmci_transport_waiting_info *wait,
0259                       u16 proto,
0260                       struct vmci_handle handle)
0261 {
0262     struct vmci_transport_packet *pkt;
0263     int err;
0264 
0265     pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
0266     if (!pkt)
0267         return -ENOMEM;
0268 
0269     err = __vmci_transport_send_control_pkt(pkt, src, dst, type, size,
0270                         mode, wait, proto, handle,
0271                         true);
0272     kfree(pkt);
0273 
0274     return err;
0275 }
0276 
0277 static int
0278 vmci_transport_send_control_pkt(struct sock *sk,
0279                 enum vmci_transport_packet_type type,
0280                 u64 size,
0281                 u64 mode,
0282                 struct vmci_transport_waiting_info *wait,
0283                 u16 proto,
0284                 struct vmci_handle handle)
0285 {
0286     struct vsock_sock *vsk;
0287 
0288     vsk = vsock_sk(sk);
0289 
0290     if (!vsock_addr_bound(&vsk->local_addr))
0291         return -EINVAL;
0292 
0293     if (!vsock_addr_bound(&vsk->remote_addr))
0294         return -EINVAL;
0295 
0296     return vmci_transport_alloc_send_control_pkt(&vsk->local_addr,
0297                              &vsk->remote_addr,
0298                              type, size, mode,
0299                              wait, proto, handle);
0300 }
0301 
0302 static int vmci_transport_send_reset_bh(struct sockaddr_vm *dst,
0303                     struct sockaddr_vm *src,
0304                     struct vmci_transport_packet *pkt)
0305 {
0306     if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_RST)
0307         return 0;
0308     return vmci_transport_send_control_pkt_bh(
0309                     dst, src,
0310                     VMCI_TRANSPORT_PACKET_TYPE_RST, 0,
0311                     0, NULL, VMCI_INVALID_HANDLE);
0312 }
0313 
0314 static int vmci_transport_send_reset(struct sock *sk,
0315                      struct vmci_transport_packet *pkt)
0316 {
0317     struct sockaddr_vm *dst_ptr;
0318     struct sockaddr_vm dst;
0319     struct vsock_sock *vsk;
0320 
0321     if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_RST)
0322         return 0;
0323 
0324     vsk = vsock_sk(sk);
0325 
0326     if (!vsock_addr_bound(&vsk->local_addr))
0327         return -EINVAL;
0328 
0329     if (vsock_addr_bound(&vsk->remote_addr)) {
0330         dst_ptr = &vsk->remote_addr;
0331     } else {
0332         vsock_addr_init(&dst, pkt->dg.src.context,
0333                 pkt->src_port);
0334         dst_ptr = &dst;
0335     }
0336     return vmci_transport_alloc_send_control_pkt(&vsk->local_addr, dst_ptr,
0337                          VMCI_TRANSPORT_PACKET_TYPE_RST,
0338                          0, 0, NULL, VSOCK_PROTO_INVALID,
0339                          VMCI_INVALID_HANDLE);
0340 }
0341 
0342 static int vmci_transport_send_negotiate(struct sock *sk, size_t size)
0343 {
0344     return vmci_transport_send_control_pkt(
0345                     sk,
0346                     VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE,
0347                     size, 0, NULL,
0348                     VSOCK_PROTO_INVALID,
0349                     VMCI_INVALID_HANDLE);
0350 }
0351 
0352 static int vmci_transport_send_negotiate2(struct sock *sk, size_t size,
0353                       u16 version)
0354 {
0355     return vmci_transport_send_control_pkt(
0356                     sk,
0357                     VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2,
0358                     size, 0, NULL, version,
0359                     VMCI_INVALID_HANDLE);
0360 }
0361 
0362 static int vmci_transport_send_qp_offer(struct sock *sk,
0363                     struct vmci_handle handle)
0364 {
0365     return vmci_transport_send_control_pkt(
0366                     sk, VMCI_TRANSPORT_PACKET_TYPE_OFFER, 0,
0367                     0, NULL,
0368                     VSOCK_PROTO_INVALID, handle);
0369 }
0370 
0371 static int vmci_transport_send_attach(struct sock *sk,
0372                       struct vmci_handle handle)
0373 {
0374     return vmci_transport_send_control_pkt(
0375                     sk, VMCI_TRANSPORT_PACKET_TYPE_ATTACH,
0376                     0, 0, NULL, VSOCK_PROTO_INVALID,
0377                     handle);
0378 }
0379 
0380 static int vmci_transport_reply_reset(struct vmci_transport_packet *pkt)
0381 {
0382     return vmci_transport_reply_control_pkt_fast(
0383                         pkt,
0384                         VMCI_TRANSPORT_PACKET_TYPE_RST,
0385                         0, 0, NULL,
0386                         VMCI_INVALID_HANDLE);
0387 }
0388 
0389 static int vmci_transport_send_invalid_bh(struct sockaddr_vm *dst,
0390                       struct sockaddr_vm *src)
0391 {
0392     return vmci_transport_send_control_pkt_bh(
0393                     dst, src,
0394                     VMCI_TRANSPORT_PACKET_TYPE_INVALID,
0395                     0, 0, NULL, VMCI_INVALID_HANDLE);
0396 }
0397 
0398 int vmci_transport_send_wrote_bh(struct sockaddr_vm *dst,
0399                  struct sockaddr_vm *src)
0400 {
0401     return vmci_transport_send_control_pkt_bh(
0402                     dst, src,
0403                     VMCI_TRANSPORT_PACKET_TYPE_WROTE, 0,
0404                     0, NULL, VMCI_INVALID_HANDLE);
0405 }
0406 
0407 int vmci_transport_send_read_bh(struct sockaddr_vm *dst,
0408                 struct sockaddr_vm *src)
0409 {
0410     return vmci_transport_send_control_pkt_bh(
0411                     dst, src,
0412                     VMCI_TRANSPORT_PACKET_TYPE_READ, 0,
0413                     0, NULL, VMCI_INVALID_HANDLE);
0414 }
0415 
0416 int vmci_transport_send_wrote(struct sock *sk)
0417 {
0418     return vmci_transport_send_control_pkt(
0419                     sk, VMCI_TRANSPORT_PACKET_TYPE_WROTE, 0,
0420                     0, NULL, VSOCK_PROTO_INVALID,
0421                     VMCI_INVALID_HANDLE);
0422 }
0423 
0424 int vmci_transport_send_read(struct sock *sk)
0425 {
0426     return vmci_transport_send_control_pkt(
0427                     sk, VMCI_TRANSPORT_PACKET_TYPE_READ, 0,
0428                     0, NULL, VSOCK_PROTO_INVALID,
0429                     VMCI_INVALID_HANDLE);
0430 }
0431 
0432 int vmci_transport_send_waiting_write(struct sock *sk,
0433                       struct vmci_transport_waiting_info *wait)
0434 {
0435     return vmci_transport_send_control_pkt(
0436                 sk, VMCI_TRANSPORT_PACKET_TYPE_WAITING_WRITE,
0437                 0, 0, wait, VSOCK_PROTO_INVALID,
0438                 VMCI_INVALID_HANDLE);
0439 }
0440 
0441 int vmci_transport_send_waiting_read(struct sock *sk,
0442                      struct vmci_transport_waiting_info *wait)
0443 {
0444     return vmci_transport_send_control_pkt(
0445                 sk, VMCI_TRANSPORT_PACKET_TYPE_WAITING_READ,
0446                 0, 0, wait, VSOCK_PROTO_INVALID,
0447                 VMCI_INVALID_HANDLE);
0448 }
0449 
0450 static int vmci_transport_shutdown(struct vsock_sock *vsk, int mode)
0451 {
0452     return vmci_transport_send_control_pkt(
0453                     &vsk->sk,
0454                     VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN,
0455                     0, mode, NULL,
0456                     VSOCK_PROTO_INVALID,
0457                     VMCI_INVALID_HANDLE);
0458 }
0459 
0460 static int vmci_transport_send_conn_request(struct sock *sk, size_t size)
0461 {
0462     return vmci_transport_send_control_pkt(sk,
0463                     VMCI_TRANSPORT_PACKET_TYPE_REQUEST,
0464                     size, 0, NULL,
0465                     VSOCK_PROTO_INVALID,
0466                     VMCI_INVALID_HANDLE);
0467 }
0468 
0469 static int vmci_transport_send_conn_request2(struct sock *sk, size_t size,
0470                          u16 version)
0471 {
0472     return vmci_transport_send_control_pkt(
0473                     sk, VMCI_TRANSPORT_PACKET_TYPE_REQUEST2,
0474                     size, 0, NULL, version,
0475                     VMCI_INVALID_HANDLE);
0476 }
0477 
0478 static struct sock *vmci_transport_get_pending(
0479                     struct sock *listener,
0480                     struct vmci_transport_packet *pkt)
0481 {
0482     struct vsock_sock *vlistener;
0483     struct vsock_sock *vpending;
0484     struct sock *pending;
0485     struct sockaddr_vm src;
0486 
0487     vsock_addr_init(&src, pkt->dg.src.context, pkt->src_port);
0488 
0489     vlistener = vsock_sk(listener);
0490 
0491     list_for_each_entry(vpending, &vlistener->pending_links,
0492                 pending_links) {
0493         if (vsock_addr_equals_addr(&src, &vpending->remote_addr) &&
0494             pkt->dst_port == vpending->local_addr.svm_port) {
0495             pending = sk_vsock(vpending);
0496             sock_hold(pending);
0497             goto found;
0498         }
0499     }
0500 
0501     pending = NULL;
0502 found:
0503     return pending;
0504 
0505 }
0506 
0507 static void vmci_transport_release_pending(struct sock *pending)
0508 {
0509     sock_put(pending);
0510 }
0511 
0512 /* We allow two kinds of sockets to communicate with a restricted VM: 1)
0513  * trusted sockets 2) sockets from applications running as the same user as the
0514  * VM (this is only true for the host side and only when using hosted products)
0515  */
0516 
0517 static bool vmci_transport_is_trusted(struct vsock_sock *vsock, u32 peer_cid)
0518 {
0519     return vsock->trusted ||
0520            vmci_is_context_owner(peer_cid, vsock->owner->uid);
0521 }
0522 
0523 /* We allow sending datagrams to and receiving datagrams from a restricted VM
0524  * only if it is trusted as described in vmci_transport_is_trusted.
0525  */
0526 
0527 static bool vmci_transport_allow_dgram(struct vsock_sock *vsock, u32 peer_cid)
0528 {
0529     if (VMADDR_CID_HYPERVISOR == peer_cid)
0530         return true;
0531 
0532     if (vsock->cached_peer != peer_cid) {
0533         vsock->cached_peer = peer_cid;
0534         if (!vmci_transport_is_trusted(vsock, peer_cid) &&
0535             (vmci_context_get_priv_flags(peer_cid) &
0536              VMCI_PRIVILEGE_FLAG_RESTRICTED)) {
0537             vsock->cached_peer_allow_dgram = false;
0538         } else {
0539             vsock->cached_peer_allow_dgram = true;
0540         }
0541     }
0542 
0543     return vsock->cached_peer_allow_dgram;
0544 }
0545 
0546 static int
0547 vmci_transport_queue_pair_alloc(struct vmci_qp **qpair,
0548                 struct vmci_handle *handle,
0549                 u64 produce_size,
0550                 u64 consume_size,
0551                 u32 peer, u32 flags, bool trusted)
0552 {
0553     int err = 0;
0554 
0555     if (trusted) {
0556         /* Try to allocate our queue pair as trusted. This will only
0557          * work if vsock is running in the host.
0558          */
0559 
0560         err = vmci_qpair_alloc(qpair, handle, produce_size,
0561                        consume_size,
0562                        peer, flags,
0563                        VMCI_PRIVILEGE_FLAG_TRUSTED);
0564         if (err != VMCI_ERROR_NO_ACCESS)
0565             goto out;
0566 
0567     }
0568 
0569     err = vmci_qpair_alloc(qpair, handle, produce_size, consume_size,
0570                    peer, flags, VMCI_NO_PRIVILEGE_FLAGS);
0571 out:
0572     if (err < 0) {
0573         pr_err_once("Could not attach to queue pair with %d\n", err);
0574         err = vmci_transport_error_to_vsock_error(err);
0575     }
0576 
0577     return err;
0578 }
0579 
0580 static int
0581 vmci_transport_datagram_create_hnd(u32 resource_id,
0582                    u32 flags,
0583                    vmci_datagram_recv_cb recv_cb,
0584                    void *client_data,
0585                    struct vmci_handle *out_handle)
0586 {
0587     int err = 0;
0588 
0589     /* Try to allocate our datagram handler as trusted. This will only work
0590      * if vsock is running in the host.
0591      */
0592 
0593     err = vmci_datagram_create_handle_priv(resource_id, flags,
0594                            VMCI_PRIVILEGE_FLAG_TRUSTED,
0595                            recv_cb,
0596                            client_data, out_handle);
0597 
0598     if (err == VMCI_ERROR_NO_ACCESS)
0599         err = vmci_datagram_create_handle(resource_id, flags,
0600                           recv_cb, client_data,
0601                           out_handle);
0602 
0603     return err;
0604 }
0605 
0606 /* This is invoked as part of a tasklet that's scheduled when the VMCI
0607  * interrupt fires.  This is run in bottom-half context and if it ever needs to
0608  * sleep it should defer that work to a work queue.
0609  */
0610 
0611 static int vmci_transport_recv_dgram_cb(void *data, struct vmci_datagram *dg)
0612 {
0613     struct sock *sk;
0614     size_t size;
0615     struct sk_buff *skb;
0616     struct vsock_sock *vsk;
0617 
0618     sk = (struct sock *)data;
0619 
0620     /* This handler is privileged when this module is running on the host.
0621      * We will get datagrams from all endpoints (even VMs that are in a
0622      * restricted context). If we get one from a restricted context then
0623      * the destination socket must be trusted.
0624      *
0625      * NOTE: We access the socket struct without holding the lock here.
0626      * This is ok because the field we are interested is never modified
0627      * outside of the create and destruct socket functions.
0628      */
0629     vsk = vsock_sk(sk);
0630     if (!vmci_transport_allow_dgram(vsk, dg->src.context))
0631         return VMCI_ERROR_NO_ACCESS;
0632 
0633     size = VMCI_DG_SIZE(dg);
0634 
0635     /* Attach the packet to the socket's receive queue as an sk_buff. */
0636     skb = alloc_skb(size, GFP_ATOMIC);
0637     if (!skb)
0638         return VMCI_ERROR_NO_MEM;
0639 
0640     /* sk_receive_skb() will do a sock_put(), so hold here. */
0641     sock_hold(sk);
0642     skb_put(skb, size);
0643     memcpy(skb->data, dg, size);
0644     sk_receive_skb(sk, skb, 0);
0645 
0646     return VMCI_SUCCESS;
0647 }
0648 
0649 static bool vmci_transport_stream_allow(u32 cid, u32 port)
0650 {
0651     static const u32 non_socket_contexts[] = {
0652         VMADDR_CID_LOCAL,
0653     };
0654     int i;
0655 
0656     BUILD_BUG_ON(sizeof(cid) != sizeof(*non_socket_contexts));
0657 
0658     for (i = 0; i < ARRAY_SIZE(non_socket_contexts); i++) {
0659         if (cid == non_socket_contexts[i])
0660             return false;
0661     }
0662 
0663     return true;
0664 }
0665 
0666 /* This is invoked as part of a tasklet that's scheduled when the VMCI
0667  * interrupt fires.  This is run in bottom-half context but it defers most of
0668  * its work to the packet handling work queue.
0669  */
0670 
0671 static int vmci_transport_recv_stream_cb(void *data, struct vmci_datagram *dg)
0672 {
0673     struct sock *sk;
0674     struct sockaddr_vm dst;
0675     struct sockaddr_vm src;
0676     struct vmci_transport_packet *pkt;
0677     struct vsock_sock *vsk;
0678     bool bh_process_pkt;
0679     int err;
0680 
0681     sk = NULL;
0682     err = VMCI_SUCCESS;
0683     bh_process_pkt = false;
0684 
0685     /* Ignore incoming packets from contexts without sockets, or resources
0686      * that aren't vsock implementations.
0687      */
0688 
0689     if (!vmci_transport_stream_allow(dg->src.context, -1)
0690         || vmci_transport_peer_rid(dg->src.context) != dg->src.resource)
0691         return VMCI_ERROR_NO_ACCESS;
0692 
0693     if (VMCI_DG_SIZE(dg) < sizeof(*pkt))
0694         /* Drop datagrams that do not contain full VSock packets. */
0695         return VMCI_ERROR_INVALID_ARGS;
0696 
0697     pkt = (struct vmci_transport_packet *)dg;
0698 
0699     /* Find the socket that should handle this packet.  First we look for a
0700      * connected socket and if there is none we look for a socket bound to
0701      * the destintation address.
0702      */
0703     vsock_addr_init(&src, pkt->dg.src.context, pkt->src_port);
0704     vsock_addr_init(&dst, pkt->dg.dst.context, pkt->dst_port);
0705 
0706     sk = vsock_find_connected_socket(&src, &dst);
0707     if (!sk) {
0708         sk = vsock_find_bound_socket(&dst);
0709         if (!sk) {
0710             /* We could not find a socket for this specified
0711              * address.  If this packet is a RST, we just drop it.
0712              * If it is another packet, we send a RST.  Note that
0713              * we do not send a RST reply to RSTs so that we do not
0714              * continually send RSTs between two endpoints.
0715              *
0716              * Note that since this is a reply, dst is src and src
0717              * is dst.
0718              */
0719             if (vmci_transport_send_reset_bh(&dst, &src, pkt) < 0)
0720                 pr_err("unable to send reset\n");
0721 
0722             err = VMCI_ERROR_NOT_FOUND;
0723             goto out;
0724         }
0725     }
0726 
0727     /* If the received packet type is beyond all types known to this
0728      * implementation, reply with an invalid message.  Hopefully this will
0729      * help when implementing backwards compatibility in the future.
0730      */
0731     if (pkt->type >= VMCI_TRANSPORT_PACKET_TYPE_MAX) {
0732         vmci_transport_send_invalid_bh(&dst, &src);
0733         err = VMCI_ERROR_INVALID_ARGS;
0734         goto out;
0735     }
0736 
0737     /* This handler is privileged when this module is running on the host.
0738      * We will get datagram connect requests from all endpoints (even VMs
0739      * that are in a restricted context). If we get one from a restricted
0740      * context then the destination socket must be trusted.
0741      *
0742      * NOTE: We access the socket struct without holding the lock here.
0743      * This is ok because the field we are interested is never modified
0744      * outside of the create and destruct socket functions.
0745      */
0746     vsk = vsock_sk(sk);
0747     if (!vmci_transport_allow_dgram(vsk, pkt->dg.src.context)) {
0748         err = VMCI_ERROR_NO_ACCESS;
0749         goto out;
0750     }
0751 
0752     /* We do most everything in a work queue, but let's fast path the
0753      * notification of reads and writes to help data transfer performance.
0754      * We can only do this if there is no process context code executing
0755      * for this socket since that may change the state.
0756      */
0757     bh_lock_sock(sk);
0758 
0759     if (!sock_owned_by_user(sk)) {
0760         /* The local context ID may be out of date, update it. */
0761         vsk->local_addr.svm_cid = dst.svm_cid;
0762 
0763         if (sk->sk_state == TCP_ESTABLISHED)
0764             vmci_trans(vsk)->notify_ops->handle_notify_pkt(
0765                     sk, pkt, true, &dst, &src,
0766                     &bh_process_pkt);
0767     }
0768 
0769     bh_unlock_sock(sk);
0770 
0771     if (!bh_process_pkt) {
0772         struct vmci_transport_recv_pkt_info *recv_pkt_info;
0773 
0774         recv_pkt_info = kmalloc(sizeof(*recv_pkt_info), GFP_ATOMIC);
0775         if (!recv_pkt_info) {
0776             if (vmci_transport_send_reset_bh(&dst, &src, pkt) < 0)
0777                 pr_err("unable to send reset\n");
0778 
0779             err = VMCI_ERROR_NO_MEM;
0780             goto out;
0781         }
0782 
0783         recv_pkt_info->sk = sk;
0784         memcpy(&recv_pkt_info->pkt, pkt, sizeof(recv_pkt_info->pkt));
0785         INIT_WORK(&recv_pkt_info->work, vmci_transport_recv_pkt_work);
0786 
0787         schedule_work(&recv_pkt_info->work);
0788         /* Clear sk so that the reference count incremented by one of
0789          * the Find functions above is not decremented below.  We need
0790          * that reference count for the packet handler we've scheduled
0791          * to run.
0792          */
0793         sk = NULL;
0794     }
0795 
0796 out:
0797     if (sk)
0798         sock_put(sk);
0799 
0800     return err;
0801 }
0802 
0803 static void vmci_transport_handle_detach(struct sock *sk)
0804 {
0805     struct vsock_sock *vsk;
0806 
0807     vsk = vsock_sk(sk);
0808     if (!vmci_handle_is_invalid(vmci_trans(vsk)->qp_handle)) {
0809         sock_set_flag(sk, SOCK_DONE);
0810 
0811         /* On a detach the peer will not be sending or receiving
0812          * anymore.
0813          */
0814         vsk->peer_shutdown = SHUTDOWN_MASK;
0815 
0816         /* We should not be sending anymore since the peer won't be
0817          * there to receive, but we can still receive if there is data
0818          * left in our consume queue. If the local endpoint is a host,
0819          * we can't call vsock_stream_has_data, since that may block,
0820          * but a host endpoint can't read data once the VM has
0821          * detached, so there is no available data in that case.
0822          */
0823         if (vsk->local_addr.svm_cid == VMADDR_CID_HOST ||
0824             vsock_stream_has_data(vsk) <= 0) {
0825             if (sk->sk_state == TCP_SYN_SENT) {
0826                 /* The peer may detach from a queue pair while
0827                  * we are still in the connecting state, i.e.,
0828                  * if the peer VM is killed after attaching to
0829                  * a queue pair, but before we complete the
0830                  * handshake. In that case, we treat the detach
0831                  * event like a reset.
0832                  */
0833 
0834                 sk->sk_state = TCP_CLOSE;
0835                 sk->sk_err = ECONNRESET;
0836                 sk_error_report(sk);
0837                 return;
0838             }
0839             sk->sk_state = TCP_CLOSE;
0840         }
0841         sk->sk_state_change(sk);
0842     }
0843 }
0844 
0845 static void vmci_transport_peer_detach_cb(u32 sub_id,
0846                       const struct vmci_event_data *e_data,
0847                       void *client_data)
0848 {
0849     struct vmci_transport *trans = client_data;
0850     const struct vmci_event_payload_qp *e_payload;
0851 
0852     e_payload = vmci_event_data_const_payload(e_data);
0853 
0854     /* XXX This is lame, we should provide a way to lookup sockets by
0855      * qp_handle.
0856      */
0857     if (vmci_handle_is_invalid(e_payload->handle) ||
0858         !vmci_handle_is_equal(trans->qp_handle, e_payload->handle))
0859         return;
0860 
0861     /* We don't ask for delayed CBs when we subscribe to this event (we
0862      * pass 0 as flags to vmci_event_subscribe()).  VMCI makes no
0863      * guarantees in that case about what context we might be running in,
0864      * so it could be BH or process, blockable or non-blockable.  So we
0865      * need to account for all possible contexts here.
0866      */
0867     spin_lock_bh(&trans->lock);
0868     if (!trans->sk)
0869         goto out;
0870 
0871     /* Apart from here, trans->lock is only grabbed as part of sk destruct,
0872      * where trans->sk isn't locked.
0873      */
0874     bh_lock_sock(trans->sk);
0875 
0876     vmci_transport_handle_detach(trans->sk);
0877 
0878     bh_unlock_sock(trans->sk);
0879  out:
0880     spin_unlock_bh(&trans->lock);
0881 }
0882 
0883 static void vmci_transport_qp_resumed_cb(u32 sub_id,
0884                      const struct vmci_event_data *e_data,
0885                      void *client_data)
0886 {
0887     vsock_for_each_connected_socket(&vmci_transport,
0888                     vmci_transport_handle_detach);
0889 }
0890 
0891 static void vmci_transport_recv_pkt_work(struct work_struct *work)
0892 {
0893     struct vmci_transport_recv_pkt_info *recv_pkt_info;
0894     struct vmci_transport_packet *pkt;
0895     struct sock *sk;
0896 
0897     recv_pkt_info =
0898         container_of(work, struct vmci_transport_recv_pkt_info, work);
0899     sk = recv_pkt_info->sk;
0900     pkt = &recv_pkt_info->pkt;
0901 
0902     lock_sock(sk);
0903 
0904     /* The local context ID may be out of date. */
0905     vsock_sk(sk)->local_addr.svm_cid = pkt->dg.dst.context;
0906 
0907     switch (sk->sk_state) {
0908     case TCP_LISTEN:
0909         vmci_transport_recv_listen(sk, pkt);
0910         break;
0911     case TCP_SYN_SENT:
0912         /* Processing of pending connections for servers goes through
0913          * the listening socket, so see vmci_transport_recv_listen()
0914          * for that path.
0915          */
0916         vmci_transport_recv_connecting_client(sk, pkt);
0917         break;
0918     case TCP_ESTABLISHED:
0919         vmci_transport_recv_connected(sk, pkt);
0920         break;
0921     default:
0922         /* Because this function does not run in the same context as
0923          * vmci_transport_recv_stream_cb it is possible that the
0924          * socket has closed. We need to let the other side know or it
0925          * could be sitting in a connect and hang forever. Send a
0926          * reset to prevent that.
0927          */
0928         vmci_transport_send_reset(sk, pkt);
0929         break;
0930     }
0931 
0932     release_sock(sk);
0933     kfree(recv_pkt_info);
0934     /* Release reference obtained in the stream callback when we fetched
0935      * this socket out of the bound or connected list.
0936      */
0937     sock_put(sk);
0938 }
0939 
0940 static int vmci_transport_recv_listen(struct sock *sk,
0941                       struct vmci_transport_packet *pkt)
0942 {
0943     struct sock *pending;
0944     struct vsock_sock *vpending;
0945     int err;
0946     u64 qp_size;
0947     bool old_request = false;
0948     bool old_pkt_proto = false;
0949 
0950     /* Because we are in the listen state, we could be receiving a packet
0951      * for ourself or any previous connection requests that we received.
0952      * If it's the latter, we try to find a socket in our list of pending
0953      * connections and, if we do, call the appropriate handler for the
0954      * state that that socket is in.  Otherwise we try to service the
0955      * connection request.
0956      */
0957     pending = vmci_transport_get_pending(sk, pkt);
0958     if (pending) {
0959         lock_sock(pending);
0960 
0961         /* The local context ID may be out of date. */
0962         vsock_sk(pending)->local_addr.svm_cid = pkt->dg.dst.context;
0963 
0964         switch (pending->sk_state) {
0965         case TCP_SYN_SENT:
0966             err = vmci_transport_recv_connecting_server(sk,
0967                                     pending,
0968                                     pkt);
0969             break;
0970         default:
0971             vmci_transport_send_reset(pending, pkt);
0972             err = -EINVAL;
0973         }
0974 
0975         if (err < 0)
0976             vsock_remove_pending(sk, pending);
0977 
0978         release_sock(pending);
0979         vmci_transport_release_pending(pending);
0980 
0981         return err;
0982     }
0983 
0984     /* The listen state only accepts connection requests.  Reply with a
0985      * reset unless we received a reset.
0986      */
0987 
0988     if (!(pkt->type == VMCI_TRANSPORT_PACKET_TYPE_REQUEST ||
0989           pkt->type == VMCI_TRANSPORT_PACKET_TYPE_REQUEST2)) {
0990         vmci_transport_reply_reset(pkt);
0991         return -EINVAL;
0992     }
0993 
0994     if (pkt->u.size == 0) {
0995         vmci_transport_reply_reset(pkt);
0996         return -EINVAL;
0997     }
0998 
0999     /* If this socket can't accommodate this connection request, we send a
1000      * reset.  Otherwise we create and initialize a child socket and reply
1001      * with a connection negotiation.
1002      */
1003     if (sk->sk_ack_backlog >= sk->sk_max_ack_backlog) {
1004         vmci_transport_reply_reset(pkt);
1005         return -ECONNREFUSED;
1006     }
1007 
1008     pending = vsock_create_connected(sk);
1009     if (!pending) {
1010         vmci_transport_send_reset(sk, pkt);
1011         return -ENOMEM;
1012     }
1013 
1014     vpending = vsock_sk(pending);
1015 
1016     vsock_addr_init(&vpending->local_addr, pkt->dg.dst.context,
1017             pkt->dst_port);
1018     vsock_addr_init(&vpending->remote_addr, pkt->dg.src.context,
1019             pkt->src_port);
1020 
1021     err = vsock_assign_transport(vpending, vsock_sk(sk));
1022     /* Transport assigned (looking at remote_addr) must be the same
1023      * where we received the request.
1024      */
1025     if (err || !vmci_check_transport(vpending)) {
1026         vmci_transport_send_reset(sk, pkt);
1027         sock_put(pending);
1028         return err;
1029     }
1030 
1031     /* If the proposed size fits within our min/max, accept it. Otherwise
1032      * propose our own size.
1033      */
1034     if (pkt->u.size >= vpending->buffer_min_size &&
1035         pkt->u.size <= vpending->buffer_max_size) {
1036         qp_size = pkt->u.size;
1037     } else {
1038         qp_size = vpending->buffer_size;
1039     }
1040 
1041     /* Figure out if we are using old or new requests based on the
1042      * overrides pkt types sent by our peer.
1043      */
1044     if (vmci_transport_old_proto_override(&old_pkt_proto)) {
1045         old_request = old_pkt_proto;
1046     } else {
1047         if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_REQUEST)
1048             old_request = true;
1049         else if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_REQUEST2)
1050             old_request = false;
1051 
1052     }
1053 
1054     if (old_request) {
1055         /* Handle a REQUEST (or override) */
1056         u16 version = VSOCK_PROTO_INVALID;
1057         if (vmci_transport_proto_to_notify_struct(
1058             pending, &version, true))
1059             err = vmci_transport_send_negotiate(pending, qp_size);
1060         else
1061             err = -EINVAL;
1062 
1063     } else {
1064         /* Handle a REQUEST2 (or override) */
1065         int proto_int = pkt->proto;
1066         int pos;
1067         u16 active_proto_version = 0;
1068 
1069         /* The list of possible protocols is the intersection of all
1070          * protocols the client supports ... plus all the protocols we
1071          * support.
1072          */
1073         proto_int &= vmci_transport_new_proto_supported_versions();
1074 
1075         /* We choose the highest possible protocol version and use that
1076          * one.
1077          */
1078         pos = fls(proto_int);
1079         if (pos) {
1080             active_proto_version = (1 << (pos - 1));
1081             if (vmci_transport_proto_to_notify_struct(
1082                 pending, &active_proto_version, false))
1083                 err = vmci_transport_send_negotiate2(pending,
1084                             qp_size,
1085                             active_proto_version);
1086             else
1087                 err = -EINVAL;
1088 
1089         } else {
1090             err = -EINVAL;
1091         }
1092     }
1093 
1094     if (err < 0) {
1095         vmci_transport_send_reset(sk, pkt);
1096         sock_put(pending);
1097         err = vmci_transport_error_to_vsock_error(err);
1098         goto out;
1099     }
1100 
1101     vsock_add_pending(sk, pending);
1102     sk_acceptq_added(sk);
1103 
1104     pending->sk_state = TCP_SYN_SENT;
1105     vmci_trans(vpending)->produce_size =
1106         vmci_trans(vpending)->consume_size = qp_size;
1107     vpending->buffer_size = qp_size;
1108 
1109     vmci_trans(vpending)->notify_ops->process_request(pending);
1110 
1111     /* We might never receive another message for this socket and it's not
1112      * connected to any process, so we have to ensure it gets cleaned up
1113      * ourself.  Our delayed work function will take care of that.  Note
1114      * that we do not ever cancel this function since we have few
1115      * guarantees about its state when calling cancel_delayed_work().
1116      * Instead we hold a reference on the socket for that function and make
1117      * it capable of handling cases where it needs to do nothing but
1118      * release that reference.
1119      */
1120     vpending->listener = sk;
1121     sock_hold(sk);
1122     sock_hold(pending);
1123     schedule_delayed_work(&vpending->pending_work, HZ);
1124 
1125 out:
1126     return err;
1127 }
1128 
1129 static int
1130 vmci_transport_recv_connecting_server(struct sock *listener,
1131                       struct sock *pending,
1132                       struct vmci_transport_packet *pkt)
1133 {
1134     struct vsock_sock *vpending;
1135     struct vmci_handle handle;
1136     struct vmci_qp *qpair;
1137     bool is_local;
1138     u32 flags;
1139     u32 detach_sub_id;
1140     int err;
1141     int skerr;
1142 
1143     vpending = vsock_sk(pending);
1144     detach_sub_id = VMCI_INVALID_ID;
1145 
1146     switch (pkt->type) {
1147     case VMCI_TRANSPORT_PACKET_TYPE_OFFER:
1148         if (vmci_handle_is_invalid(pkt->u.handle)) {
1149             vmci_transport_send_reset(pending, pkt);
1150             skerr = EPROTO;
1151             err = -EINVAL;
1152             goto destroy;
1153         }
1154         break;
1155     default:
1156         /* Close and cleanup the connection. */
1157         vmci_transport_send_reset(pending, pkt);
1158         skerr = EPROTO;
1159         err = pkt->type == VMCI_TRANSPORT_PACKET_TYPE_RST ? 0 : -EINVAL;
1160         goto destroy;
1161     }
1162 
1163     /* In order to complete the connection we need to attach to the offered
1164      * queue pair and send an attach notification.  We also subscribe to the
1165      * detach event so we know when our peer goes away, and we do that
1166      * before attaching so we don't miss an event.  If all this succeeds,
1167      * we update our state and wakeup anything waiting in accept() for a
1168      * connection.
1169      */
1170 
1171     /* We don't care about attach since we ensure the other side has
1172      * attached by specifying the ATTACH_ONLY flag below.
1173      */
1174     err = vmci_event_subscribe(VMCI_EVENT_QP_PEER_DETACH,
1175                    vmci_transport_peer_detach_cb,
1176                    vmci_trans(vpending), &detach_sub_id);
1177     if (err < VMCI_SUCCESS) {
1178         vmci_transport_send_reset(pending, pkt);
1179         err = vmci_transport_error_to_vsock_error(err);
1180         skerr = -err;
1181         goto destroy;
1182     }
1183 
1184     vmci_trans(vpending)->detach_sub_id = detach_sub_id;
1185 
1186     /* Now attach to the queue pair the client created. */
1187     handle = pkt->u.handle;
1188 
1189     /* vpending->local_addr always has a context id so we do not need to
1190      * worry about VMADDR_CID_ANY in this case.
1191      */
1192     is_local =
1193         vpending->remote_addr.svm_cid == vpending->local_addr.svm_cid;
1194     flags = VMCI_QPFLAG_ATTACH_ONLY;
1195     flags |= is_local ? VMCI_QPFLAG_LOCAL : 0;
1196 
1197     err = vmci_transport_queue_pair_alloc(
1198                     &qpair,
1199                     &handle,
1200                     vmci_trans(vpending)->produce_size,
1201                     vmci_trans(vpending)->consume_size,
1202                     pkt->dg.src.context,
1203                     flags,
1204                     vmci_transport_is_trusted(
1205                         vpending,
1206                         vpending->remote_addr.svm_cid));
1207     if (err < 0) {
1208         vmci_transport_send_reset(pending, pkt);
1209         skerr = -err;
1210         goto destroy;
1211     }
1212 
1213     vmci_trans(vpending)->qp_handle = handle;
1214     vmci_trans(vpending)->qpair = qpair;
1215 
1216     /* When we send the attach message, we must be ready to handle incoming
1217      * control messages on the newly connected socket. So we move the
1218      * pending socket to the connected state before sending the attach
1219      * message. Otherwise, an incoming packet triggered by the attach being
1220      * received by the peer may be processed concurrently with what happens
1221      * below after sending the attach message, and that incoming packet
1222      * will find the listening socket instead of the (currently) pending
1223      * socket. Note that enqueueing the socket increments the reference
1224      * count, so even if a reset comes before the connection is accepted,
1225      * the socket will be valid until it is removed from the queue.
1226      *
1227      * If we fail sending the attach below, we remove the socket from the
1228      * connected list and move the socket to TCP_CLOSE before
1229      * releasing the lock, so a pending slow path processing of an incoming
1230      * packet will not see the socket in the connected state in that case.
1231      */
1232     pending->sk_state = TCP_ESTABLISHED;
1233 
1234     vsock_insert_connected(vpending);
1235 
1236     /* Notify our peer of our attach. */
1237     err = vmci_transport_send_attach(pending, handle);
1238     if (err < 0) {
1239         vsock_remove_connected(vpending);
1240         pr_err("Could not send attach\n");
1241         vmci_transport_send_reset(pending, pkt);
1242         err = vmci_transport_error_to_vsock_error(err);
1243         skerr = -err;
1244         goto destroy;
1245     }
1246 
1247     /* We have a connection. Move the now connected socket from the
1248      * listener's pending list to the accept queue so callers of accept()
1249      * can find it.
1250      */
1251     vsock_remove_pending(listener, pending);
1252     vsock_enqueue_accept(listener, pending);
1253 
1254     /* Callers of accept() will be waiting on the listening socket, not
1255      * the pending socket.
1256      */
1257     listener->sk_data_ready(listener);
1258 
1259     return 0;
1260 
1261 destroy:
1262     pending->sk_err = skerr;
1263     pending->sk_state = TCP_CLOSE;
1264     /* As long as we drop our reference, all necessary cleanup will handle
1265      * when the cleanup function drops its reference and our destruct
1266      * implementation is called.  Note that since the listen handler will
1267      * remove pending from the pending list upon our failure, the cleanup
1268      * function won't drop the additional reference, which is why we do it
1269      * here.
1270      */
1271     sock_put(pending);
1272 
1273     return err;
1274 }
1275 
1276 static int
1277 vmci_transport_recv_connecting_client(struct sock *sk,
1278                       struct vmci_transport_packet *pkt)
1279 {
1280     struct vsock_sock *vsk;
1281     int err;
1282     int skerr;
1283 
1284     vsk = vsock_sk(sk);
1285 
1286     switch (pkt->type) {
1287     case VMCI_TRANSPORT_PACKET_TYPE_ATTACH:
1288         if (vmci_handle_is_invalid(pkt->u.handle) ||
1289             !vmci_handle_is_equal(pkt->u.handle,
1290                       vmci_trans(vsk)->qp_handle)) {
1291             skerr = EPROTO;
1292             err = -EINVAL;
1293             goto destroy;
1294         }
1295 
1296         /* Signify the socket is connected and wakeup the waiter in
1297          * connect(). Also place the socket in the connected table for
1298          * accounting (it can already be found since it's in the bound
1299          * table).
1300          */
1301         sk->sk_state = TCP_ESTABLISHED;
1302         sk->sk_socket->state = SS_CONNECTED;
1303         vsock_insert_connected(vsk);
1304         sk->sk_state_change(sk);
1305 
1306         break;
1307     case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE:
1308     case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2:
1309         if (pkt->u.size == 0
1310             || pkt->dg.src.context != vsk->remote_addr.svm_cid
1311             || pkt->src_port != vsk->remote_addr.svm_port
1312             || !vmci_handle_is_invalid(vmci_trans(vsk)->qp_handle)
1313             || vmci_trans(vsk)->qpair
1314             || vmci_trans(vsk)->produce_size != 0
1315             || vmci_trans(vsk)->consume_size != 0
1316             || vmci_trans(vsk)->detach_sub_id != VMCI_INVALID_ID) {
1317             skerr = EPROTO;
1318             err = -EINVAL;
1319 
1320             goto destroy;
1321         }
1322 
1323         err = vmci_transport_recv_connecting_client_negotiate(sk, pkt);
1324         if (err) {
1325             skerr = -err;
1326             goto destroy;
1327         }
1328 
1329         break;
1330     case VMCI_TRANSPORT_PACKET_TYPE_INVALID:
1331         err = vmci_transport_recv_connecting_client_invalid(sk, pkt);
1332         if (err) {
1333             skerr = -err;
1334             goto destroy;
1335         }
1336 
1337         break;
1338     case VMCI_TRANSPORT_PACKET_TYPE_RST:
1339         /* Older versions of the linux code (WS 6.5 / ESX 4.0) used to
1340          * continue processing here after they sent an INVALID packet.
1341          * This meant that we got a RST after the INVALID. We ignore a
1342          * RST after an INVALID. The common code doesn't send the RST
1343          * ... so we can hang if an old version of the common code
1344          * fails between getting a REQUEST and sending an OFFER back.
1345          * Not much we can do about it... except hope that it doesn't
1346          * happen.
1347          */
1348         if (vsk->ignore_connecting_rst) {
1349             vsk->ignore_connecting_rst = false;
1350         } else {
1351             skerr = ECONNRESET;
1352             err = 0;
1353             goto destroy;
1354         }
1355 
1356         break;
1357     default:
1358         /* Close and cleanup the connection. */
1359         skerr = EPROTO;
1360         err = -EINVAL;
1361         goto destroy;
1362     }
1363 
1364     return 0;
1365 
1366 destroy:
1367     vmci_transport_send_reset(sk, pkt);
1368 
1369     sk->sk_state = TCP_CLOSE;
1370     sk->sk_err = skerr;
1371     sk_error_report(sk);
1372     return err;
1373 }
1374 
1375 static int vmci_transport_recv_connecting_client_negotiate(
1376                     struct sock *sk,
1377                     struct vmci_transport_packet *pkt)
1378 {
1379     int err;
1380     struct vsock_sock *vsk;
1381     struct vmci_handle handle;
1382     struct vmci_qp *qpair;
1383     u32 detach_sub_id;
1384     bool is_local;
1385     u32 flags;
1386     bool old_proto = true;
1387     bool old_pkt_proto;
1388     u16 version;
1389 
1390     vsk = vsock_sk(sk);
1391     handle = VMCI_INVALID_HANDLE;
1392     detach_sub_id = VMCI_INVALID_ID;
1393 
1394     /* If we have gotten here then we should be past the point where old
1395      * linux vsock could have sent the bogus rst.
1396      */
1397     vsk->sent_request = false;
1398     vsk->ignore_connecting_rst = false;
1399 
1400     /* Verify that we're OK with the proposed queue pair size */
1401     if (pkt->u.size < vsk->buffer_min_size ||
1402         pkt->u.size > vsk->buffer_max_size) {
1403         err = -EINVAL;
1404         goto destroy;
1405     }
1406 
1407     /* At this point we know the CID the peer is using to talk to us. */
1408 
1409     if (vsk->local_addr.svm_cid == VMADDR_CID_ANY)
1410         vsk->local_addr.svm_cid = pkt->dg.dst.context;
1411 
1412     /* Setup the notify ops to be the highest supported version that both
1413      * the server and the client support.
1414      */
1415 
1416     if (vmci_transport_old_proto_override(&old_pkt_proto)) {
1417         old_proto = old_pkt_proto;
1418     } else {
1419         if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE)
1420             old_proto = true;
1421         else if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2)
1422             old_proto = false;
1423 
1424     }
1425 
1426     if (old_proto)
1427         version = VSOCK_PROTO_INVALID;
1428     else
1429         version = pkt->proto;
1430 
1431     if (!vmci_transport_proto_to_notify_struct(sk, &version, old_proto)) {
1432         err = -EINVAL;
1433         goto destroy;
1434     }
1435 
1436     /* Subscribe to detach events first.
1437      *
1438      * XXX We attach once for each queue pair created for now so it is easy
1439      * to find the socket (it's provided), but later we should only
1440      * subscribe once and add a way to lookup sockets by queue pair handle.
1441      */
1442     err = vmci_event_subscribe(VMCI_EVENT_QP_PEER_DETACH,
1443                    vmci_transport_peer_detach_cb,
1444                    vmci_trans(vsk), &detach_sub_id);
1445     if (err < VMCI_SUCCESS) {
1446         err = vmci_transport_error_to_vsock_error(err);
1447         goto destroy;
1448     }
1449 
1450     /* Make VMCI select the handle for us. */
1451     handle = VMCI_INVALID_HANDLE;
1452     is_local = vsk->remote_addr.svm_cid == vsk->local_addr.svm_cid;
1453     flags = is_local ? VMCI_QPFLAG_LOCAL : 0;
1454 
1455     err = vmci_transport_queue_pair_alloc(&qpair,
1456                           &handle,
1457                           pkt->u.size,
1458                           pkt->u.size,
1459                           vsk->remote_addr.svm_cid,
1460                           flags,
1461                           vmci_transport_is_trusted(
1462                           vsk,
1463                           vsk->
1464                           remote_addr.svm_cid));
1465     if (err < 0)
1466         goto destroy;
1467 
1468     err = vmci_transport_send_qp_offer(sk, handle);
1469     if (err < 0) {
1470         err = vmci_transport_error_to_vsock_error(err);
1471         goto destroy;
1472     }
1473 
1474     vmci_trans(vsk)->qp_handle = handle;
1475     vmci_trans(vsk)->qpair = qpair;
1476 
1477     vmci_trans(vsk)->produce_size = vmci_trans(vsk)->consume_size =
1478         pkt->u.size;
1479 
1480     vmci_trans(vsk)->detach_sub_id = detach_sub_id;
1481 
1482     vmci_trans(vsk)->notify_ops->process_negotiate(sk);
1483 
1484     return 0;
1485 
1486 destroy:
1487     if (detach_sub_id != VMCI_INVALID_ID)
1488         vmci_event_unsubscribe(detach_sub_id);
1489 
1490     if (!vmci_handle_is_invalid(handle))
1491         vmci_qpair_detach(&qpair);
1492 
1493     return err;
1494 }
1495 
1496 static int
1497 vmci_transport_recv_connecting_client_invalid(struct sock *sk,
1498                           struct vmci_transport_packet *pkt)
1499 {
1500     int err = 0;
1501     struct vsock_sock *vsk = vsock_sk(sk);
1502 
1503     if (vsk->sent_request) {
1504         vsk->sent_request = false;
1505         vsk->ignore_connecting_rst = true;
1506 
1507         err = vmci_transport_send_conn_request(sk, vsk->buffer_size);
1508         if (err < 0)
1509             err = vmci_transport_error_to_vsock_error(err);
1510         else
1511             err = 0;
1512 
1513     }
1514 
1515     return err;
1516 }
1517 
1518 static int vmci_transport_recv_connected(struct sock *sk,
1519                      struct vmci_transport_packet *pkt)
1520 {
1521     struct vsock_sock *vsk;
1522     bool pkt_processed = false;
1523 
1524     /* In cases where we are closing the connection, it's sufficient to
1525      * mark the state change (and maybe error) and wake up any waiting
1526      * threads. Since this is a connected socket, it's owned by a user
1527      * process and will be cleaned up when the failure is passed back on
1528      * the current or next system call.  Our system call implementations
1529      * must therefore check for error and state changes on entry and when
1530      * being awoken.
1531      */
1532     switch (pkt->type) {
1533     case VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN:
1534         if (pkt->u.mode) {
1535             vsk = vsock_sk(sk);
1536 
1537             vsk->peer_shutdown |= pkt->u.mode;
1538             sk->sk_state_change(sk);
1539         }
1540         break;
1541 
1542     case VMCI_TRANSPORT_PACKET_TYPE_RST:
1543         vsk = vsock_sk(sk);
1544         /* It is possible that we sent our peer a message (e.g a
1545          * WAITING_READ) right before we got notified that the peer had
1546          * detached. If that happens then we can get a RST pkt back
1547          * from our peer even though there is data available for us to
1548          * read. In that case, don't shutdown the socket completely but
1549          * instead allow the local client to finish reading data off
1550          * the queuepair. Always treat a RST pkt in connected mode like
1551          * a clean shutdown.
1552          */
1553         sock_set_flag(sk, SOCK_DONE);
1554         vsk->peer_shutdown = SHUTDOWN_MASK;
1555         if (vsock_stream_has_data(vsk) <= 0)
1556             sk->sk_state = TCP_CLOSING;
1557 
1558         sk->sk_state_change(sk);
1559         break;
1560 
1561     default:
1562         vsk = vsock_sk(sk);
1563         vmci_trans(vsk)->notify_ops->handle_notify_pkt(
1564                 sk, pkt, false, NULL, NULL,
1565                 &pkt_processed);
1566         if (!pkt_processed)
1567             return -EINVAL;
1568 
1569         break;
1570     }
1571 
1572     return 0;
1573 }
1574 
1575 static int vmci_transport_socket_init(struct vsock_sock *vsk,
1576                       struct vsock_sock *psk)
1577 {
1578     vsk->trans = kmalloc(sizeof(struct vmci_transport), GFP_KERNEL);
1579     if (!vsk->trans)
1580         return -ENOMEM;
1581 
1582     vmci_trans(vsk)->dg_handle = VMCI_INVALID_HANDLE;
1583     vmci_trans(vsk)->qp_handle = VMCI_INVALID_HANDLE;
1584     vmci_trans(vsk)->qpair = NULL;
1585     vmci_trans(vsk)->produce_size = vmci_trans(vsk)->consume_size = 0;
1586     vmci_trans(vsk)->detach_sub_id = VMCI_INVALID_ID;
1587     vmci_trans(vsk)->notify_ops = NULL;
1588     INIT_LIST_HEAD(&vmci_trans(vsk)->elem);
1589     vmci_trans(vsk)->sk = &vsk->sk;
1590     spin_lock_init(&vmci_trans(vsk)->lock);
1591 
1592     return 0;
1593 }
1594 
1595 static void vmci_transport_free_resources(struct list_head *transport_list)
1596 {
1597     while (!list_empty(transport_list)) {
1598         struct vmci_transport *transport =
1599             list_first_entry(transport_list, struct vmci_transport,
1600                      elem);
1601         list_del(&transport->elem);
1602 
1603         if (transport->detach_sub_id != VMCI_INVALID_ID) {
1604             vmci_event_unsubscribe(transport->detach_sub_id);
1605             transport->detach_sub_id = VMCI_INVALID_ID;
1606         }
1607 
1608         if (!vmci_handle_is_invalid(transport->qp_handle)) {
1609             vmci_qpair_detach(&transport->qpair);
1610             transport->qp_handle = VMCI_INVALID_HANDLE;
1611             transport->produce_size = 0;
1612             transport->consume_size = 0;
1613         }
1614 
1615         kfree(transport);
1616     }
1617 }
1618 
1619 static void vmci_transport_cleanup(struct work_struct *work)
1620 {
1621     LIST_HEAD(pending);
1622 
1623     spin_lock_bh(&vmci_transport_cleanup_lock);
1624     list_replace_init(&vmci_transport_cleanup_list, &pending);
1625     spin_unlock_bh(&vmci_transport_cleanup_lock);
1626     vmci_transport_free_resources(&pending);
1627 }
1628 
1629 static void vmci_transport_destruct(struct vsock_sock *vsk)
1630 {
1631     /* transport can be NULL if we hit a failure at init() time */
1632     if (!vmci_trans(vsk))
1633         return;
1634 
1635     /* Ensure that the detach callback doesn't use the sk/vsk
1636      * we are about to destruct.
1637      */
1638     spin_lock_bh(&vmci_trans(vsk)->lock);
1639     vmci_trans(vsk)->sk = NULL;
1640     spin_unlock_bh(&vmci_trans(vsk)->lock);
1641 
1642     if (vmci_trans(vsk)->notify_ops)
1643         vmci_trans(vsk)->notify_ops->socket_destruct(vsk);
1644 
1645     spin_lock_bh(&vmci_transport_cleanup_lock);
1646     list_add(&vmci_trans(vsk)->elem, &vmci_transport_cleanup_list);
1647     spin_unlock_bh(&vmci_transport_cleanup_lock);
1648     schedule_work(&vmci_transport_cleanup_work);
1649 
1650     vsk->trans = NULL;
1651 }
1652 
1653 static void vmci_transport_release(struct vsock_sock *vsk)
1654 {
1655     vsock_remove_sock(vsk);
1656 
1657     if (!vmci_handle_is_invalid(vmci_trans(vsk)->dg_handle)) {
1658         vmci_datagram_destroy_handle(vmci_trans(vsk)->dg_handle);
1659         vmci_trans(vsk)->dg_handle = VMCI_INVALID_HANDLE;
1660     }
1661 }
1662 
1663 static int vmci_transport_dgram_bind(struct vsock_sock *vsk,
1664                      struct sockaddr_vm *addr)
1665 {
1666     u32 port;
1667     u32 flags;
1668     int err;
1669 
1670     /* VMCI will select a resource ID for us if we provide
1671      * VMCI_INVALID_ID.
1672      */
1673     port = addr->svm_port == VMADDR_PORT_ANY ?
1674             VMCI_INVALID_ID : addr->svm_port;
1675 
1676     if (port <= LAST_RESERVED_PORT && !capable(CAP_NET_BIND_SERVICE))
1677         return -EACCES;
1678 
1679     flags = addr->svm_cid == VMADDR_CID_ANY ?
1680                 VMCI_FLAG_ANYCID_DG_HND : 0;
1681 
1682     err = vmci_transport_datagram_create_hnd(port, flags,
1683                          vmci_transport_recv_dgram_cb,
1684                          &vsk->sk,
1685                          &vmci_trans(vsk)->dg_handle);
1686     if (err < VMCI_SUCCESS)
1687         return vmci_transport_error_to_vsock_error(err);
1688     vsock_addr_init(&vsk->local_addr, addr->svm_cid,
1689             vmci_trans(vsk)->dg_handle.resource);
1690 
1691     return 0;
1692 }
1693 
1694 static int vmci_transport_dgram_enqueue(
1695     struct vsock_sock *vsk,
1696     struct sockaddr_vm *remote_addr,
1697     struct msghdr *msg,
1698     size_t len)
1699 {
1700     int err;
1701     struct vmci_datagram *dg;
1702 
1703     if (len > VMCI_MAX_DG_PAYLOAD_SIZE)
1704         return -EMSGSIZE;
1705 
1706     if (!vmci_transport_allow_dgram(vsk, remote_addr->svm_cid))
1707         return -EPERM;
1708 
1709     /* Allocate a buffer for the user's message and our packet header. */
1710     dg = kmalloc(len + sizeof(*dg), GFP_KERNEL);
1711     if (!dg)
1712         return -ENOMEM;
1713 
1714     memcpy_from_msg(VMCI_DG_PAYLOAD(dg), msg, len);
1715 
1716     dg->dst = vmci_make_handle(remote_addr->svm_cid,
1717                    remote_addr->svm_port);
1718     dg->src = vmci_make_handle(vsk->local_addr.svm_cid,
1719                    vsk->local_addr.svm_port);
1720     dg->payload_size = len;
1721 
1722     err = vmci_datagram_send(dg);
1723     kfree(dg);
1724     if (err < 0)
1725         return vmci_transport_error_to_vsock_error(err);
1726 
1727     return err - sizeof(*dg);
1728 }
1729 
1730 static int vmci_transport_dgram_dequeue(struct vsock_sock *vsk,
1731                     struct msghdr *msg, size_t len,
1732                     int flags)
1733 {
1734     int err;
1735     struct vmci_datagram *dg;
1736     size_t payload_len;
1737     struct sk_buff *skb;
1738 
1739     if (flags & MSG_OOB || flags & MSG_ERRQUEUE)
1740         return -EOPNOTSUPP;
1741 
1742     /* Retrieve the head sk_buff from the socket's receive queue. */
1743     err = 0;
1744     skb = skb_recv_datagram(&vsk->sk, flags, &err);
1745     if (!skb)
1746         return err;
1747 
1748     dg = (struct vmci_datagram *)skb->data;
1749     if (!dg)
1750         /* err is 0, meaning we read zero bytes. */
1751         goto out;
1752 
1753     payload_len = dg->payload_size;
1754     /* Ensure the sk_buff matches the payload size claimed in the packet. */
1755     if (payload_len != skb->len - sizeof(*dg)) {
1756         err = -EINVAL;
1757         goto out;
1758     }
1759 
1760     if (payload_len > len) {
1761         payload_len = len;
1762         msg->msg_flags |= MSG_TRUNC;
1763     }
1764 
1765     /* Place the datagram payload in the user's iovec. */
1766     err = skb_copy_datagram_msg(skb, sizeof(*dg), msg, payload_len);
1767     if (err)
1768         goto out;
1769 
1770     if (msg->msg_name) {
1771         /* Provide the address of the sender. */
1772         DECLARE_SOCKADDR(struct sockaddr_vm *, vm_addr, msg->msg_name);
1773         vsock_addr_init(vm_addr, dg->src.context, dg->src.resource);
1774         msg->msg_namelen = sizeof(*vm_addr);
1775     }
1776     err = payload_len;
1777 
1778 out:
1779     skb_free_datagram(&vsk->sk, skb);
1780     return err;
1781 }
1782 
1783 static bool vmci_transport_dgram_allow(u32 cid, u32 port)
1784 {
1785     if (cid == VMADDR_CID_HYPERVISOR) {
1786         /* Registrations of PBRPC Servers do not modify VMX/Hypervisor
1787          * state and are allowed.
1788          */
1789         return port == VMCI_UNITY_PBRPC_REGISTER;
1790     }
1791 
1792     return true;
1793 }
1794 
1795 static int vmci_transport_connect(struct vsock_sock *vsk)
1796 {
1797     int err;
1798     bool old_pkt_proto = false;
1799     struct sock *sk = &vsk->sk;
1800 
1801     if (vmci_transport_old_proto_override(&old_pkt_proto) &&
1802         old_pkt_proto) {
1803         err = vmci_transport_send_conn_request(sk, vsk->buffer_size);
1804         if (err < 0) {
1805             sk->sk_state = TCP_CLOSE;
1806             return err;
1807         }
1808     } else {
1809         int supported_proto_versions =
1810             vmci_transport_new_proto_supported_versions();
1811         err = vmci_transport_send_conn_request2(sk, vsk->buffer_size,
1812                 supported_proto_versions);
1813         if (err < 0) {
1814             sk->sk_state = TCP_CLOSE;
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 int vmci_transport_notify_poll_in(
1865     struct vsock_sock *vsk,
1866     size_t target,
1867     bool *data_ready_now)
1868 {
1869     return vmci_trans(vsk)->notify_ops->poll_in(
1870             &vsk->sk, target, data_ready_now);
1871 }
1872 
1873 static int vmci_transport_notify_poll_out(
1874     struct vsock_sock *vsk,
1875     size_t target,
1876     bool *space_available_now)
1877 {
1878     return vmci_trans(vsk)->notify_ops->poll_out(
1879             &vsk->sk, target, space_available_now);
1880 }
1881 
1882 static int vmci_transport_notify_recv_init(
1883     struct vsock_sock *vsk,
1884     size_t target,
1885     struct vsock_transport_recv_notify_data *data)
1886 {
1887     return vmci_trans(vsk)->notify_ops->recv_init(
1888             &vsk->sk, target,
1889             (struct vmci_transport_recv_notify_data *)data);
1890 }
1891 
1892 static int vmci_transport_notify_recv_pre_block(
1893     struct vsock_sock *vsk,
1894     size_t target,
1895     struct vsock_transport_recv_notify_data *data)
1896 {
1897     return vmci_trans(vsk)->notify_ops->recv_pre_block(
1898             &vsk->sk, target,
1899             (struct vmci_transport_recv_notify_data *)data);
1900 }
1901 
1902 static int vmci_transport_notify_recv_pre_dequeue(
1903     struct vsock_sock *vsk,
1904     size_t target,
1905     struct vsock_transport_recv_notify_data *data)
1906 {
1907     return vmci_trans(vsk)->notify_ops->recv_pre_dequeue(
1908             &vsk->sk, target,
1909             (struct vmci_transport_recv_notify_data *)data);
1910 }
1911 
1912 static int vmci_transport_notify_recv_post_dequeue(
1913     struct vsock_sock *vsk,
1914     size_t target,
1915     ssize_t copied,
1916     bool data_read,
1917     struct vsock_transport_recv_notify_data *data)
1918 {
1919     return vmci_trans(vsk)->notify_ops->recv_post_dequeue(
1920             &vsk->sk, target, copied, data_read,
1921             (struct vmci_transport_recv_notify_data *)data);
1922 }
1923 
1924 static int vmci_transport_notify_send_init(
1925     struct vsock_sock *vsk,
1926     struct vsock_transport_send_notify_data *data)
1927 {
1928     return vmci_trans(vsk)->notify_ops->send_init(
1929             &vsk->sk,
1930             (struct vmci_transport_send_notify_data *)data);
1931 }
1932 
1933 static int vmci_transport_notify_send_pre_block(
1934     struct vsock_sock *vsk,
1935     struct vsock_transport_send_notify_data *data)
1936 {
1937     return vmci_trans(vsk)->notify_ops->send_pre_block(
1938             &vsk->sk,
1939             (struct vmci_transport_send_notify_data *)data);
1940 }
1941 
1942 static int vmci_transport_notify_send_pre_enqueue(
1943     struct vsock_sock *vsk,
1944     struct vsock_transport_send_notify_data *data)
1945 {
1946     return vmci_trans(vsk)->notify_ops->send_pre_enqueue(
1947             &vsk->sk,
1948             (struct vmci_transport_send_notify_data *)data);
1949 }
1950 
1951 static int vmci_transport_notify_send_post_enqueue(
1952     struct vsock_sock *vsk,
1953     ssize_t written,
1954     struct vsock_transport_send_notify_data *data)
1955 {
1956     return vmci_trans(vsk)->notify_ops->send_post_enqueue(
1957             &vsk->sk, written,
1958             (struct vmci_transport_send_notify_data *)data);
1959 }
1960 
1961 static bool vmci_transport_old_proto_override(bool *old_pkt_proto)
1962 {
1963     if (PROTOCOL_OVERRIDE != -1) {
1964         if (PROTOCOL_OVERRIDE == 0)
1965             *old_pkt_proto = true;
1966         else
1967             *old_pkt_proto = false;
1968 
1969         pr_info("Proto override in use\n");
1970         return true;
1971     }
1972 
1973     return false;
1974 }
1975 
1976 static bool vmci_transport_proto_to_notify_struct(struct sock *sk,
1977                           u16 *proto,
1978                           bool old_pkt_proto)
1979 {
1980     struct vsock_sock *vsk = vsock_sk(sk);
1981 
1982     if (old_pkt_proto) {
1983         if (*proto != VSOCK_PROTO_INVALID) {
1984             pr_err("Can't set both an old and new protocol\n");
1985             return false;
1986         }
1987         vmci_trans(vsk)->notify_ops = &vmci_transport_notify_pkt_ops;
1988         goto exit;
1989     }
1990 
1991     switch (*proto) {
1992     case VSOCK_PROTO_PKT_ON_NOTIFY:
1993         vmci_trans(vsk)->notify_ops =
1994             &vmci_transport_notify_pkt_q_state_ops;
1995         break;
1996     default:
1997         pr_err("Unknown notify protocol version\n");
1998         return false;
1999     }
2000 
2001 exit:
2002     vmci_trans(vsk)->notify_ops->socket_init(sk);
2003     return true;
2004 }
2005 
2006 static u16 vmci_transport_new_proto_supported_versions(void)
2007 {
2008     if (PROTOCOL_OVERRIDE != -1)
2009         return PROTOCOL_OVERRIDE;
2010 
2011     return VSOCK_PROTO_ALL_SUPPORTED;
2012 }
2013 
2014 static u32 vmci_transport_get_local_cid(void)
2015 {
2016     return vmci_get_context_id();
2017 }
2018 
2019 static struct vsock_transport vmci_transport = {
2020     .module = THIS_MODULE,
2021     .init = vmci_transport_socket_init,
2022     .destruct = vmci_transport_destruct,
2023     .release = vmci_transport_release,
2024     .connect = vmci_transport_connect,
2025     .dgram_bind = vmci_transport_dgram_bind,
2026     .dgram_dequeue = vmci_transport_dgram_dequeue,
2027     .dgram_enqueue = vmci_transport_dgram_enqueue,
2028     .dgram_allow = vmci_transport_dgram_allow,
2029     .stream_dequeue = vmci_transport_stream_dequeue,
2030     .stream_enqueue = vmci_transport_stream_enqueue,
2031     .stream_has_data = vmci_transport_stream_has_data,
2032     .stream_has_space = vmci_transport_stream_has_space,
2033     .stream_rcvhiwat = vmci_transport_stream_rcvhiwat,
2034     .stream_is_active = vmci_transport_stream_is_active,
2035     .stream_allow = vmci_transport_stream_allow,
2036     .notify_poll_in = vmci_transport_notify_poll_in,
2037     .notify_poll_out = vmci_transport_notify_poll_out,
2038     .notify_recv_init = vmci_transport_notify_recv_init,
2039     .notify_recv_pre_block = vmci_transport_notify_recv_pre_block,
2040     .notify_recv_pre_dequeue = vmci_transport_notify_recv_pre_dequeue,
2041     .notify_recv_post_dequeue = vmci_transport_notify_recv_post_dequeue,
2042     .notify_send_init = vmci_transport_notify_send_init,
2043     .notify_send_pre_block = vmci_transport_notify_send_pre_block,
2044     .notify_send_pre_enqueue = vmci_transport_notify_send_pre_enqueue,
2045     .notify_send_post_enqueue = vmci_transport_notify_send_post_enqueue,
2046     .shutdown = vmci_transport_shutdown,
2047     .get_local_cid = vmci_transport_get_local_cid,
2048 };
2049 
2050 static bool vmci_check_transport(struct vsock_sock *vsk)
2051 {
2052     return vsk->transport == &vmci_transport;
2053 }
2054 
2055 static void vmci_vsock_transport_cb(bool is_host)
2056 {
2057     int features;
2058 
2059     if (is_host)
2060         features = VSOCK_TRANSPORT_F_H2G;
2061     else
2062         features = VSOCK_TRANSPORT_F_G2H;
2063 
2064     vsock_core_register(&vmci_transport, features);
2065 }
2066 
2067 static int __init vmci_transport_init(void)
2068 {
2069     int err;
2070 
2071     /* Create the datagram handle that we will use to send and receive all
2072      * VSocket control messages for this context.
2073      */
2074     err = vmci_transport_datagram_create_hnd(VMCI_TRANSPORT_PACKET_RID,
2075                          VMCI_FLAG_ANYCID_DG_HND,
2076                          vmci_transport_recv_stream_cb,
2077                          NULL,
2078                          &vmci_transport_stream_handle);
2079     if (err < VMCI_SUCCESS) {
2080         pr_err("Unable to create datagram handle. (%d)\n", err);
2081         return vmci_transport_error_to_vsock_error(err);
2082     }
2083     err = vmci_event_subscribe(VMCI_EVENT_QP_RESUMED,
2084                    vmci_transport_qp_resumed_cb,
2085                    NULL, &vmci_transport_qp_resumed_sub_id);
2086     if (err < VMCI_SUCCESS) {
2087         pr_err("Unable to subscribe to resumed event. (%d)\n", err);
2088         err = vmci_transport_error_to_vsock_error(err);
2089         vmci_transport_qp_resumed_sub_id = VMCI_INVALID_ID;
2090         goto err_destroy_stream_handle;
2091     }
2092 
2093     /* Register only with dgram feature, other features (H2G, G2H) will be
2094      * registered when the first host or guest becomes active.
2095      */
2096     err = vsock_core_register(&vmci_transport, VSOCK_TRANSPORT_F_DGRAM);
2097     if (err < 0)
2098         goto err_unsubscribe;
2099 
2100     err = vmci_register_vsock_callback(vmci_vsock_transport_cb);
2101     if (err < 0)
2102         goto err_unregister;
2103 
2104     return 0;
2105 
2106 err_unregister:
2107     vsock_core_unregister(&vmci_transport);
2108 err_unsubscribe:
2109     vmci_event_unsubscribe(vmci_transport_qp_resumed_sub_id);
2110 err_destroy_stream_handle:
2111     vmci_datagram_destroy_handle(vmci_transport_stream_handle);
2112     return err;
2113 }
2114 module_init(vmci_transport_init);
2115 
2116 static void __exit vmci_transport_exit(void)
2117 {
2118     cancel_work_sync(&vmci_transport_cleanup_work);
2119     vmci_transport_free_resources(&vmci_transport_cleanup_list);
2120 
2121     if (!vmci_handle_is_invalid(vmci_transport_stream_handle)) {
2122         if (vmci_datagram_destroy_handle(
2123             vmci_transport_stream_handle) != VMCI_SUCCESS)
2124             pr_err("Couldn't destroy datagram handle\n");
2125         vmci_transport_stream_handle = VMCI_INVALID_HANDLE;
2126     }
2127 
2128     if (vmci_transport_qp_resumed_sub_id != VMCI_INVALID_ID) {
2129         vmci_event_unsubscribe(vmci_transport_qp_resumed_sub_id);
2130         vmci_transport_qp_resumed_sub_id = VMCI_INVALID_ID;
2131     }
2132 
2133     vmci_register_vsock_callback(NULL);
2134     vsock_core_unregister(&vmci_transport);
2135 }
2136 module_exit(vmci_transport_exit);
2137 
2138 MODULE_AUTHOR("VMware, Inc.");
2139 MODULE_DESCRIPTION("VMCI transport for Virtual Sockets");
2140 MODULE_VERSION("1.0.5.0-k");
2141 MODULE_LICENSE("GPL v2");
2142 MODULE_ALIAS("vmware_vsock");
2143 MODULE_ALIAS_NETPROTO(PF_VSOCK);