~n-muench/ubuntu/oneiric/open-vm-tools/open-vm-tools.fix-836277

« back to all changes in this revision

Viewing changes to modules/linux/vsock/linux/af_vsock.c

  • Committer: Bazaar Package Importer
  • Author(s): Devid Antonio Filoni
  • Date: 2008-08-15 21:21:40 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080815212140-05fhxj8wroosysmj
Tags: 2008.08.08-109361-1ubuntu1
* Merge from Debian unstable (LP: #258393), remaining Ubuntu change:
  - add ubuntu_toolchain_FTBFS.dpatch patch, fix FTBFS
* Update ubuntu_toolchain_FTBFS.dpatch patch for the new version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*********************************************************
 
2
 * Copyright (C) 2007 VMware, Inc. All rights reserved.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify it
 
5
 * under the terms of the GNU General Public License as published by the
 
6
 * Free Software Foundation version 2 and no later version.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful, but
 
9
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
10
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 
11
 * for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License along
 
14
 * with this program; if not, write to the Free Software Foundation, Inc.,
 
15
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 
16
 *
 
17
 *********************************************************/
 
18
 
 
19
/*
 
20
 * af_vsock.c --
 
21
 *
 
22
 *      Linux socket module for the VMCI Sockets protocol family.
 
23
 */
 
24
 
 
25
 
 
26
/*
 
27
 * Implementation notes:
 
28
 *
 
29
 * - There are two kinds of sockets: those created by user action (such as
 
30
 *   calling socket(2)) and those created by incoming connection request
 
31
 *   packets.
 
32
 *
 
33
 * - There are two "global" tables, one for bound sockets (sockets that have
 
34
 *   specified an address that they are responsible for) and one for connected
 
35
 *   sockets (sockets that have established a connection with another socket).
 
36
 *   These tables are "global" in that all sockets on the system are placed
 
37
 *   within them.
 
38
 *   - Note, though, that the bound table contains an extra entry for a list of
 
39
 *     unbound sockets and SOCK_DGRAM sockets will always remain in that list.
 
40
 *     The bound table is used solely for lookup of sockets when packets are
 
41
 *     received and that's not necessary for SOCK_DGRAM sockets since we create
 
42
 *     a datagram handle for each and need not perform a lookup.  Keeping
 
43
 *     SOCK_DGRAM sockets out of the bound hash buckets will reduce the chance
 
44
 *     of collisions when looking for SOCK_STREAM sockets and prevents us from
 
45
 *     having to check the socket type in the hash table lookups.
 
46
 *
 
47
 * - Sockets created by user action will either be "client" sockets that
 
48
 *   initiate a connection or "server" sockets that listen for connections; we
 
49
 *   do not support simultaneous connects (two "client" sockets connecting).
 
50
 *
 
51
 * - "Server" sockets are referred to as listener sockets throughout this
 
52
 *   implementation because they are in the SS_LISTEN state.  When a connection
 
53
 *   request is received (the second kind of socket mentioned above), we create
 
54
 *   a new socket and refer to it as a pending socket.  These pending sockets
 
55
 *   are placed on the pending connection list of the listener socket.  When
 
56
 *   future packets are received for the address the listener socket is bound
 
57
 *   to, we check if the source of the packet is from one that has an existing
 
58
 *   pending connection.  If it does, we process the packet for the pending
 
59
 *   socket.  When that socket reaches the connected state, it is removed from
 
60
 *   the listener socket's pending list and enqueued in the listener socket's
 
61
 *   accept queue.  Callers of accept(2) will accept connected sockets from the
 
62
 *   listener socket's accept queue.  If the socket cannot be accepted for some
 
63
 *   reason then it is marked rejected.  Once the connection is accepted, it is
 
64
 *   owned by the user process and the responsibility for cleanup falls with
 
65
 *   that user process.
 
66
 *
 
67
 * - It is possible that these pending sockets will never reach the connected
 
68
 *   state; in fact, we may never receive another packet after the connection
 
69
 *   request.  Because of this, we must schedule a cleanup function to run in
 
70
 *   the future, after some amount of time passes where a connection should
 
71
 *   have been established.  This function ensures that the socket is off all
 
72
 *   lists so it cannot be retrieved, then drops all references to the socket
 
73
 *   so it is cleaned up (sock_put() -> sk_free() -> our sk_destruct
 
74
 *   implementation).  Note this function will also cleanup rejected sockets,
 
75
 *   those that reach the connected state but leave it before they have been
 
76
 *   accepted.
 
77
 *
 
78
 * - Sockets created by user action will be cleaned up when the user
 
79
 *   process calls close(2), causing our release implementation to be called.
 
80
 *   Our release implementation will perform some cleanup then drop the
 
81
 *   last reference so our sk_destruct implementation is invoked.  Our
 
82
 *   sk_destruct implementation will perform additional cleanup that's common
 
83
 *   for both types of sockets.
 
84
 *
 
85
 * - A socket's reference count is what ensures that the structure won't be
 
86
 *   freed.  Each entry in a list (such as the "global" bound and connected
 
87
 *   tables and the listener socket's pending list and connected queue) ensures
 
88
 *   a reference.  When we defer work until process context and pass a socket
 
89
 *   as our argument, we must ensure the reference count is increased to ensure
 
90
 *   the socket isn't freed before the function is run; the deferred function
 
91
 *   will then drop the reference.
 
92
 *
 
93
 */
 
94
 
 
95
#include "driver-config.h"
 
96
 
 
97
#include <linux/kmod.h>
 
98
#include <linux/socket.h>
 
99
#include <linux/net.h>
 
100
#include <linux/skbuff.h>
 
101
#include <linux/miscdevice.h>
 
102
#include <linux/poll.h>
 
103
#include <linux/smp.h>
 
104
#include <linux/smp_lock.h>
 
105
#include <asm/io.h>
 
106
#if defined(__x86_64__) && LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 12)
 
107
#   if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
 
108
#      include <asm/ioctl32.h>
 
109
#   else
 
110
#      include <linux/ioctl32.h>
 
111
#   endif
 
112
/* Use weak: not all kernels export sys_ioctl for use by modules */
 
113
#   if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 66)
 
114
asmlinkage __attribute__((weak)) long
 
115
sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg);
 
116
#   else
 
117
asmlinkage __attribute__((weak)) int
 
118
sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg);
 
119
#   endif
 
120
#endif
 
121
 
 
122
#include "compat_module.h"
 
123
#include "compat_kernel.h"
 
124
#include "compat_init.h"
 
125
#include "compat_sock.h"
 
126
#include "compat_wait.h"
 
127
#include "compat_version.h"
 
128
#include "compat_workqueue.h"
 
129
#include "compat_list.h"
 
130
#if defined(HAVE_COMPAT_IOCTL) || defined(HAVE_UNLOCKED_IOCTL)
 
131
# include "compat_semaphore.h"
 
132
#endif
 
133
 
 
134
#include "vmware.h"
 
135
 
 
136
#include "vsockCommon.h"
 
137
#include "vsockPacket.h"
 
138
#include "vsockVmci.h"
 
139
 
 
140
#include "vmci_defs.h"
 
141
#include "vmci_call_defs.h"
 
142
#include "vmci_iocontrols.h"
 
143
#ifdef VMX86_TOOLS
 
144
# include "vmciGuestKernelAPI.h"
 
145
#else
 
146
# include "vmciDatagram.h"
 
147
#endif
 
148
 
 
149
#include "af_vsock.h"
 
150
#include "util.h"
 
151
#include "vsock_version.h"
 
152
#include "driverLog.h"
 
153
 
 
154
 
 
155
#define VSOCK_INVALID_FAMILY        NPROTO
 
156
#define VSOCK_AF_IS_REGISTERED(val) ((val) >= 0 && (val) < NPROTO)
 
157
 
 
158
/* Some kernel versions don't define __user. Define it ourself if so. */
 
159
#ifndef __user
 
160
#define __user
 
161
#endif
 
162
 
 
163
 
 
164
/*
 
165
 * Prototypes
 
166
 */
 
167
 
 
168
int VSockVmci_GetAFValue(void);
 
169
 
 
170
/* Internal functions. */
 
171
static int VSockVmciRecvDgramCB(void *data, VMCIDatagram *dg);
 
172
#ifdef VMX86_TOOLS
 
173
static int VSockVmciRecvStreamCB(void *data, VMCIDatagram *dg);
 
174
static void VSockVmciPeerAttachCB(VMCIId subId,
 
175
                                  VMCI_EventData *ed, void *clientData);
 
176
static void VSockVmciPeerDetachCB(VMCIId subId,
 
177
                                  VMCI_EventData *ed, void *clientData);
 
178
static int VSockVmciSendControlPktBH(struct sockaddr_vm *src,
 
179
                                     struct sockaddr_vm *dst,
 
180
                                     VSockPacketType type,
 
181
                                     uint64 size,
 
182
                                     uint64 mode,
 
183
                                     VSockWaitingInfo *wait,
 
184
                                     VMCIHandle handle);
 
185
static int VSockVmciSendControlPkt(struct sock *sk, VSockPacketType type,
 
186
                                   uint64 size, uint64 mode,
 
187
                                   VSockWaitingInfo *wait, VMCIHandle handle);
 
188
static void VSockVmciRecvPktWork(compat_work_arg work);
 
189
static int VSockVmciRecvListen(struct sock *sk, VSockPacket *pkt);
 
190
static int VSockVmciRecvConnectingServer(struct sock *sk,
 
191
                                         struct sock *pending, VSockPacket *pkt);
 
192
static int VSockVmciRecvConnectingClient(struct sock *sk, VSockPacket *pkt);
 
193
static int VSockVmciRecvConnectingClientNegotiate(struct sock *sk,
 
194
                                                  VSockPacket *pkt);
 
195
static int VSockVmciRecvConnected(struct sock *sk, VSockPacket *pkt);
 
196
#endif
 
197
static int __VSockVmciBind(struct sock *sk, struct sockaddr_vm *addr);
 
198
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 14)
 
199
static struct sock *__VSockVmciCreate(struct socket *sock, unsigned int priority);
 
200
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
 
201
static struct sock *__VSockVmciCreate(struct socket *sock, gfp_t priority);
 
202
#else
 
203
static struct sock *__VSockVmciCreate(struct net *net,
 
204
                                      struct socket *sock, gfp_t priority);
 
205
#endif
 
206
static int VSockVmciRegisterAddressFamily(void);
 
207
static void VSockVmciUnregisterAddressFamily(void);
 
208
 
 
209
 
 
210
/* Socket operations. */
 
211
static void VSockVmciSkDestruct(struct sock *sk);
 
212
static int VSockVmciQueueRcvSkb(struct sock *sk, struct sk_buff *skb);
 
213
static int VSockVmciRelease(struct socket *sock);
 
214
static int VSockVmciBind(struct socket *sock,
 
215
                         struct sockaddr *addr, int addrLen);
 
216
static int VSockVmciDgramConnect(struct socket *sock,
 
217
                                 struct sockaddr *addr, int addrLen, int flags);
 
218
#ifdef VMX86_TOOLS
 
219
static int VSockVmciStreamConnect(struct socket *sock,
 
220
                                  struct sockaddr *addr, int addrLen, int flags);
 
221
static int VSockVmciAccept(struct socket *sock, struct socket *newsock, int flags);
 
222
#endif
 
223
static int VSockVmciGetname(struct socket *sock,
 
224
                            struct sockaddr *addr, int *addrLen, int peer);
 
225
static unsigned int VSockVmciPoll(struct file *file,
 
226
                                  struct socket *sock, poll_table *wait);
 
227
#ifdef VMX86_TOOLS
 
228
static int VSockVmciListen(struct socket *sock, int backlog);
 
229
#endif
 
230
static int VSockVmciShutdown(struct socket *sock, int mode);
 
231
 
 
232
#ifdef VMX86_TOOLS
 
233
static int VSockVmciStreamSetsockopt(struct socket *sock, int level, int optname,
 
234
                                     char __user *optval, int optlen);
 
235
static int VSockVmciStreamGetsockopt(struct socket *sock, int level, int optname,
 
236
                                     char __user *optval, int __user * optlen);
 
237
#endif
 
238
 
 
239
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 43)
 
240
static int VSockVmciDgramSendmsg(struct socket *sock, struct msghdr *msg,
 
241
                                 int len, struct scm_cookie *scm);
 
242
static int VSockVmciDgramRecvmsg(struct socket *sock, struct msghdr *msg,
 
243
                                 int len, int flags, struct scm_cookie *scm);
 
244
# ifdef VMX86_TOOLS
 
245
static int VSockVmciStreamSendmsg(struct socket *sock, struct msghdr *msg,
 
246
                                  int len, struct scm_cookie *scm);
 
247
static int VSockVmciStreamRecvmsg(struct socket *sock, struct msghdr *msg,
 
248
                                  int len, int flags, struct scm_cookie *scm);
 
249
# endif
 
250
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 65)
 
251
static int VSockVmciDgramSendmsg(struct kiocb *kiocb, struct socket *sock,
 
252
                                 struct msghdr *msg, int len,
 
253
                                 struct scm_cookie *scm);
 
254
static int VSockVmciDgramRecvmsg(struct kiocb *kiocb, struct socket *sock,
 
255
                                 struct msghdr *msg, int len,
 
256
                                 int flags, struct scm_cookie *scm);
 
257
# ifdef VMX86_TOOLS
 
258
static int VSockVmciStreamSendmsg(struct kiocb *kiocb, struct socket *sock,
 
259
                                  struct msghdr *msg, int len,
 
260
                                  struct scm_cookie *scm);
 
261
static int VSockVmciStreamRecvmsg(struct kiocb *kiocb, struct socket *sock,
 
262
                                  struct msghdr *msg, int len,
 
263
                                  int flags, struct scm_cookie *scm);
 
264
# endif
 
265
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 2)
 
266
static int VSockVmciDgramSendmsg(struct kiocb *kiocb,
 
267
                                 struct socket *sock, struct msghdr *msg, int len);
 
268
static int VSockVmciDgramRecvmsg(struct kiocb *kiocb, struct socket *sock,
 
269
                                 struct msghdr *msg, int len, int flags);
 
270
# ifdef VMX86_TOOLS
 
271
static int VSockVmciStreamSendmsg(struct kiocb *kiocb,
 
272
                                  struct socket *sock, struct msghdr *msg, int len);
 
273
static int VSockVmciStreamRecvmsg(struct kiocb *kiocb, struct socket *sock,
 
274
                                  struct msghdr *msg, int len, int flags);
 
275
# endif
 
276
#else
 
277
static int VSockVmciDgramSendmsg(struct kiocb *kiocb,
 
278
                                 struct socket *sock, struct msghdr *msg, size_t len);
 
279
static int VSockVmciDgramRecvmsg(struct kiocb *kiocb, struct socket *sock,
 
280
                                 struct msghdr *msg, size_t len, int flags);
 
281
# ifdef VMX86_TOOLS
 
282
static int VSockVmciStreamSendmsg(struct kiocb *kiocb,
 
283
                                 struct socket *sock, struct msghdr *msg, size_t len);
 
284
static int VSockVmciStreamRecvmsg(struct kiocb *kiocb, struct socket *sock,
 
285
                                 struct msghdr *msg, size_t len, int flags);
 
286
# endif
 
287
#endif
 
288
 
 
289
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
 
290
static int VSockVmciCreate(struct socket *sock, int protocol);
 
291
#else
 
292
static int VSockVmciCreate(struct net *net, struct socket *sock, int protocol);
 
293
#endif
 
294
 
 
295
/*
 
296
 * Device operations.
 
297
 */
 
298
int VSockVmciDevOpen(struct inode *inode, struct file *file);
 
299
int VSockVmciDevRelease(struct inode *inode, struct file *file);
 
300
static int VSockVmciDevIoctl(struct inode *inode, struct file *filp,
 
301
                             u_int iocmd, unsigned long ioarg);
 
302
#if defined(HAVE_COMPAT_IOCTL) || defined(HAVE_UNLOCKED_IOCTL)
 
303
static long VSockVmciDevUnlockedIoctl(struct file *filp,
 
304
                                      u_int iocmd, unsigned long ioarg);
 
305
#endif
 
306
 
 
307
/*
 
308
 * Variables.
 
309
 */
 
310
 
 
311
/* Protocol family.  We only use this for builds against 2.6.9 and later. */
 
312
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 9)
 
313
static struct proto vsockVmciProto = {
 
314
   .name     = "AF_VMCI",
 
315
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 10)
 
316
   /* Added in 2.6.10. */
 
317
   .owner    = THIS_MODULE,
 
318
#endif
 
319
   /*
 
320
    * Before 2.6.9, each address family created their own slab (by calling
 
321
    * kmem_cache_create() directly).  From 2.6.9 until 2.6.11, these address
 
322
    * families instead called sk_alloc_slab() and the allocated slab was
 
323
    * assigned to the slab variable in the proto struct and was created of size
 
324
    * slab_obj_size.  As of 2.6.12 and later, this slab allocation was moved
 
325
    * into proto_register() and only done if you specified a non-zero value for
 
326
    * the second argument (alloc_slab); the size of the slab element was
 
327
    * changed to obj_size.
 
328
    */
 
329
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 9)
 
330
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 12)
 
331
   .slab_obj_size = sizeof (VSockVmciSock),
 
332
#else
 
333
   .obj_size = sizeof (VSockVmciSock),
 
334
#endif
 
335
};
 
336
#endif
 
337
 
 
338
static struct net_proto_family vsockVmciFamilyOps = {
 
339
   .family = VSOCK_INVALID_FAMILY,
 
340
   .create = VSockVmciCreate,
 
341
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 69)
 
342
   .owner  = THIS_MODULE,
 
343
#endif
 
344
};
 
345
 
 
346
/* Socket operations, split for DGRAM and STREAM sockets. */
 
347
static struct proto_ops vsockVmciDgramOps = {
 
348
   .family     = VSOCK_INVALID_FAMILY,
 
349
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 69)
 
350
   .owner      = THIS_MODULE,
 
351
#endif
 
352
   .release    = VSockVmciRelease,
 
353
   .bind       = VSockVmciBind,
 
354
   .connect    = VSockVmciDgramConnect,
 
355
   .socketpair = sock_no_socketpair,
 
356
   .accept     = sock_no_accept,
 
357
   .getname    = VSockVmciGetname,
 
358
   .poll       = VSockVmciPoll,
 
359
   .ioctl      = sock_no_ioctl,
 
360
   .listen     = sock_no_listen,
 
361
   .shutdown   = VSockVmciShutdown,
 
362
   .setsockopt = sock_no_setsockopt,
 
363
   .getsockopt = sock_no_getsockopt,
 
364
   .sendmsg    = VSockVmciDgramSendmsg,
 
365
   .recvmsg    = VSockVmciDgramRecvmsg,
 
366
   .mmap       = sock_no_mmap,
 
367
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 4)
 
368
   .sendpage   = sock_no_sendpage,
 
369
#endif
 
370
};
 
371
 
 
372
#ifdef VMX86_TOOLS
 
373
static struct proto_ops vsockVmciStreamOps = {
 
374
   .family     = VSOCK_INVALID_FAMILY,
 
375
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 69)
 
376
   .owner      = THIS_MODULE,
 
377
#endif
 
378
   .release    = VSockVmciRelease,
 
379
   .bind       = VSockVmciBind,
 
380
   .connect    = VSockVmciStreamConnect,
 
381
   .socketpair = sock_no_socketpair,
 
382
   .accept     = VSockVmciAccept,
 
383
   .getname    = VSockVmciGetname,
 
384
   .poll       = VSockVmciPoll,
 
385
   .ioctl      = sock_no_ioctl,
 
386
   .listen     = VSockVmciListen,
 
387
   .shutdown   = VSockVmciShutdown,
 
388
   .setsockopt = VSockVmciStreamSetsockopt,
 
389
   .getsockopt = VSockVmciStreamGetsockopt,
 
390
   .sendmsg    = VSockVmciStreamSendmsg,
 
391
   .recvmsg    = VSockVmciStreamRecvmsg,
 
392
   .mmap       = sock_no_mmap,
 
393
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 4)
 
394
   .sendpage   = sock_no_sendpage,
 
395
#endif
 
396
};
 
397
#endif
 
398
 
 
399
static struct file_operations vsockVmciDeviceOps = {
 
400
   .ioctl = VSockVmciDevIoctl,
 
401
#ifdef HAVE_UNLOCKED_IOCTL
 
402
   .unlocked_ioctl = VSockVmciDevUnlockedIoctl,
 
403
#endif
 
404
#ifdef HAVE_COMPAT_IOCTL
 
405
   .compat_ioctl = VSockVmciDevUnlockedIoctl,
 
406
#endif
 
407
   .open = VSockVmciDevOpen,
 
408
   .release = VSockVmciDevRelease,
 
409
};
 
410
 
 
411
static struct miscdevice vsockVmciDevice = {
 
412
   .name = "vsock",
 
413
   .minor = MISC_DYNAMIC_MINOR,
 
414
   .fops = &vsockVmciDeviceOps,
 
415
};
 
416
 
 
417
typedef struct VSockRecvPktInfo {
 
418
   compat_work work;
 
419
   struct sock *sk;
 
420
   VSockPacket pkt;
 
421
} VSockRecvPktInfo;
 
422
 
 
423
static spinlock_t registrationLock = SPIN_LOCK_UNLOCKED;
 
424
static int devOpenCount = 0;
 
425
static int vsockVmciSocketCount = 0;
 
426
#ifdef VMX86_TOOLS
 
427
static VMCIHandle vmciStreamHandle = { VMCI_INVALID_ID, VMCI_INVALID_ID };
 
428
static Bool vmciDevicePresent = FALSE;
 
429
static VMCIId qpResumedSubId = VMCI_INVALID_ID;
 
430
#endif
 
431
 
 
432
/* Comment this out to compare with old protocol. */
 
433
#define VSOCK_OPTIMIZATION_WAITING_NOTIFY 1
 
434
 
 
435
/* Comment this out to turn off datagram counting. */
 
436
//#define VSOCK_CONTROL_PACKET_COUNT 1
 
437
#ifdef VSOCK_CONTROL_PACKET_COUNT
 
438
uint64 controlPacketCount[VSOCK_PACKET_TYPE_MAX];
 
439
#endif
 
440
 
 
441
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 9)
 
442
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 5)
 
443
kmem_cache_t *vsockCachep;
 
444
#endif
 
445
#endif
 
446
 
 
447
#define VSOCK_MAX_DGRAM_RESENDS       10
 
448
 
 
449
/*
 
450
 * 64k is hopefully a reasonable default, but we should do some real
 
451
 * benchmarks. There are also some issues with resource limits on ESX.
 
452
 */
 
453
#define VSOCK_DEFAULT_QP_SIZE_MIN   128
 
454
#define VSOCK_DEFAULT_QP_SIZE       65536
 
455
#define VSOCK_DEFAULT_QP_SIZE_MAX   262144
 
456
 
 
457
#define VSOCK_SEND_RESET_BH(_dst, _src, _pkt)                           \
 
458
   ((_pkt)->type == VSOCK_PACKET_TYPE_RST) ?                            \
 
459
      0 :                                                               \
 
460
      VSockVmciSendControlPktBH(_dst, _src, VSOCK_PACKET_TYPE_RST, 0,   \
 
461
                                0, NULL, VMCI_INVALID_HANDLE)
 
462
#define VSOCK_SEND_INVALID_BH(_dst, _src)                               \
 
463
   VSockVmciSendControlPktBH(_dst, _src, VSOCK_PACKET_TYPE_INVALID, 0,  \
 
464
                             0, NULL, VMCI_INVALID_HANDLE)
 
465
#define VSOCK_SEND_WROTE_BH(_dst, _src)                                 \
 
466
   VSockVmciSendControlPktBH(_dst, _src, VSOCK_PACKET_TYPE_WROTE, 0,    \
 
467
                             0, NULL, VMCI_INVALID_HANDLE)
 
468
#define VSOCK_SEND_READ_BH(_dst, _src)                                  \
 
469
   VSockVmciSendControlPktBH(_dst, _src, VSOCK_PACKET_TYPE_READ, 0,     \
 
470
                             0, NULL, VMCI_INVALID_HANDLE)
 
471
#define VSOCK_SEND_RESET(_sk, _pkt)                                     \
 
472
   ((_pkt)->type == VSOCK_PACKET_TYPE_RST) ?                            \
 
473
      0 :                                                               \
 
474
      VSockVmciSendControlPkt(_sk, VSOCK_PACKET_TYPE_RST,               \
 
475
                              0, 0, NULL, VMCI_INVALID_HANDLE)
 
476
#define VSOCK_SEND_NEGOTIATE(_sk, _size)                                \
 
477
   VSockVmciSendControlPkt(_sk, VSOCK_PACKET_TYPE_NEGOTIATE,            \
 
478
                           _size, 0, NULL, VMCI_INVALID_HANDLE)
 
479
#define VSOCK_SEND_QP_OFFER(_sk, _handle)                               \
 
480
   VSockVmciSendControlPkt(_sk, VSOCK_PACKET_TYPE_OFFER,                \
 
481
                           0, 0, NULL, _handle)
 
482
#define VSOCK_SEND_CONN_REQUEST(_sk, _size)                             \
 
483
   VSockVmciSendControlPkt(_sk, VSOCK_PACKET_TYPE_REQUEST,              \
 
484
                           _size, 0, NULL, VMCI_INVALID_HANDLE)
 
485
#define VSOCK_SEND_ATTACH(_sk, _handle)                                 \
 
486
   VSockVmciSendControlPkt(_sk, VSOCK_PACKET_TYPE_ATTACH,               \
 
487
                           0, 0, NULL, _handle)
 
488
#define VSOCK_SEND_WROTE(_sk)                                           \
 
489
   VSockVmciSendControlPkt(_sk, VSOCK_PACKET_TYPE_WROTE,                \
 
490
                           0, 0, NULL, VMCI_INVALID_HANDLE)
 
491
#define VSOCK_SEND_READ(_sk)                                            \
 
492
   VSockVmciSendControlPkt(_sk, VSOCK_PACKET_TYPE_READ,                 \
 
493
                           0, 0, NULL, VMCI_INVALID_HANDLE)
 
494
#define VSOCK_SEND_SHUTDOWN(_sk, _mode)                                 \
 
495
   VSockVmciSendControlPkt(_sk, VSOCK_PACKET_TYPE_SHUTDOWN,             \
 
496
                           0, _mode, NULL, VMCI_INVALID_HANDLE)
 
497
#define VSOCK_SEND_WAITING_WRITE(_sk, _waitInfo)                        \
 
498
   VSockVmciSendControlPkt(_sk, VSOCK_PACKET_TYPE_WAITING_WRITE,        \
 
499
                           0, 0, _waitInfo, VMCI_INVALID_HANDLE)
 
500
#define VSOCK_SEND_WAITING_READ(_sk, _waitInfo)                         \
 
501
   VSockVmciSendControlPkt(_sk, VSOCK_PACKET_TYPE_WAITING_READ,         \
 
502
                           0, 0, _waitInfo, VMCI_INVALID_HANDLE)
 
503
 
 
504
 
 
505
#ifdef VMX86_LOG
 
506
# define LOG_PACKET(_pkt)  VSockVmciLogPkt(__FUNCTION__, __LINE__, _pkt)
 
507
#else
 
508
# define LOG_PACKET(_pkt)
 
509
#endif
 
510
 
 
511
 
 
512
/*
 
513
 *----------------------------------------------------------------------------
 
514
 *
 
515
 * VSockVmci_GetAFValue --
 
516
 *
 
517
 *      Returns the address family value being used.
 
518
 *
 
519
 * Results:
 
520
 *      The address family on success, a negative error on failure.
 
521
 *
 
522
 * Side effects:
 
523
 *      None.
 
524
 *
 
525
 *----------------------------------------------------------------------------
 
526
 */
 
527
 
 
528
int
 
529
VSockVmci_GetAFValue(void)
 
530
{
 
531
   int afvalue;
 
532
 
 
533
   spin_lock(&registrationLock);
 
534
 
 
535
   afvalue = vsockVmciFamilyOps.family;
 
536
   if (!VSOCK_AF_IS_REGISTERED(afvalue)) {
 
537
      afvalue = VSockVmciRegisterAddressFamily();
 
538
   }
 
539
 
 
540
   spin_unlock(&registrationLock);
 
541
   return afvalue;
 
542
}
 
543
 
 
544
 
 
545
/*
 
546
 *----------------------------------------------------------------------------
 
547
 *
 
548
 * VSockVmciTestUnregister --
 
549
 *
 
550
 *      Tests if it's necessary to unregister the socket family, and does so.
 
551
 *
 
552
 *      Note that this assumes the registration lock is held.
 
553
 *
 
554
 * Results:
 
555
 *      None.
 
556
 *
 
557
 * Side effects:
 
558
 *      None.
 
559
 *
 
560
 *----------------------------------------------------------------------------
 
561
 */
 
562
 
 
563
static inline void
 
564
VSockVmciTestUnregister(void)
 
565
{
 
566
   if (devOpenCount <= 0 && vsockVmciSocketCount <= 0) {
 
567
      if (VSOCK_AF_IS_REGISTERED(vsockVmciFamilyOps.family)) {
 
568
         VSockVmciUnregisterAddressFamily();
 
569
      }
 
570
   }
 
571
}
 
572
 
 
573
 
 
574
/*
 
575
 * Helper functions.
 
576
 */
 
577
 
 
578
 
 
579
#ifdef VMX86_TOOLS
 
580
/*
 
581
 *----------------------------------------------------------------------------
 
582
 *
 
583
 * VSockVmciNotifyWaitingWrite --
 
584
 *
 
585
 *      Determines if the conditions have been met to notify a waiting writer.
 
586
 *
 
587
 * Results:
 
588
 *      TRUE if a notification should be sent, FALSE otherwise.
 
589
 *
 
590
 * Side effects:
 
591
 *      None.
 
592
 *
 
593
 *----------------------------------------------------------------------------
 
594
 */
 
595
 
 
596
static Bool
 
597
VSockVmciNotifyWaitingWrite(VSockVmciSock *vsk)    // IN
 
598
{
 
599
#ifdef VSOCK_OPTIMIZATION_WAITING_NOTIFY
 
600
   if (!vsk->peerWaitingWrite) {
 
601
      return FALSE;
 
602
   }
 
603
 
 
604
   /*
 
605
    * For now we ignore the wait information and just see if there is any room
 
606
    * to write anything.  Note that improving this function to be more
 
607
    * intelligent will not require a protocol change and will retain
 
608
    * compatibility between endpoints with mixed versions of this function.
 
609
    */
 
610
   return VMCIQueue_FreeSpace(vsk->consumeQ,
 
611
                              vsk->produceQ, vsk->consumeSize) > 0;
 
612
#else
 
613
   return TRUE;
 
614
#endif
 
615
}
 
616
 
 
617
 
 
618
/*
 
619
 *----------------------------------------------------------------------------
 
620
 *
 
621
 * VSockVmciNotifyWaitingRead --
 
622
 *
 
623
 *      Determines if the conditions have been met to notify a waiting reader.
 
624
 *
 
625
 * Results:
 
626
 *      TRUE if a notification should be sent, FALSE otherwise.
 
627
 *
 
628
 * Side effects:
 
629
 *      None.
 
630
 *
 
631
 *----------------------------------------------------------------------------
 
632
 */
 
633
 
 
634
static Bool
 
635
VSockVmciNotifyWaitingRead(VSockVmciSock *vsk)  // IN
 
636
{
 
637
#ifdef VSOCK_OPTIMIZATION_WAITING_NOTIFY
 
638
   if (!vsk->peerWaitingRead) {
 
639
      return FALSE;
 
640
   }
 
641
 
 
642
   /*
 
643
    * For now we ignore the wait information and just see if there is any data
 
644
    * to read.  Note that improving this function to be more intelligent will
 
645
    * not require a protocol change and will retain compatibility between
 
646
    * endpoints with mixed versions of this function.
 
647
    */
 
648
   return VMCIQueue_BufReady(vsk->produceQ,
 
649
                             vsk->consumeQ, vsk->produceSize) > 0;
 
650
#else
 
651
   return TRUE;
 
652
#endif
 
653
}
 
654
 
 
655
 
 
656
/*
 
657
 *----------------------------------------------------------------------------
 
658
 *
 
659
 * VSockVmciHandleWaitingWrite --
 
660
 *
 
661
 *      Handles an incoming waiting write message.
 
662
 *
 
663
 * Results:
 
664
 *      None.
 
665
 *
 
666
 * Side effects:
 
667
 *      May send a notification to the peer, may update socket's wait info
 
668
 *      structure.
 
669
 *
 
670
 *----------------------------------------------------------------------------
 
671
 */
 
672
 
 
673
static void
 
674
VSockVmciHandleWaitingWrite(struct sock *sk,            // IN
 
675
                            VSockPacket *pkt,           // IN
 
676
                            Bool bottomHalf,            // IN
 
677
                            struct sockaddr_vm *dst,    // IN
 
678
                            struct sockaddr_vm *src)    // IN
 
679
{
 
680
#ifdef VSOCK_OPTIMIZATION_WAITING_NOTIFY
 
681
   VSockVmciSock *vsk;
 
682
 
 
683
   vsk = vsock_sk(sk);
 
684
 
 
685
   vsk->peerWaitingWrite = TRUE;
 
686
   memcpy(&vsk->peerWaitingWriteInfo, &pkt->u.wait,
 
687
          sizeof vsk->peerWaitingWriteInfo);
 
688
 
 
689
   if (VSockVmciNotifyWaitingWrite(vsk)) {
 
690
      Bool sent;
 
691
 
 
692
      if (bottomHalf) {
 
693
         sent = VSOCK_SEND_READ_BH(dst, src) > 0;
 
694
      } else {
 
695
         sent = VSOCK_SEND_READ(sk) > 0;
 
696
      }
 
697
 
 
698
      if (sent) {
 
699
         vsk->peerWaitingWrite = FALSE;
 
700
      }
 
701
   }
 
702
#endif
 
703
}
 
704
 
 
705
 
 
706
/*
 
707
 *----------------------------------------------------------------------------
 
708
 *
 
709
 * VSockVmciHandleWaitingRead --
 
710
 *
 
711
 *      Handles an incoming waiting read message.
 
712
 *
 
713
 * Results:
 
714
 *      None.
 
715
 *
 
716
 * Side effects:
 
717
 *      May send a notification to the peer, may update socket's wait info
 
718
 *      structure.
 
719
 *
 
720
 *----------------------------------------------------------------------------
 
721
 */
 
722
 
 
723
static void
 
724
VSockVmciHandleWaitingRead(struct sock *sk,             // IN
 
725
                           VSockPacket *pkt,            // IN
 
726
                           Bool bottomHalf,             // IN
 
727
                           struct sockaddr_vm *dst,     // IN
 
728
                           struct sockaddr_vm *src)     // IN
 
729
{
 
730
#ifdef VSOCK_OPTIMIZATION_WAITING_NOTIFY
 
731
   VSockVmciSock *vsk;
 
732
 
 
733
   vsk = vsock_sk(sk);
 
734
 
 
735
   vsk->peerWaitingRead = TRUE;
 
736
   memcpy(&vsk->peerWaitingReadInfo, &pkt->u.wait,
 
737
          sizeof vsk->peerWaitingReadInfo);
 
738
 
 
739
   if (VSockVmciNotifyWaitingRead(vsk)) {
 
740
      Bool sent;
 
741
 
 
742
      if (bottomHalf) {
 
743
         sent = VSOCK_SEND_WROTE_BH(dst, src) > 0;
 
744
      } else {
 
745
         sent = VSOCK_SEND_WROTE(sk) > 0;
 
746
      }
 
747
 
 
748
      if (sent) {
 
749
         vsk->peerWaitingRead = FALSE;
 
750
      }
 
751
   }
 
752
#endif
 
753
}
 
754
#endif
 
755
 
 
756
 
 
757
/*
 
758
 *----------------------------------------------------------------------------
 
759
 *
 
760
 * VSockVmciRecvDgramCB --
 
761
 *
 
762
 *    VMCI Datagram receive callback.  This function is used specifically for
 
763
 *    SOCK_DGRAM sockets.
 
764
 *
 
765
 *    This is invoked as part of a tasklet that's scheduled when the VMCI
 
766
 *    interrupt fires.  This is run in bottom-half context and if it ever needs
 
767
 *    to sleep it should defer that work to a work queue.
 
768
 *
 
769
 * Results:
 
770
 *    Zero on success, negative error code on failure.
 
771
 *
 
772
 * Side effects:
 
773
 *    An sk_buff is created and queued with this socket.
 
774
 *
 
775
 *----------------------------------------------------------------------------
 
776
 */
 
777
 
 
778
static int
 
779
VSockVmciRecvDgramCB(void *data,           // IN
 
780
                     VMCIDatagram *dg)     // IN
 
781
{
 
782
   struct sock *sk;
 
783
   size_t size;
 
784
   struct sk_buff *skb;
 
785
 
 
786
   ASSERT(dg);
 
787
   ASSERT(dg->payloadSize <= VMCI_MAX_DG_PAYLOAD_SIZE);
 
788
 
 
789
   sk = (struct sock *)data;
 
790
 
 
791
   ASSERT(sk);
 
792
   /* XXX Figure out why sk->compat_sk_socket can be NULL. */
 
793
   ASSERT(sk->compat_sk_socket ? sk->compat_sk_socket->type == SOCK_DGRAM : 1);
 
794
 
 
795
   size = VMCI_DG_SIZE(dg);
 
796
 
 
797
   /*
 
798
    * Attach the packet to the socket's receive queue as an sk_buff.
 
799
    */
 
800
   skb = alloc_skb(size, GFP_ATOMIC);
 
801
   if (skb) {
 
802
      /* compat_sk_receive_skb() will do a sock_put(), so hold here. */
 
803
      sock_hold(sk);
 
804
      skb_put(skb, size);
 
805
      memcpy(skb->data, dg, size);
 
806
      compat_sk_receive_skb(sk, skb, 0);
 
807
   }
 
808
 
 
809
   return 0;
 
810
}
 
811
 
 
812
 
 
813
#ifdef VMX86_TOOLS
 
814
/*
 
815
 *----------------------------------------------------------------------------
 
816
 *
 
817
 * VSockVmciRecvStreamCB --
 
818
 *
 
819
 *    VMCI stream receive callback for control datagrams.  This function is
 
820
 *    used specifically for SOCK_STREAM sockets.
 
821
 *
 
822
 *    This is invoked as part of a tasklet that's scheduled when the VMCI
 
823
 *    interrupt fires.  This is run in bottom-half context but it defers most
 
824
 *    of its work to the packet handling work queue.
 
825
 *
 
826
 * Results:
 
827
 *    Zero on success, negative error code on failure.
 
828
 *
 
829
 * Side effects:
 
830
 *    None.
 
831
 *
 
832
 *----------------------------------------------------------------------------
 
833
 */
 
834
 
 
835
static int
 
836
VSockVmciRecvStreamCB(void *data,           // IN
 
837
                      VMCIDatagram *dg)     // IN
 
838
{
 
839
   struct sock *sk;
 
840
   struct sockaddr_vm dst;
 
841
   struct sockaddr_vm src;
 
842
   VSockPacket *pkt;
 
843
   Bool processPkt;
 
844
   int err;
 
845
 
 
846
   ASSERT(dg);
 
847
   ASSERT(dg->payloadSize <= VMCI_MAX_DG_PAYLOAD_SIZE);
 
848
 
 
849
   sk = NULL;
 
850
   err = VMCI_SUCCESS;
 
851
   processPkt = TRUE;
 
852
 
 
853
   /*
 
854
    * Ignore incoming packets from contexts without sockets, or resources that
 
855
    * aren't vsock implementations.
 
856
    */
 
857
   if (!VSockAddr_SocketContext(VMCI_HANDLE_TO_CONTEXT_ID(dg->src)) ||
 
858
       VSOCK_PACKET_RID != VMCI_HANDLE_TO_RESOURCE_ID(dg->src)) {
 
859
      return VMCI_ERROR_NO_ACCESS;
 
860
   }
 
861
 
 
862
   if (VMCI_DG_SIZE(dg) < sizeof *pkt) {
 
863
      /* Drop datagrams that do not contain full VSock packets. */
 
864
      return VMCI_ERROR_INVALID_ARGS;
 
865
   }
 
866
 
 
867
   pkt = (VSockPacket *)dg;
 
868
 
 
869
   LOG_PACKET(pkt);
 
870
 
 
871
   /*
 
872
    * Find the socket that should handle this packet.  First we look for
 
873
    * a connected socket and if there is none we look for a socket bound to
 
874
    * the destintation address.
 
875
    *
 
876
    * Note that we don't initialize the family member of the src and dst
 
877
    * sockaddr_vm since we don't want to call VMCISock_GetAFValue() and
 
878
    * possibly register the address family.
 
879
    */
 
880
   VSockAddr_InitNoFamily(&src,
 
881
                          VMCI_HANDLE_TO_CONTEXT_ID(pkt->dg.src),
 
882
                          pkt->srcPort);
 
883
 
 
884
   VSockAddr_InitNoFamily(&dst,
 
885
                          VMCI_HANDLE_TO_CONTEXT_ID(pkt->dg.dst),
 
886
                          pkt->dstPort);
 
887
 
 
888
   sk = VSockVmciFindConnectedSocket(&src, &dst);
 
889
   if (!sk) {
 
890
      sk = VSockVmciFindBoundSocket(&dst);
 
891
      if (!sk) {
 
892
         /*
 
893
          * We could not find a socket for this specified address.  If this
 
894
          * packet is a RST, we just drop it.  If it is another packet, we send
 
895
          * a RST.  Note that we do not send a RST reply to RSTs so that we do
 
896
          * not continually send RSTs between two endpoints.
 
897
          *
 
898
          * Note that since this is a reply, dst is src and src is dst.
 
899
          */
 
900
         if (VSOCK_SEND_RESET_BH(&dst, &src, pkt) < 0) {
 
901
            Log("unable to send reset.\n");
 
902
         }
 
903
         err = VMCI_ERROR_NOT_FOUND;
 
904
         goto out;
 
905
      }
 
906
   }
 
907
 
 
908
   /*
 
909
    * If the received packet type is beyond all types known to this
 
910
    * implementation, reply with an invalid message.  Hopefully this will help
 
911
    * when implementing backwards compatibility in the future.
 
912
    */
 
913
   if (pkt->type >= VSOCK_PACKET_TYPE_MAX) {
 
914
      if (VSOCK_SEND_INVALID_BH(&dst, &src) < 0) {
 
915
         Warning("unable to send reply for invalid packet.\n");
 
916
         err = VMCI_ERROR_INVALID_ARGS;
 
917
         goto out;
 
918
      }
 
919
   }
 
920
 
 
921
   /*
 
922
    * We do most everything in a work queue, but let's fast path the
 
923
    * notification of reads and writes to help data transfer performance.  We
 
924
    * can only do this if there is no process context code executing for this
 
925
    * socket since that may change the state.
 
926
    */
 
927
   bh_lock_sock(sk);
 
928
 
 
929
   if (!compat_sock_owned_by_user(sk) && sk->compat_sk_state == SS_CONNECTED) {
 
930
      switch (pkt->type) {
 
931
      case VSOCK_PACKET_TYPE_WROTE:
 
932
         sk->compat_sk_data_ready(sk, 0);
 
933
         processPkt = FALSE;
 
934
         break;
 
935
      case VSOCK_PACKET_TYPE_READ:
 
936
         sk->compat_sk_write_space(sk);
 
937
         processPkt = FALSE;
 
938
         break;
 
939
      case VSOCK_PACKET_TYPE_WAITING_WRITE:
 
940
         VSockVmciHandleWaitingWrite(sk, pkt, TRUE, &dst, &src);
 
941
         processPkt = FALSE;
 
942
         break;
 
943
 
 
944
      case VSOCK_PACKET_TYPE_WAITING_READ:
 
945
         VSockVmciHandleWaitingRead(sk, pkt, TRUE, &dst, &src);
 
946
         processPkt = FALSE;
 
947
         break;
 
948
      }
 
949
   }
 
950
 
 
951
   bh_unlock_sock(sk);
 
952
 
 
953
   if (processPkt) {
 
954
      VSockRecvPktInfo *recvPktInfo;
 
955
 
 
956
      recvPktInfo = kmalloc(sizeof *recvPktInfo, GFP_ATOMIC);
 
957
      if (!recvPktInfo) {
 
958
         if (VSOCK_SEND_RESET_BH(&dst, &src, pkt) < 0) {
 
959
            Warning("unable to send reset\n");
 
960
         }
 
961
         err = VMCI_ERROR_NO_MEM;
 
962
         goto out;
 
963
      }
 
964
 
 
965
      recvPktInfo->sk = sk;
 
966
      memcpy(&recvPktInfo->pkt, pkt, sizeof recvPktInfo->pkt);
 
967
      COMPAT_INIT_WORK(&recvPktInfo->work, VSockVmciRecvPktWork, recvPktInfo);
 
968
 
 
969
      compat_schedule_work(&recvPktInfo->work);
 
970
      /*
 
971
       * Clear sk so that the reference count incremented by one of the Find
 
972
       * functions above is not decremented below.  We need that reference
 
973
       * count for the packet handler we've scheduled to run.
 
974
       */
 
975
      sk = NULL;
 
976
   }
 
977
 
 
978
out:
 
979
   if (sk) {
 
980
      sock_put(sk);
 
981
   }
 
982
   return err;
 
983
}
 
984
 
 
985
 
 
986
/*
 
987
 *----------------------------------------------------------------------------
 
988
 *
 
989
 * VSockVmciPeerAttachCB --
 
990
 *
 
991
 *    Invoked when a peer attaches to a queue pair.
 
992
 *
 
993
 *    Right now this does not do anything.
 
994
 *
 
995
 * Results:
 
996
 *    None.
 
997
 *
 
998
 * Side effects:
 
999
 *    May modify socket state and signal socket.
 
1000
 *
 
1001
 *----------------------------------------------------------------------------
 
1002
 */
 
1003
 
 
1004
static void
 
1005
VSockVmciPeerAttachCB(VMCIId subId,             // IN
 
1006
                      VMCI_EventData *eData,    // IN
 
1007
                      void *clientData)         // IN
 
1008
{
 
1009
   struct sock *sk;
 
1010
   VMCIEventPayload_QP *ePayload;
 
1011
   VSockVmciSock *vsk;
 
1012
 
 
1013
   ASSERT(eData);
 
1014
   ASSERT(clientData);
 
1015
 
 
1016
   sk = (struct sock *)clientData;
 
1017
   ePayload = VMCIEventDataPayload(eData);
 
1018
 
 
1019
   vsk = vsock_sk(sk);
 
1020
 
 
1021
   bh_lock_sock(sk);
 
1022
 
 
1023
   /*
 
1024
    * XXX This is lame, we should provide a way to lookup sockets by qpHandle.
 
1025
    */
 
1026
   if (VMCI_HANDLE_EQUAL(vsk->qpHandle, ePayload->handle)) {
 
1027
      /*
 
1028
       * XXX This doesn't do anything, but in the future we may want to set
 
1029
       * a flag here to verify the attach really did occur and we weren't just
 
1030
       * sent a datagram claiming it was.
 
1031
       */
 
1032
      goto out;
 
1033
   }
 
1034
 
 
1035
out:
 
1036
   bh_unlock_sock(sk);
 
1037
}
 
1038
 
 
1039
 
 
1040
/*
 
1041
 *----------------------------------------------------------------------------
 
1042
 *
 
1043
 * VSockVmciHandleDetach --
 
1044
 *
 
1045
 *      Perform the work necessary when the peer has detached.
 
1046
 *
 
1047
 *      Note that this assumes the socket lock is held.
 
1048
 *
 
1049
 * Results:
 
1050
 *      None.
 
1051
 *
 
1052
 * Side effects:
 
1053
 *      The socket's and its peer's shutdown mask will be set appropriately,
 
1054
 *      and any callers waiting on this socket will be awoken.
 
1055
 *
 
1056
 *----------------------------------------------------------------------------
 
1057
 */
 
1058
 
 
1059
static INLINE void
 
1060
VSockVmciHandleDetach(struct sock *sk) // IN
 
1061
{
 
1062
   VSockVmciSock *vsk;
 
1063
 
 
1064
   ASSERT(sk);
 
1065
 
 
1066
   vsk = vsock_sk(sk);
 
1067
   if (!VMCI_HANDLE_INVALID(vsk->qpHandle)) {
 
1068
      ASSERT(vsk->produceQ);
 
1069
      ASSERT(vsk->consumeQ);
 
1070
 
 
1071
      /* On a detach the peer will not be sending or receiving anymore. */
 
1072
      vsk->peerShutdown = SHUTDOWN_MASK;
 
1073
 
 
1074
      /*
 
1075
       * We should not be sending anymore since the peer won't be there to
 
1076
       * receive, but we can still receive if there is data left in our consume
 
1077
       * queue.
 
1078
       */
 
1079
      sk->compat_sk_shutdown |= SEND_SHUTDOWN;
 
1080
      if (VMCIQueue_BufReady(vsk->consumeQ,
 
1081
                             vsk->produceQ, vsk->consumeSize) <= 0) {
 
1082
         sk->compat_sk_shutdown |= RCV_SHUTDOWN;
 
1083
         sk->compat_sk_state = SS_UNCONNECTED;
 
1084
      }
 
1085
      sk->compat_sk_state_change(sk);
 
1086
   }
 
1087
}
 
1088
 
 
1089
 
 
1090
/*
 
1091
 *----------------------------------------------------------------------------
 
1092
 *
 
1093
 * VSockVmciPeerDetachCB --
 
1094
 *
 
1095
 *    Invoked when a peer detaches from a queue pair.
 
1096
 *
 
1097
 * Results:
 
1098
 *    None.
 
1099
 *
 
1100
 * Side effects:
 
1101
 *    May modify socket state and signal socket.
 
1102
 *
 
1103
 *----------------------------------------------------------------------------
 
1104
 */
 
1105
 
 
1106
static void
 
1107
VSockVmciPeerDetachCB(VMCIId subId,             // IN
 
1108
                      VMCI_EventData *eData,    // IN
 
1109
                      void *clientData)         // IN
 
1110
{
 
1111
   struct sock *sk;
 
1112
   VMCIEventPayload_QP *ePayload;
 
1113
   VSockVmciSock *vsk;
 
1114
 
 
1115
   ASSERT(eData);
 
1116
   ASSERT(clientData);
 
1117
 
 
1118
   sk = (struct sock *)clientData;
 
1119
   ePayload = VMCIEventDataPayload(eData);
 
1120
   vsk = vsock_sk(sk);
 
1121
   if (VMCI_HANDLE_INVALID(ePayload->handle)) {
 
1122
      return;
 
1123
   }
 
1124
 
 
1125
   /*
 
1126
    * XXX This is lame, we should provide a way to lookup sockets by qpHandle.
 
1127
    */
 
1128
   bh_lock_sock(sk);
 
1129
 
 
1130
   if (VMCI_HANDLE_EQUAL(vsk->qpHandle, ePayload->handle)) {
 
1131
      VSockVmciHandleDetach(sk);
 
1132
   }
 
1133
 
 
1134
   bh_unlock_sock(sk);
 
1135
}
 
1136
 
 
1137
 
 
1138
/*
 
1139
 *----------------------------------------------------------------------------
 
1140
 *
 
1141
 * VSockVmciQPResumedCB --
 
1142
 *
 
1143
 *    Invoked when a VM is resumed.  We must mark all connected stream sockets
 
1144
 *    as detached.
 
1145
 *
 
1146
 * Results:
 
1147
 *    None.
 
1148
 *
 
1149
 * Side effects:
 
1150
 *    May modify socket state and signal socket.
 
1151
 *
 
1152
 *----------------------------------------------------------------------------
 
1153
 */
 
1154
 
 
1155
static void
 
1156
VSockVmciQPResumedCB(VMCIId subId,             // IN
 
1157
                     VMCI_EventData *eData,    // IN
 
1158
                     void *clientData)         // IN
 
1159
{
 
1160
   uint32 i;
 
1161
 
 
1162
   spin_lock_bh(&vsockTableLock);
 
1163
 
 
1164
   /*
 
1165
    * XXX This loop should probably be provided by util.{h,c}, but that's for
 
1166
    * another day.
 
1167
    */
 
1168
   for (i = 0; i < ARRAYSIZE(vsockConnectedTable); i++) {
 
1169
      VSockVmciSock *vsk;
 
1170
 
 
1171
      list_for_each_entry(vsk, &vsockConnectedTable[i], connectedTable) {
 
1172
         struct sock *sk = sk_vsock(vsk);
 
1173
 
 
1174
         /*
 
1175
          * XXX Technically this is racy but the resulting outcome from such
 
1176
          * a race is relatively harmless.  My next change will be a fix to
 
1177
          * this.
 
1178
          */
 
1179
         VSockVmciHandleDetach(sk);
 
1180
      }
 
1181
   }
 
1182
 
 
1183
   spin_unlock_bh(&vsockTableLock);
 
1184
}
 
1185
 
 
1186
 
 
1187
/*
 
1188
 *----------------------------------------------------------------------------
 
1189
 *
 
1190
 * VSockVmciPendingWork --
 
1191
 *
 
1192
 *    Releases the resources for a pending socket if it has not reached the
 
1193
 *    connected state and been accepted by a user process.
 
1194
 *
 
1195
 * Results:
 
1196
 *    None.
 
1197
 *
 
1198
 * Side effects:
 
1199
 *    The socket may be removed from the connected list and all its resources
 
1200
 *    freed.
 
1201
 *
 
1202
 *----------------------------------------------------------------------------
 
1203
 */
 
1204
 
 
1205
static void
 
1206
VSockVmciPendingWork(compat_delayed_work_arg work)    // IN
 
1207
{
 
1208
   struct sock *sk;
 
1209
   struct sock *listener;
 
1210
   VSockVmciSock *vsk;
 
1211
   Bool cleanup;
 
1212
 
 
1213
   vsk = COMPAT_DELAYED_WORK_GET_DATA(work, VSockVmciSock, dwork);
 
1214
   ASSERT(vsk);
 
1215
 
 
1216
   sk = sk_vsock(vsk);
 
1217
   listener = vsk->listener;
 
1218
   cleanup = TRUE;
 
1219
 
 
1220
   ASSERT(listener);
 
1221
 
 
1222
   lock_sock(listener);
 
1223
   lock_sock(sk);
 
1224
 
 
1225
   /*
 
1226
    * The socket should be on the pending list or the accept queue, but not
 
1227
    * both.  It's also possible that the socket isn't on either.
 
1228
    */
 
1229
   ASSERT(    ( VSockVmciIsPending(sk) && !VSockVmciInAcceptQueue(sk))
 
1230
           || (!VSockVmciIsPending(sk) &&  VSockVmciInAcceptQueue(sk))
 
1231
           || (!VSockVmciIsPending(sk) && !VSockVmciInAcceptQueue(sk)));
 
1232
 
 
1233
   if (VSockVmciIsPending(sk)) {
 
1234
      VSockVmciRemovePending(listener, sk);
 
1235
   } else if (!vsk->rejected) {
 
1236
      /*
 
1237
       * We are not on the pending list and accept() did not reject us, so we
 
1238
       * must have been accepted by our user process.  We just need to drop our
 
1239
       * references to the sockets and be on our way.
 
1240
       */
 
1241
      cleanup = FALSE;
 
1242
      goto out;
 
1243
   }
 
1244
 
 
1245
   listener->compat_sk_ack_backlog--;
 
1246
 
 
1247
   /*
 
1248
    * We need to remove ourself from the global connected sockets list so
 
1249
    * incoming packets can't find this socket, and to reduce the reference
 
1250
    * count.
 
1251
    */
 
1252
   if (VSockVmciInConnectedTable(sk)) {
 
1253
      VSockVmciRemoveConnected(sk);
 
1254
   }
 
1255
 
 
1256
   sk->compat_sk_state = SS_FREE;
 
1257
 
 
1258
out:
 
1259
   release_sock(sk);
 
1260
   release_sock(listener);
 
1261
   if (cleanup) {
 
1262
      sock_put(sk);
 
1263
   }
 
1264
   sock_put(sk);
 
1265
   sock_put(listener);
 
1266
}
 
1267
 
 
1268
 
 
1269
/*
 
1270
 *----------------------------------------------------------------------------
 
1271
 *
 
1272
 * VSockVmciRecvPktWork --
 
1273
 *
 
1274
 *    Handles an incoming control packet for the provided socket.  This is the
 
1275
 *    state machine for our stream sockets.
 
1276
 *
 
1277
 * Results:
 
1278
 *    None.
 
1279
 *
 
1280
 * Side effects:
 
1281
 *    May set state and wakeup threads waiting for socket state to change.
 
1282
 *
 
1283
 *----------------------------------------------------------------------------
 
1284
 */
 
1285
 
 
1286
static void
 
1287
VSockVmciRecvPktWork(compat_work_arg work)  // IN
 
1288
{
 
1289
   int err;
 
1290
   VSockRecvPktInfo *recvPktInfo;
 
1291
   VSockPacket *pkt;
 
1292
   VSockVmciSock *vsk;
 
1293
   struct sock *sk;
 
1294
 
 
1295
   recvPktInfo = COMPAT_WORK_GET_DATA(work, VSockRecvPktInfo);
 
1296
   ASSERT(recvPktInfo);
 
1297
 
 
1298
   err = 0;
 
1299
   sk = recvPktInfo->sk;
 
1300
   pkt = &recvPktInfo->pkt;
 
1301
   vsk = vsock_sk(sk);
 
1302
 
 
1303
   ASSERT(vsk);
 
1304
   ASSERT(pkt);
 
1305
   ASSERT(pkt->type < VSOCK_PACKET_TYPE_MAX);
 
1306
 
 
1307
   lock_sock(sk);
 
1308
 
 
1309
   switch (sk->compat_sk_state) {
 
1310
   case SS_LISTEN:
 
1311
      err = VSockVmciRecvListen(sk, pkt);
 
1312
      break;
 
1313
   case SS_UNCONNECTED:
 
1314
      Log("packet received for socket in unconnected state; dropping.\n");
 
1315
      goto out;
 
1316
   case SS_CONNECTING:
 
1317
      /*
 
1318
       * Processing of pending connections for servers goes through the
 
1319
       * listening socket, so see VSockVmciRecvListen() for that path.
 
1320
       */
 
1321
      err = VSockVmciRecvConnectingClient(sk, pkt);
 
1322
      break;
 
1323
   case SS_CONNECTED:
 
1324
      err = VSockVmciRecvConnected(sk, pkt);
 
1325
      break;
 
1326
   case SS_DISCONNECTING:
 
1327
      Log("packet receieved for socket in disconnecting state; dropping.\n");
 
1328
      goto out;
 
1329
   case SS_FREE:
 
1330
      Log("packet receieved for socket in free state; dropping.\n");
 
1331
      goto out;
 
1332
   default:
 
1333
      Log("socket is in invalid state; dropping packet.\n");
 
1334
      goto out;
 
1335
   }
 
1336
 
 
1337
out:
 
1338
   release_sock(sk);
 
1339
   kfree(recvPktInfo);
 
1340
   /*
 
1341
    * Release reference obtained in the stream callback when we fetched this
 
1342
    * socket out of the bound or connected list.
 
1343
    */
 
1344
   sock_put(sk);
 
1345
}
 
1346
 
 
1347
 
 
1348
/*
 
1349
 *----------------------------------------------------------------------------
 
1350
 *
 
1351
 * VSockVmciRecvListen --
 
1352
 *
 
1353
 *    Receives packets for sockets in the listen state.
 
1354
 *
 
1355
 *    Note that this assumes the socket lock is held.
 
1356
 *
 
1357
 * Results:
 
1358
 *    Zero on success, negative error code on failure.
 
1359
 *
 
1360
 * Side effects:
 
1361
 *    A new socket may be created and a negotiate control packet is sent.
 
1362
 *
 
1363
 *----------------------------------------------------------------------------
 
1364
 */
 
1365
 
 
1366
static int
 
1367
VSockVmciRecvListen(struct sock *sk,   // IN
 
1368
                    VSockPacket *pkt)  // IN
 
1369
{
 
1370
   VSockVmciSock *vsk;
 
1371
   struct sock *pending;
 
1372
   VSockVmciSock *vpending;
 
1373
   int err;
 
1374
   uint64 qpSize;
 
1375
 
 
1376
   ASSERT(sk);
 
1377
   ASSERT(pkt);
 
1378
   ASSERT(sk->compat_sk_state == SS_LISTEN);
 
1379
 
 
1380
   vsk = vsock_sk(sk);
 
1381
   err = 0;
 
1382
 
 
1383
   /*
 
1384
    * Because we are in the listen state, we could be receiving a packet for
 
1385
    * ourself or any previous connection requests that we received.  If it's
 
1386
    * the latter, we try to find a socket in our list of pending connections
 
1387
    * and, if we do, call the appropriate handler for the state that that
 
1388
    * socket is in.  Otherwise we try to service the connection request.
 
1389
    */
 
1390
   pending = VSockVmciGetPending(sk, pkt);
 
1391
   if (pending) {
 
1392
      lock_sock(pending);
 
1393
      switch (pending->compat_sk_state) {
 
1394
      case SS_CONNECTING:
 
1395
         err = VSockVmciRecvConnectingServer(sk, pending, pkt);
 
1396
         break;
 
1397
      default:
 
1398
         VSOCK_SEND_RESET(pending, pkt);
 
1399
         err = -EINVAL;
 
1400
      }
 
1401
 
 
1402
      if (err < 0) {
 
1403
         VSockVmciRemovePending(sk, pending);
 
1404
      }
 
1405
 
 
1406
      release_sock(pending);
 
1407
      VSockVmciReleasePending(pending);
 
1408
 
 
1409
      return err;
 
1410
   }
 
1411
 
 
1412
   /*
 
1413
    * The listen state only accepts connection requests.  Reply with a reset
 
1414
    * unless we received a reset.
 
1415
    */
 
1416
   if (pkt->type != VSOCK_PACKET_TYPE_REQUEST ||
 
1417
       pkt->u.size == 0) {
 
1418
      VSOCK_SEND_RESET(sk, pkt);
 
1419
      return -EINVAL;
 
1420
   }
 
1421
 
 
1422
   /*
 
1423
    * If this socket can't accommodate this connection request, we send
 
1424
    * a reset.  Otherwise we create and initialize a child socket and reply
 
1425
    * with a connection negotiation.
 
1426
    */
 
1427
   if (sk->compat_sk_ack_backlog >= sk->compat_sk_max_ack_backlog) {
 
1428
      VSOCK_SEND_RESET(sk, pkt);
 
1429
      return -ECONNREFUSED;
 
1430
   }
 
1431
 
 
1432
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
 
1433
   pending = __VSockVmciCreate(NULL, GFP_KERNEL);
 
1434
#else
 
1435
   pending = __VSockVmciCreate(compat_sock_net(sk), NULL, GFP_KERNEL);
 
1436
#endif
 
1437
   if (!pending) {
 
1438
      VSOCK_SEND_RESET(sk, pkt);
 
1439
      return -ENOMEM;
 
1440
   }
 
1441
 
 
1442
   vpending = vsock_sk(pending);
 
1443
   ASSERT(vpending);
 
1444
   ASSERT(vsk->localAddr.svm_port == pkt->dstPort);
 
1445
 
 
1446
   VSockAddr_Init(&vpending->localAddr,
 
1447
                  VMCI_GetContextID(),
 
1448
                  pkt->dstPort);
 
1449
   VSockAddr_Init(&vpending->remoteAddr,
 
1450
                  VMCI_HANDLE_TO_CONTEXT_ID(pkt->dg.src),
 
1451
                  pkt->srcPort);
 
1452
 
 
1453
   /*
 
1454
    * If the proposed size fits within our min/max, accept
 
1455
    * it. Otherwise propose our own size.
 
1456
    */
 
1457
   if (pkt->u.size >= vsk->queuePairMinSize &&
 
1458
      pkt->u.size <= vsk->queuePairMaxSize) {
 
1459
      qpSize = pkt->u.size;
 
1460
   } else {
 
1461
      qpSize = vsk->queuePairSize;
 
1462
   }
 
1463
 
 
1464
   err = VSOCK_SEND_NEGOTIATE(pending, qpSize);
 
1465
   if (err < 0) {
 
1466
      VSOCK_SEND_RESET(sk, pkt);
 
1467
      sock_put(pending);
 
1468
      err = VSockVmci_ErrorToVSockError(err);
 
1469
      goto out;
 
1470
   }
 
1471
 
 
1472
   VSockVmciAddPending(sk, pending);
 
1473
   sk->compat_sk_ack_backlog++;
 
1474
 
 
1475
   pending->compat_sk_state = SS_CONNECTING;
 
1476
   vpending->produceSize = vpending->consumeSize = pkt->u.size;
 
1477
 
 
1478
   /*
 
1479
    * We might never receive another message for this socket and it's not
 
1480
    * connected to any process, so we have to ensure it gets cleaned up
 
1481
    * ourself.  Our delayed work function will take care of that.  Note that we
 
1482
    * do not ever cancel this function since we have few guarantees about its
 
1483
    * state when calling cancel_delayed_work().  Instead we hold a reference on
 
1484
    * the socket for that function and make it capable of handling cases where
 
1485
    * it needs to do nothing but release that reference.
 
1486
    */
 
1487
   vpending->listener = sk;
 
1488
   sock_hold(sk);
 
1489
   sock_hold(pending);
 
1490
   COMPAT_INIT_DELAYED_WORK(&vpending->dwork, VSockVmciPendingWork, vpending);
 
1491
   compat_schedule_delayed_work(&vpending->dwork, HZ);
 
1492
 
 
1493
out:
 
1494
   return err;
 
1495
}
 
1496
 
 
1497
 
 
1498
/*
 
1499
 *----------------------------------------------------------------------------
 
1500
 *
 
1501
 * VSockVmciRecvConnectingServer --
 
1502
 *
 
1503
 *    Receives packets for sockets in the connecting state on the server side.
 
1504
 *
 
1505
 *    Connecting sockets on the server side can only receive queue pair offer
 
1506
 *    packets.  All others should be treated as cause for closing the
 
1507
 *    connection.
 
1508
 *
 
1509
 *    Note that this assumes the socket lock is held for both sk and pending.
 
1510
 *
 
1511
 * Results:
 
1512
 *    Zero on success, negative error code on failure.
 
1513
 *
 
1514
 * Side effects:
 
1515
 *    A queue pair may be created, an attach control packet may be sent, the
 
1516
 *    socket may transition to the connected state, and a pending caller in
 
1517
 *    accept() may be woken up.
 
1518
 *
 
1519
 *----------------------------------------------------------------------------
 
1520
 */
 
1521
 
 
1522
static int
 
1523
VSockVmciRecvConnectingServer(struct sock *listener, // IN: the listening socket
 
1524
                              struct sock *pending,  // IN: the pending connection
 
1525
                              VSockPacket *pkt)      // IN: current packet
 
1526
{
 
1527
   VSockVmciSock *vpending;
 
1528
   VMCIHandle handle;
 
1529
   VMCIQueue *produceQ;
 
1530
   VMCIQueue *consumeQ;
 
1531
   Bool isLocal;
 
1532
   uint32 flags;
 
1533
   VMCIId detachSubId;
 
1534
   int err;
 
1535
   int skerr;
 
1536
 
 
1537
   ASSERT(listener);
 
1538
   ASSERT(pkt);
 
1539
   ASSERT(listener->compat_sk_state == SS_LISTEN);
 
1540
   ASSERT(pending->compat_sk_state == SS_CONNECTING);
 
1541
 
 
1542
   vpending = vsock_sk(pending);
 
1543
   detachSubId = VMCI_INVALID_ID;
 
1544
 
 
1545
   switch (pkt->type) {
 
1546
   case VSOCK_PACKET_TYPE_OFFER:
 
1547
      if (VMCI_HANDLE_INVALID(pkt->u.handle)) {
 
1548
         VSOCK_SEND_RESET(pending, pkt);
 
1549
         skerr = EPROTO;
 
1550
         err = -EINVAL;
 
1551
         goto destroy;
 
1552
      }
 
1553
      break;
 
1554
   default:
 
1555
      /* Close and cleanup the connection. */
 
1556
      VSOCK_SEND_RESET(pending, pkt);
 
1557
      skerr = EPROTO;
 
1558
      err =  pkt->type == VSOCK_PACKET_TYPE_RST ?
 
1559
                0 :
 
1560
                -EINVAL;
 
1561
      goto destroy;
 
1562
   }
 
1563
 
 
1564
   ASSERT(pkt->type == VSOCK_PACKET_TYPE_OFFER);
 
1565
 
 
1566
   /*
 
1567
    * In order to complete the connection we need to attach to the offered
 
1568
    * queue pair and send an attach notification.  We also subscribe to the
 
1569
    * detach event so we know when our peer goes away, and we do that before
 
1570
    * attaching so we don't miss an event.  If all this succeeds, we update our
 
1571
    * state and wakeup anything waiting in accept() for a connection.
 
1572
    */
 
1573
 
 
1574
   /*
 
1575
    * We don't care about attach since we ensure the other side has attached by
 
1576
    * specifying the ATTACH_ONLY flag below.
 
1577
    */
 
1578
   err = VMCIEvent_Subscribe(VMCI_EVENT_QP_PEER_DETACH,
 
1579
                             VSockVmciPeerDetachCB,
 
1580
                             pending,
 
1581
                             &detachSubId);
 
1582
   if (err < VMCI_SUCCESS) {
 
1583
      VSOCK_SEND_RESET(pending, pkt);
 
1584
      err = VSockVmci_ErrorToVSockError(err);
 
1585
      skerr = -err;
 
1586
      goto destroy;
 
1587
   }
 
1588
 
 
1589
   vpending->detachSubId = detachSubId;
 
1590
 
 
1591
   /* Now attach to the queue pair the client created. */
 
1592
   handle = pkt->u.handle;
 
1593
   isLocal = vpending->remoteAddr.svm_cid == vpending->localAddr.svm_cid;
 
1594
   flags = VMCI_QPFLAG_ATTACH_ONLY;
 
1595
   flags |= isLocal ? VMCI_QPFLAG_LOCAL : 0;
 
1596
 
 
1597
   err = VMCIQueuePair_Alloc(&handle,
 
1598
                             &produceQ, vpending->produceSize,
 
1599
                             &consumeQ, vpending->consumeSize,
 
1600
                             VMCI_HANDLE_TO_CONTEXT_ID(pkt->dg.src),
 
1601
                             flags);
 
1602
   if (err < 0) {
 
1603
      /* We cannot complete this connection: send a reset and close. */
 
1604
      Log("Could not attach to queue pair with %d\n", err);
 
1605
      VSOCK_SEND_RESET(pending, pkt);
 
1606
      err = VSockVmci_ErrorToVSockError(err);
 
1607
      skerr = -err;
 
1608
      goto destroy;
 
1609
   }
 
1610
 
 
1611
   VMCIQueue_Init(handle, produceQ);
 
1612
 
 
1613
   ASSERT(VMCI_HANDLE_EQUAL(handle, pkt->u.handle));
 
1614
   vpending->qpHandle = handle;
 
1615
   vpending->produceQ = produceQ;
 
1616
   vpending->consumeQ = consumeQ;
 
1617
 
 
1618
   /* Notify our peer of our attach. */
 
1619
   err = VSOCK_SEND_ATTACH(pending, handle);
 
1620
   if (err < 0) {
 
1621
      Log("Could not send attach\n");
 
1622
      VSOCK_SEND_RESET(pending, pkt);
 
1623
      err = VSockVmci_ErrorToVSockError(err);
 
1624
      skerr = -err;
 
1625
      goto destroy;
 
1626
   }
 
1627
 
 
1628
   /*
 
1629
    * We have a connection.  Add our connection to the connected list so it no
 
1630
    * longer goes through the listening socket, move it from the listener's
 
1631
    * pending list to the accept queue so callers of accept() can find it.
 
1632
    * Note that enqueueing the socket increments the reference count, so even
 
1633
    * if a reset comes before the connection is accepted, the socket will be
 
1634
    * valid until it is removed from the queue.
 
1635
    */
 
1636
   pending->compat_sk_state = SS_CONNECTED;
 
1637
 
 
1638
   VSockVmciInsertConnected(vsockConnectedSocketsVsk(vpending), pending);
 
1639
 
 
1640
   VSockVmciRemovePending(listener, pending);
 
1641
   VSockVmciEnqueueAccept(listener, pending);
 
1642
 
 
1643
   /*
 
1644
    * Callers of accept() will be be waiting on the listening socket, not the
 
1645
    * pending socket.
 
1646
    */
 
1647
   listener->compat_sk_state_change(listener);
 
1648
 
 
1649
   return 0;
 
1650
 
 
1651
destroy:
 
1652
   pending->compat_sk_err = skerr;
 
1653
   pending->compat_sk_state = SS_UNCONNECTED;
 
1654
   /*
 
1655
    * As long as we drop our reference, all necessary cleanup will handle when
 
1656
    * the cleanup function drops its reference and our destruct implementation
 
1657
    * is called.  Note that since the listen handler will remove pending from
 
1658
    * the pending list upon our failure, the cleanup function won't drop the
 
1659
    * additional reference, which is why we do it here.
 
1660
    */
 
1661
   sock_put(pending);
 
1662
 
 
1663
   return err;
 
1664
}
 
1665
 
 
1666
 
 
1667
/*
 
1668
 *----------------------------------------------------------------------------
 
1669
 *
 
1670
 * VSockVmciRecvConnectingClient --
 
1671
 *
 
1672
 *    Receives packets for sockets in the connecting state on the client side.
 
1673
 *
 
1674
 *    Connecting sockets on the client side should only receive attach packets.
 
1675
 *    All others should be treated as cause for closing the connection.
 
1676
 *
 
1677
 *    Note that this assumes the socket lock is held for both sk and pending.
 
1678
 *
 
1679
 * Results:
 
1680
 *    Zero on success, negative error code on failure.
 
1681
 *
 
1682
 * Side effects:
 
1683
 *    The socket may transition to the connected state and wakeup the pending
 
1684
 *    caller of connect().
 
1685
 *
 
1686
 *----------------------------------------------------------------------------
 
1687
 */
 
1688
 
 
1689
static int
 
1690
VSockVmciRecvConnectingClient(struct sock *sk,       // IN: socket
 
1691
                              VSockPacket *pkt)      // IN: current packet
 
1692
{
 
1693
   VSockVmciSock *vsk;
 
1694
   int err;
 
1695
   int skerr;
 
1696
 
 
1697
   ASSERT(sk);
 
1698
   ASSERT(pkt);
 
1699
   ASSERT(sk->compat_sk_state == SS_CONNECTING);
 
1700
 
 
1701
   vsk = vsock_sk(sk);
 
1702
 
 
1703
   switch (pkt->type) {
 
1704
   case VSOCK_PACKET_TYPE_ATTACH:
 
1705
      if (VMCI_HANDLE_INVALID(pkt->u.handle) ||
 
1706
          !VMCI_HANDLE_EQUAL(pkt->u.handle, vsk->qpHandle)) {
 
1707
         skerr = EPROTO;
 
1708
         err = -EINVAL;
 
1709
         goto destroy;
 
1710
      }
 
1711
 
 
1712
      /*
 
1713
       * Signify the socket is connected and wakeup the waiter in connect().
 
1714
       * Also place the socket in the connected table for accounting (it can
 
1715
       * already be found since it's in the bound table).
 
1716
       */
 
1717
      sk->compat_sk_state = SS_CONNECTED;
 
1718
      sk->compat_sk_socket->state = SS_CONNECTED;
 
1719
      VSockVmciInsertConnected(vsockConnectedSocketsVsk(vsk), sk);
 
1720
      sk->compat_sk_state_change(sk);
 
1721
      break;
 
1722
   case VSOCK_PACKET_TYPE_NEGOTIATE:
 
1723
      if (pkt->u.size == 0 ||
 
1724
          VMCI_HANDLE_TO_CONTEXT_ID(pkt->dg.src) != vsk->remoteAddr.svm_cid ||
 
1725
          pkt->srcPort != vsk->remoteAddr.svm_port ||
 
1726
          !VMCI_HANDLE_INVALID(vsk->qpHandle) ||
 
1727
          vsk->produceQ ||
 
1728
          vsk->consumeQ ||
 
1729
          vsk->produceSize != 0 ||
 
1730
          vsk->consumeSize != 0 ||
 
1731
          vsk->attachSubId != VMCI_INVALID_ID ||
 
1732
          vsk->detachSubId != VMCI_INVALID_ID) {
 
1733
         skerr = EPROTO;
 
1734
         err = -EINVAL;
 
1735
         goto destroy;
 
1736
      }
 
1737
 
 
1738
      err = VSockVmciRecvConnectingClientNegotiate(sk, pkt);
 
1739
      if (err) {
 
1740
         skerr = -err;
 
1741
         goto destroy;
 
1742
      }
 
1743
 
 
1744
      break;
 
1745
   case VSOCK_PACKET_TYPE_RST:
 
1746
      skerr = ECONNRESET;
 
1747
      err = 0;
 
1748
      goto destroy;
 
1749
   default:
 
1750
      /* Close and cleanup the connection. */
 
1751
      skerr = EPROTO;
 
1752
      err = -EINVAL;
 
1753
      goto destroy;
 
1754
   }
 
1755
 
 
1756
   ASSERT(pkt->type == VSOCK_PACKET_TYPE_ATTACH ||
 
1757
          pkt->type == VSOCK_PACKET_TYPE_NEGOTIATE);
 
1758
 
 
1759
   return 0;
 
1760
 
 
1761
destroy:
 
1762
   VSOCK_SEND_RESET(sk, pkt);
 
1763
 
 
1764
   sk->compat_sk_state = SS_UNCONNECTED;
 
1765
   sk->compat_sk_err = skerr;
 
1766
   sk->compat_sk_error_report(sk);
 
1767
   return err;
 
1768
}
 
1769
 
 
1770
 
 
1771
/*
 
1772
 *----------------------------------------------------------------------------
 
1773
 *
 
1774
 * VSockVmciRecvConnectingClientNegotiate --
 
1775
 *
 
1776
 *    Handles a negotiate packet for a client in the connecting state.
 
1777
 *
 
1778
 *    Note that this assumes the socket lock is held for both sk and pending.
 
1779
 *
 
1780
 * Results:
 
1781
 *    Zero on success, negative error code on failure.
 
1782
 *
 
1783
 * Side effects:
 
1784
 *    The socket may transition to the connected state and wakeup the pending
 
1785
 *    caller of connect().
 
1786
 *
 
1787
 *----------------------------------------------------------------------------
 
1788
 */
 
1789
 
 
1790
static int
 
1791
VSockVmciRecvConnectingClientNegotiate(struct sock *sk,   // IN: socket
 
1792
                                       VSockPacket *pkt)  // IN: current packet
 
1793
{
 
1794
   int err;
 
1795
   VSockVmciSock *vsk;
 
1796
   VMCIHandle handle;
 
1797
   VMCIQueue *produceQ;
 
1798
   VMCIQueue *consumeQ;
 
1799
   VMCIId attachSubId;
 
1800
   VMCIId detachSubId;
 
1801
   Bool isLocal;
 
1802
 
 
1803
   vsk = vsock_sk(sk);
 
1804
   handle = VMCI_INVALID_HANDLE;
 
1805
   attachSubId = VMCI_INVALID_ID;
 
1806
   detachSubId = VMCI_INVALID_ID;
 
1807
 
 
1808
   ASSERT(sk);
 
1809
   ASSERT(pkt);
 
1810
   ASSERT(pkt->u.size > 0);
 
1811
   ASSERT(vsk->remoteAddr.svm_cid == VMCI_HANDLE_TO_CONTEXT_ID(pkt->dg.src));
 
1812
   ASSERT(vsk->remoteAddr.svm_port == pkt->srcPort);
 
1813
   ASSERT(VMCI_HANDLE_INVALID(vsk->qpHandle));
 
1814
   ASSERT(vsk->produceQ == NULL);
 
1815
   ASSERT(vsk->consumeQ == NULL);
 
1816
   ASSERT(vsk->produceSize == 0);
 
1817
   ASSERT(vsk->consumeSize == 0);
 
1818
   ASSERT(vsk->attachSubId == VMCI_INVALID_ID);
 
1819
   ASSERT(vsk->detachSubId == VMCI_INVALID_ID);
 
1820
 
 
1821
   /* Verify that we're OK with the proposed queue pair size */
 
1822
   if (pkt->u.size < vsk->queuePairMinSize ||
 
1823
       pkt->u.size > vsk->queuePairMaxSize) {
 
1824
      err = -EINVAL;
 
1825
      goto destroy;
 
1826
   }
 
1827
 
 
1828
   /*
 
1829
    * Subscribe to attach and detach events first.
 
1830
    *
 
1831
    * XXX We attach once for each queue pair created for now so it is easy
 
1832
    * to find the socket (it's provided), but later we should only subscribe
 
1833
    * once and add a way to lookup sockets by queue pair handle.
 
1834
    */
 
1835
   err = VMCIEvent_Subscribe(VMCI_EVENT_QP_PEER_ATTACH,
 
1836
                             VSockVmciPeerAttachCB,
 
1837
                             sk,
 
1838
                             &attachSubId);
 
1839
   if (err < VMCI_SUCCESS) {
 
1840
      err = VSockVmci_ErrorToVSockError(err);
 
1841
      goto destroy;
 
1842
   }
 
1843
 
 
1844
   err = VMCIEvent_Subscribe(VMCI_EVENT_QP_PEER_DETACH,
 
1845
                             VSockVmciPeerDetachCB,
 
1846
                             sk,
 
1847
                             &detachSubId);
 
1848
   if (err < VMCI_SUCCESS) {
 
1849
      err = VSockVmci_ErrorToVSockError(err);
 
1850
      goto destroy;
 
1851
   }
 
1852
 
 
1853
   /* Make VMCI select the handle for us. */
 
1854
   handle = VMCI_INVALID_HANDLE;
 
1855
   isLocal = vsk->remoteAddr.svm_cid == vsk->localAddr.svm_cid;
 
1856
 
 
1857
   err = VMCIQueuePair_Alloc(&handle,
 
1858
                             &produceQ, pkt->u.size,
 
1859
                             &consumeQ, pkt->u.size,
 
1860
                             vsk->remoteAddr.svm_cid,
 
1861
                             isLocal ? VMCI_QPFLAG_LOCAL : 0);
 
1862
   if (err < VMCI_SUCCESS) {
 
1863
      err = VSockVmci_ErrorToVSockError(err);
 
1864
      goto destroy;
 
1865
   }
 
1866
 
 
1867
   VMCIQueue_Init(handle, produceQ);
 
1868
 
 
1869
   err = VSOCK_SEND_QP_OFFER(sk, handle);
 
1870
   if (err < 0) {
 
1871
      err = VSockVmci_ErrorToVSockError(err);
 
1872
      goto destroy;
 
1873
   }
 
1874
 
 
1875
   vsk->qpHandle = handle;
 
1876
   vsk->produceQ = produceQ;
 
1877
   vsk->consumeQ = consumeQ;
 
1878
   vsk->produceSize = vsk->consumeSize = pkt->u.size;
 
1879
   vsk->attachSubId = attachSubId;
 
1880
   vsk->detachSubId = detachSubId;
 
1881
 
 
1882
   return 0;
 
1883
 
 
1884
destroy:
 
1885
   if (attachSubId != VMCI_INVALID_ID) {
 
1886
      VMCIEvent_Unsubscribe(attachSubId);
 
1887
      ASSERT(vsk->attachSubId == VMCI_INVALID_ID);
 
1888
   }
 
1889
 
 
1890
   if (detachSubId != VMCI_INVALID_ID) {
 
1891
      VMCIEvent_Unsubscribe(detachSubId);
 
1892
      ASSERT(vsk->detachSubId == VMCI_INVALID_ID);
 
1893
   }
 
1894
 
 
1895
   if (!VMCI_HANDLE_INVALID(handle)) {
 
1896
      VMCIQueuePair_Detach(handle);
 
1897
      ASSERT(VMCI_HANDLE_INVALID(vsk->qpHandle));
 
1898
   }
 
1899
 
 
1900
   return err;
 
1901
}
 
1902
 
 
1903
 
 
1904
/*
 
1905
 *----------------------------------------------------------------------------
 
1906
 *
 
1907
 * VSockVmciRecvConnected --
 
1908
 *
 
1909
 *    Receives packets for sockets in the connected state.
 
1910
 *
 
1911
 *    Connected sockets should only ever receive detach, wrote, read, or reset
 
1912
 *    control messages.  Others are treated as errors that are ignored.
 
1913
 *
 
1914
 *    Wrote and read signify that the peer has produced or consumed,
 
1915
 *    respectively.
 
1916
 *
 
1917
 *    Detach messages signify that the connection is being closed cleanly and
 
1918
 *    reset messages signify that the connection is being closed in error.
 
1919
 *
 
1920
 *    Note that this assumes the socket lock is held.
 
1921
 *
 
1922
 * Results:
 
1923
 *    Zero on success, negative error code on failure.
 
1924
 *
 
1925
 * Side effects:
 
1926
 *    A queue pair may be created, an offer control packet sent, and the socket
 
1927
 *    may transition to the connecting state.
 
1928
 *
 
1929
 *
 
1930
 *----------------------------------------------------------------------------
 
1931
 */
 
1932
 
 
1933
static int
 
1934
VSockVmciRecvConnected(struct sock *sk,      // IN
 
1935
                       VSockPacket *pkt)     // IN
 
1936
{
 
1937
   ASSERT(sk);
 
1938
   ASSERT(pkt);
 
1939
   ASSERT(sk->compat_sk_state == SS_CONNECTED);
 
1940
 
 
1941
   /*
 
1942
    * In cases where we are closing the connection, it's sufficient to mark
 
1943
    * the state change (and maybe error) and wake up any waiting threads.
 
1944
    * Since this is a connected socket, it's owned by a user process and will
 
1945
    * be cleaned up when the failure is passed back on the current or next
 
1946
    * system call.  Our system call implementations must therefore check for
 
1947
    * error and state changes on entry and when being awoken.
 
1948
    */
 
1949
   switch (pkt->type) {
 
1950
   case VSOCK_PACKET_TYPE_SHUTDOWN:
 
1951
      if (pkt->u.mode) {
 
1952
         VSockVmciSock *vsk = vsock_sk(sk);
 
1953
 
 
1954
         vsk->peerShutdown |= pkt->u.mode;
 
1955
         sk->compat_sk_state_change(sk);
 
1956
      }
 
1957
      break;
 
1958
 
 
1959
   case VSOCK_PACKET_TYPE_RST:
 
1960
      sk->compat_sk_state = SS_DISCONNECTING;
 
1961
      sk->compat_sk_shutdown = SHUTDOWN_MASK;
 
1962
      sk->compat_sk_err = ECONNRESET;
 
1963
      sk->compat_sk_error_report(sk);
 
1964
      break;
 
1965
 
 
1966
   case VSOCK_PACKET_TYPE_WROTE:
 
1967
      sk->compat_sk_data_ready(sk, 0);
 
1968
      break;
 
1969
 
 
1970
   case VSOCK_PACKET_TYPE_READ:
 
1971
      sk->compat_sk_write_space(sk);
 
1972
      break;
 
1973
 
 
1974
   case VSOCK_PACKET_TYPE_WAITING_WRITE:
 
1975
      VSockVmciHandleWaitingWrite(sk, pkt, FALSE, NULL, NULL);
 
1976
      break;
 
1977
 
 
1978
   case VSOCK_PACKET_TYPE_WAITING_READ:
 
1979
      VSockVmciHandleWaitingRead(sk, pkt, FALSE, NULL, NULL);
 
1980
      break;
 
1981
 
 
1982
   default:
 
1983
      return -EINVAL;
 
1984
   }
 
1985
 
 
1986
   return 0;
 
1987
}
 
1988
 
 
1989
 
 
1990
/*
 
1991
 *----------------------------------------------------------------------------
 
1992
 *
 
1993
 * VSockVmciSendControlPktBH --
 
1994
 *
 
1995
 *    Sends a control packet from bottom-half context.
 
1996
 *
 
1997
 * Results:
 
1998
 *    Size of datagram sent on success, negative error code otherwise.  Note
 
1999
 *    that we return a VMCI error message since that's what callers will need
 
2000
 *    to provide.
 
2001
 *
 
2002
 * Side effects:
 
2003
 *    None.
 
2004
 *
 
2005
 *----------------------------------------------------------------------------
 
2006
 */
 
2007
 
 
2008
static int
 
2009
VSockVmciSendControlPktBH(struct sockaddr_vm *src,      // IN
 
2010
                          struct sockaddr_vm *dst,      // IN
 
2011
                          VSockPacketType type,         // IN
 
2012
                          uint64 size,                  // IN
 
2013
                          uint64 mode,                  // IN
 
2014
                          VSockWaitingInfo *wait,       // IN
 
2015
                          VMCIHandle handle)            // IN
 
2016
{
 
2017
   /*
 
2018
    * Note that it is safe to use a single packet across all CPUs since two
 
2019
    * tasklets of the same type are guaranteed to not ever run simultaneously.
 
2020
    * If that ever changes, or VMCI stops using tasklets, we can use per-cpu
 
2021
    * packets.
 
2022
    */
 
2023
   static VSockPacket pkt;
 
2024
 
 
2025
   VSockPacket_Init(&pkt, src, dst, type, size, mode, wait, handle);
 
2026
 
 
2027
   LOG_PACKET(&pkt);
 
2028
#ifdef VSOCK_CONTROL_PACKET_COUNT
 
2029
   controlPacketCount[pkt.type]++;
 
2030
#endif
 
2031
   return VMCIDatagram_Send(&pkt.dg);
 
2032
}
 
2033
 
 
2034
 
 
2035
/*
 
2036
 *----------------------------------------------------------------------------
 
2037
 *
 
2038
 * VSockVmciSendControlPkt --
 
2039
 *
 
2040
 *      Sends a control packet.
 
2041
 *
 
2042
 * Results:
 
2043
 *      Size of datagram sent on success, negative error on failure.
 
2044
 *
 
2045
 * Side effects:
 
2046
 *      None.
 
2047
 *
 
2048
 *----------------------------------------------------------------------------
 
2049
 */
 
2050
 
 
2051
static int
 
2052
VSockVmciSendControlPkt(struct sock *sk,        // IN
 
2053
                        VSockPacketType type,   // IN
 
2054
                        uint64 size,            // IN
 
2055
                        uint64 mode,            // IN
 
2056
                        VSockWaitingInfo *wait, // IN
 
2057
                        VMCIHandle handle)      // IN
 
2058
{
 
2059
   VSockPacket *pkt;
 
2060
   VSockVmciSock *vsk;
 
2061
   int err;
 
2062
 
 
2063
   ASSERT(sk);
 
2064
   /*
 
2065
    * New sockets for connection establishment won't have socket structures
 
2066
    * yet; if one exists, ensure it is of the proper type.
 
2067
    */
 
2068
   ASSERT(sk->compat_sk_socket ?
 
2069
             sk->compat_sk_socket->type == SOCK_STREAM :
 
2070
             1);
 
2071
 
 
2072
   vsk = vsock_sk(sk);
 
2073
 
 
2074
   if (!VSockAddr_Bound(&vsk->localAddr)) {
 
2075
      return -EINVAL;
 
2076
   }
 
2077
 
 
2078
   if (!VSockAddr_Bound(&vsk->remoteAddr)) {
 
2079
      return -EINVAL;
 
2080
   }
 
2081
 
 
2082
   pkt = kmalloc(sizeof *pkt, GFP_KERNEL);
 
2083
   if (!pkt) {
 
2084
      return -ENOMEM;
 
2085
   }
 
2086
 
 
2087
   VSockPacket_Init(pkt, &vsk->localAddr, &vsk->remoteAddr,
 
2088
                    type, size, mode, wait, handle);
 
2089
 
 
2090
   LOG_PACKET(pkt);
 
2091
   err = VMCIDatagram_Send(&pkt->dg);
 
2092
   kfree(pkt);
 
2093
   if (err < 0) {
 
2094
      return VSockVmci_ErrorToVSockError(err);
 
2095
   }
 
2096
 
 
2097
#ifdef VSOCK_CONTROL_PACKET_COUNT
 
2098
   controlPacketCount[pkt->type]++;
 
2099
#endif
 
2100
 
 
2101
   return err;
 
2102
}
 
2103
#endif
 
2104
 
 
2105
 
 
2106
/*
 
2107
 *----------------------------------------------------------------------------
 
2108
 *
 
2109
 * __VSockVmciBind --
 
2110
 *
 
2111
 *    Common functionality needed to bind the specified address to the
 
2112
 *    VSocket.  If VMADDR_CID_ANY or VMADDR_PORT_ANY are specified, the context
 
2113
 *    ID or port are selected automatically.
 
2114
 *
 
2115
 * Results:
 
2116
 *    Zero on success, negative error code on failure.
 
2117
 *
 
2118
 * Side effects:
 
2119
 *    On success, a new datagram handle is created.
 
2120
 *
 
2121
 *----------------------------------------------------------------------------
 
2122
 */
 
2123
 
 
2124
static int
 
2125
__VSockVmciBind(struct sock *sk,          // IN/OUT
 
2126
                struct sockaddr_vm *addr) // IN
 
2127
{
 
2128
   static unsigned int port = LAST_RESERVED_PORT + 1;
 
2129
   struct sockaddr_vm newAddr;
 
2130
   VSockVmciSock *vsk;
 
2131
   VMCIId cid;
 
2132
   int err;
 
2133
 
 
2134
   ASSERT(sk);
 
2135
   ASSERT(sk->compat_sk_socket);
 
2136
   ASSERT(addr);
 
2137
 
 
2138
   vsk = vsock_sk(sk);
 
2139
 
 
2140
   /* First ensure this socket isn't already bound. */
 
2141
   if (VSockAddr_Bound(&vsk->localAddr)) {
 
2142
      return -EINVAL;
 
2143
   }
 
2144
 
 
2145
   /*
 
2146
    * Now bind to the provided address or select appropriate values if none are
 
2147
    * provided (VMADDR_CID_ANY and VMADDR_PORT_ANY).  Note that like AF_INET
 
2148
    * prevents binding to a non-local IP address (in most cases), we only allow
 
2149
    * binding to the local CID.
 
2150
    */
 
2151
   VSockAddr_Init(&newAddr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
 
2152
 
 
2153
   cid = VMCI_GetContextID();
 
2154
   if (addr->svm_cid != cid &&
 
2155
       addr->svm_cid != VMADDR_CID_ANY) {
 
2156
      return -EADDRNOTAVAIL;
 
2157
   }
 
2158
 
 
2159
   newAddr.svm_cid = cid;
 
2160
 
 
2161
   switch (sk->compat_sk_socket->type) {
 
2162
   case SOCK_STREAM:
 
2163
      spin_lock_bh(&vsockTableLock);
 
2164
 
 
2165
      if (addr->svm_port == VMADDR_PORT_ANY) {
 
2166
         Bool found = FALSE;
 
2167
         unsigned int i;
 
2168
 
 
2169
         for (i = 0; i < MAX_PORT_RETRIES; i++) {
 
2170
            if (port <= LAST_RESERVED_PORT) {
 
2171
               port = LAST_RESERVED_PORT + 1;
 
2172
            }
 
2173
 
 
2174
            newAddr.svm_port = port++;
 
2175
 
 
2176
            if (!__VSockVmciFindBoundSocket(&newAddr)) {
 
2177
               found = TRUE;
 
2178
               break;
 
2179
            }
 
2180
         }
 
2181
 
 
2182
         if (!found) {
 
2183
            err = -EADDRNOTAVAIL;
 
2184
            goto out;
 
2185
         }
 
2186
      } else {
 
2187
         /* If port is in reserved range, ensure caller has necessary privileges. */
 
2188
         if (addr->svm_port <= LAST_RESERVED_PORT &&
 
2189
             !capable(CAP_NET_BIND_SERVICE)) {
 
2190
            err = -EACCES;
 
2191
            goto out;
 
2192
         }
 
2193
 
 
2194
         newAddr.svm_port = addr->svm_port;
 
2195
         if (__VSockVmciFindBoundSocket(&newAddr)) {
 
2196
            err = -EADDRINUSE;
 
2197
            goto out;
 
2198
         }
 
2199
 
 
2200
      }
 
2201
      break;
 
2202
   case SOCK_DGRAM:
 
2203
      /* VMCI will select a resource ID for us if we provide VMCI_INVALID_ID. */
 
2204
      newAddr.svm_port = addr->svm_port == VMADDR_PORT_ANY ?
 
2205
                            VMCI_INVALID_ID :
 
2206
                            addr->svm_port;
 
2207
 
 
2208
      if (newAddr.svm_port <= LAST_RESERVED_PORT &&
 
2209
          !capable(CAP_NET_BIND_SERVICE)) {
 
2210
         err = -EACCES;
 
2211
         goto out;
 
2212
      }
 
2213
 
 
2214
      err = VMCIDatagram_CreateHnd(newAddr.svm_port, 0,
 
2215
                                   VSockVmciRecvDgramCB, sk,
 
2216
                                   &vsk->dgHandle);
 
2217
      if (err != VMCI_SUCCESS ||
 
2218
          vsk->dgHandle.context == VMCI_INVALID_ID ||
 
2219
          vsk->dgHandle.resource == VMCI_INVALID_ID) {
 
2220
         err = VSockVmci_ErrorToVSockError(err);
 
2221
         goto out;
 
2222
      }
 
2223
 
 
2224
      newAddr.svm_port = VMCI_HANDLE_TO_RESOURCE_ID(vsk->dgHandle);
 
2225
      break;
 
2226
   default:
 
2227
      err = -EINVAL;
 
2228
      goto out;
 
2229
   }
 
2230
 
 
2231
   VSockAddr_Init(&vsk->localAddr, newAddr.svm_cid, newAddr.svm_port);
 
2232
 
 
2233
   /*
 
2234
    * Remove stream sockets from the unbound list and add them to the hash
 
2235
    * table for easy lookup by its address.  The unbound list is simply an
 
2236
    * extra entry at the end of the hash table, a trick used by AF_UNIX.
 
2237
    */
 
2238
   if (sk->compat_sk_socket->type == SOCK_STREAM) {
 
2239
      __VSockVmciRemoveBound(sk);
 
2240
      __VSockVmciInsertBound(vsockBoundSockets(&vsk->localAddr), sk);
 
2241
   }
 
2242
 
 
2243
   err = 0;
 
2244
 
 
2245
out:
 
2246
   if (sk->compat_sk_socket->type == SOCK_STREAM) {
 
2247
      spin_unlock_bh(&vsockTableLock);
 
2248
   }
 
2249
   return err;
 
2250
}
 
2251
 
 
2252
 
 
2253
#ifdef VMX86_TOOLS
 
2254
/*
 
2255
 *----------------------------------------------------------------------------
 
2256
 *
 
2257
 * VSockVmciSendWaitingWrite --
 
2258
 *
 
2259
 *      Sends a waiting write notification to this socket's peer.
 
2260
 *
 
2261
 * Results:
 
2262
 *      TRUE if the datagram is sent successfully, FALSE otherwise.
 
2263
 *
 
2264
 * Side effects:
 
2265
 *      Our peer will notify us when there is room to write in to our produce
 
2266
 *      queue.
 
2267
 *
 
2268
 *----------------------------------------------------------------------------
 
2269
 */
 
2270
 
 
2271
 
 
2272
static Bool
 
2273
VSockVmciSendWaitingWrite(struct sock *sk,   // IN
 
2274
                          uint64 roomNeeded) // IN
 
2275
{
 
2276
#ifdef VSOCK_OPTIMIZATION_WAITING_NOTIFY
 
2277
   VSockVmciSock *vsk;
 
2278
   VSockWaitingInfo waitingInfo;
 
2279
   uint64 tail;
 
2280
   uint64 head;
 
2281
   uint64 roomLeft;
 
2282
 
 
2283
   ASSERT(sk);
 
2284
 
 
2285
   vsk = vsock_sk(sk);
 
2286
 
 
2287
   VMCIQueue_GetPointers(vsk->produceQ, vsk->consumeQ, &tail, &head);
 
2288
   roomLeft = vsk->produceSize - tail;
 
2289
   if (roomNeeded + 1 >= roomLeft) {
 
2290
      /* Wraps around to current generation. */
 
2291
      waitingInfo.offset = roomNeeded + 1 - roomLeft;
 
2292
      waitingInfo.generation = vsk->produceQGeneration;
 
2293
   } else {
 
2294
      waitingInfo.offset = tail + roomNeeded + 1;
 
2295
      waitingInfo.generation = vsk->produceQGeneration - 1;
 
2296
   }
 
2297
 
 
2298
   return VSOCK_SEND_WAITING_WRITE(sk, &waitingInfo) > 0;
 
2299
#else
 
2300
   return TRUE;
 
2301
#endif
 
2302
}
 
2303
 
 
2304
 
 
2305
/*
 
2306
 *----------------------------------------------------------------------------
 
2307
 *
 
2308
 * VSockVmciSendWaitingRead --
 
2309
 *
 
2310
 *      Sends a waiting read notification to this socket's peer.
 
2311
 *
 
2312
 * Results:
 
2313
 *      TRUE if the datagram is sent successfully, FALSE otherwise.
 
2314
 *
 
2315
 * Side effects:
 
2316
 *      Our peer will notify us when there is data to read from our consume
 
2317
 *      queue.
 
2318
 *
 
2319
 *----------------------------------------------------------------------------
 
2320
 */
 
2321
 
 
2322
static Bool
 
2323
VSockVmciSendWaitingRead(struct sock *sk,    // IN
 
2324
                         uint64 roomNeeded)  // IN
 
2325
{
 
2326
#ifdef VSOCK_OPTIMIZATION_WAITING_NOTIFY
 
2327
   VSockVmciSock *vsk;
 
2328
   VSockWaitingInfo waitingInfo;
 
2329
   uint64 tail;
 
2330
   uint64 head;
 
2331
   uint64 roomLeft;
 
2332
 
 
2333
   ASSERT(sk);
 
2334
 
 
2335
   vsk = vsock_sk(sk);
 
2336
 
 
2337
   VMCIQueue_GetPointers(vsk->consumeQ, vsk->produceQ, &tail, &head);
 
2338
   roomLeft = vsk->consumeSize - head;
 
2339
   if (roomNeeded >= roomLeft) {
 
2340
      waitingInfo.offset = roomNeeded - roomLeft;
 
2341
      waitingInfo.generation = vsk->consumeQGeneration + 1;
 
2342
   } else {
 
2343
      waitingInfo.offset = head + roomNeeded;
 
2344
      waitingInfo.generation = vsk->consumeQGeneration;
 
2345
   }
 
2346
 
 
2347
   return VSOCK_SEND_WAITING_READ(sk, &waitingInfo) > 0;
 
2348
#else
 
2349
   return TRUE;
 
2350
#endif
 
2351
}
 
2352
#endif
 
2353
 
 
2354
 
 
2355
/*
 
2356
 *----------------------------------------------------------------------------
 
2357
 *
 
2358
 * __VSockVmciCreate --
 
2359
 *
 
2360
 *    Does the work to create the sock structure.
 
2361
 *
 
2362
 * Results:
 
2363
 *    sock structure on success, NULL on failure.
 
2364
 *
 
2365
 * Side effects:
 
2366
 *    Allocated sk is added to the unbound sockets list iff it is owned by
 
2367
 *    a struct socket.
 
2368
 *
 
2369
 *----------------------------------------------------------------------------
 
2370
 */
 
2371
 
 
2372
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 14)
 
2373
static struct sock *
 
2374
__VSockVmciCreate(struct socket *sock,   // IN: Owning socket, may be NULL
 
2375
                  unsigned int priority) // IN: Allocation flags
 
2376
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
 
2377
static struct sock *
 
2378
__VSockVmciCreate(struct socket *sock,   // IN: Owning socket, may be NULL
 
2379
                  gfp_t priority)        // IN: Allocation flags
 
2380
#else
 
2381
static struct sock *
 
2382
__VSockVmciCreate(struct net *net,       // IN: Network namespace
 
2383
                  struct socket *sock,   // IN: Owning socket, may be NULL
 
2384
                  gfp_t priority)        // IN: Allocation flags
 
2385
#endif
 
2386
{
 
2387
   struct sock *sk;
 
2388
   VSockVmciSock *vsk;
 
2389
 
 
2390
   vsk = NULL;
 
2391
 
 
2392
   /*
 
2393
    * Before 2.5.5, sk_alloc() always used its own cache and protocol-specific
 
2394
    * data was contained in the protinfo union.  We cannot use those other
 
2395
    * structures so we allocate our own structure and attach it to the
 
2396
    * user_data pointer that we don't otherwise need.  We must be sure to free
 
2397
    * it later in our destruct routine.
 
2398
    *
 
2399
    * From 2.5.5 until 2.6.8, sk_alloc() offerred to use a cache that the
 
2400
    * caller provided.  After this, the cache was moved into the proto
 
2401
    * structure, but you still had to specify the size and cache yourself until
 
2402
    * 2.6.12. Most recently (in 2.6.24), sk_alloc() was changed to expect the
 
2403
    * network namespace, and the option to zero the sock was dropped.
 
2404
    *
 
2405
    */
 
2406
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 5)
 
2407
   sk = sk_alloc(vsockVmciFamilyOps.family, priority, 1);
 
2408
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 9)
 
2409
   sk = sk_alloc(vsockVmciFamilyOps.family, priority,
 
2410
                 sizeof (VSockVmciSock), vsockCachep);
 
2411
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 12)
 
2412
   sk = sk_alloc(vsockVmciFamilyOps.family, priority,
 
2413
                 vsockVmciProto.slab_obj_size, vsockVmciProto.slab);
 
2414
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
 
2415
   sk = sk_alloc(vsockVmciFamilyOps.family, priority, &vsockVmciProto, 1);
 
2416
#else
 
2417
   sk = sk_alloc(net, vsockVmciFamilyOps.family, priority, &vsockVmciProto);
 
2418
#endif
 
2419
   if (!sk) {
 
2420
      return NULL;
 
2421
   }
 
2422
 
 
2423
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 5)
 
2424
   vsock_sk(sk) = kmalloc(sizeof *vsk, priority);
 
2425
   if (!vsock_sk(sk)) {
 
2426
      sk_free(sk);
 
2427
      return NULL;
 
2428
   }
 
2429
   sk_vsock(vsock_sk(sk)) = sk;
 
2430
#endif
 
2431
 
 
2432
   /*
 
2433
    * If we go this far, we know the socket family is registered, so there's no
 
2434
    * need to register it now.
 
2435
    */
 
2436
   spin_lock(&registrationLock);
 
2437
   vsockVmciSocketCount++;
 
2438
   spin_unlock(&registrationLock);
 
2439
 
 
2440
   sock_init_data(sock, sk);
 
2441
 
 
2442
   vsk = vsock_sk(sk);
 
2443
   VSockAddr_Init(&vsk->localAddr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
 
2444
   VSockAddr_Init(&vsk->remoteAddr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
 
2445
 
 
2446
   sk->compat_sk_destruct = VSockVmciSkDestruct;
 
2447
   sk->compat_sk_backlog_rcv = VSockVmciQueueRcvSkb;
 
2448
   sk->compat_sk_state = SS_UNCONNECTED;
 
2449
 
 
2450
   INIT_LIST_HEAD(&vsk->boundTable);
 
2451
   INIT_LIST_HEAD(&vsk->connectedTable);
 
2452
   vsk->dgHandle = VMCI_INVALID_HANDLE;
 
2453
#ifdef VMX86_TOOLS
 
2454
   vsk->qpHandle = VMCI_INVALID_HANDLE;
 
2455
   vsk->produceQ = vsk->consumeQ = NULL;
 
2456
   vsk->produceQGeneration = vsk->consumeQGeneration = 0;
 
2457
   vsk->produceSize = vsk->consumeSize = 0;
 
2458
   vsk->queuePairSize = VSOCK_DEFAULT_QP_SIZE;
 
2459
   vsk->queuePairMinSize = VSOCK_DEFAULT_QP_SIZE_MIN;
 
2460
   vsk->queuePairMaxSize = VSOCK_DEFAULT_QP_SIZE_MAX;
 
2461
   vsk->peerWaitingRead = vsk->peerWaitingWrite = FALSE;
 
2462
   memset(&vsk->peerWaitingReadInfo, 0, sizeof vsk->peerWaitingReadInfo);
 
2463
   memset(&vsk->peerWaitingWriteInfo, 0, sizeof vsk->peerWaitingWriteInfo);
 
2464
   vsk->listener = NULL;
 
2465
   INIT_LIST_HEAD(&vsk->pendingLinks);
 
2466
   INIT_LIST_HEAD(&vsk->acceptQueue);
 
2467
   vsk->rejected = FALSE;
 
2468
   vsk->attachSubId = vsk->detachSubId = VMCI_INVALID_ID;
 
2469
   vsk->peerShutdown = 0;
 
2470
#endif
 
2471
 
 
2472
   if (sock) {
 
2473
      VSockVmciInsertBound(vsockUnboundSockets, sk);
 
2474
   }
 
2475
 
 
2476
   return sk;
 
2477
}
 
2478
 
 
2479
 
 
2480
/*
 
2481
 *----------------------------------------------------------------------------
 
2482
 *
 
2483
 * __VSockVmciRelease --
 
2484
 *
 
2485
 *      Releases the provided socket.
 
2486
 *
 
2487
 * Results:
 
2488
 *      None.
 
2489
 *
 
2490
 * Side effects:
 
2491
 *      Any pending sockets are also released.
 
2492
 *
 
2493
 *----------------------------------------------------------------------------
 
2494
 */
 
2495
 
 
2496
static void
 
2497
__VSockVmciRelease(struct sock *sk) // IN
 
2498
{
 
2499
   if (sk) {
 
2500
      struct sk_buff *skb;
 
2501
      struct sock *pending;
 
2502
      struct VSockVmciSock *vsk;
 
2503
 
 
2504
      vsk = vsock_sk(sk);
 
2505
      pending = NULL;  /* Compiler warning. */
 
2506
 
 
2507
      if (VSockVmciInBoundTable(sk)) {
 
2508
         VSockVmciRemoveBound(sk);
 
2509
      }
 
2510
 
 
2511
      if (VSockVmciInConnectedTable(sk)) {
 
2512
         VSockVmciRemoveConnected(sk);
 
2513
      }
 
2514
 
 
2515
      if (!VMCI_HANDLE_INVALID(vsk->dgHandle)) {
 
2516
         VMCIDatagram_DestroyHnd(vsk->dgHandle);
 
2517
         vsk->dgHandle = VMCI_INVALID_HANDLE;
 
2518
      }
 
2519
 
 
2520
      lock_sock(sk);
 
2521
      sock_orphan(sk);
 
2522
      sk->compat_sk_shutdown = SHUTDOWN_MASK;
 
2523
 
 
2524
      while ((skb = skb_dequeue(&sk->compat_sk_receive_queue))) {
 
2525
         kfree_skb(skb);
 
2526
      }
 
2527
 
 
2528
      /* Clean up any sockets that never were accepted. */
 
2529
#ifdef VMX86_TOOLS
 
2530
      while ((pending = VSockVmciDequeueAccept(sk)) != NULL) {
 
2531
         __VSockVmciRelease(pending);
 
2532
         sock_put(pending);
 
2533
      }
 
2534
#endif
 
2535
 
 
2536
      release_sock(sk);
 
2537
      sock_put(sk);
 
2538
   }
 
2539
}
 
2540
 
 
2541
 
 
2542
/*
 
2543
 * Sock operations.
 
2544
 */
 
2545
 
 
2546
/*
 
2547
 *----------------------------------------------------------------------------
 
2548
 *
 
2549
 * VSockVmciSkDestruct --
 
2550
 *
 
2551
 *    Destroys the provided socket.  This is called by sk_free(), which is
 
2552
 *    invoked when the reference count of the socket drops to zero.
 
2553
 *
 
2554
 * Results:
 
2555
 *    None.
 
2556
 *
 
2557
 * Side effects:
 
2558
 *    Socket count is decremented.
 
2559
 *
 
2560
 *----------------------------------------------------------------------------
 
2561
 */
 
2562
 
 
2563
static void
 
2564
VSockVmciSkDestruct(struct sock *sk) // IN
 
2565
{
 
2566
   VSockVmciSock *vsk;
 
2567
 
 
2568
   vsk = vsock_sk(sk);
 
2569
 
 
2570
#ifdef VMX86_TOOLS
 
2571
   if (vsk->attachSubId != VMCI_INVALID_ID) {
 
2572
      VMCIEvent_Unsubscribe(vsk->attachSubId);
 
2573
      vsk->attachSubId = VMCI_INVALID_ID;
 
2574
   }
 
2575
 
 
2576
   if (vsk->detachSubId != VMCI_INVALID_ID) {
 
2577
      VMCIEvent_Unsubscribe(vsk->detachSubId);
 
2578
      vsk->detachSubId = VMCI_INVALID_ID;
 
2579
   }
 
2580
 
 
2581
   if (!VMCI_HANDLE_INVALID(vsk->qpHandle)) {
 
2582
      VMCIQueuePair_Detach(vsk->qpHandle);
 
2583
      vsk->qpHandle = VMCI_INVALID_HANDLE;
 
2584
      vsk->produceQ = vsk->consumeQ = NULL;
 
2585
      vsk->produceSize = vsk->consumeSize = 0;
 
2586
   }
 
2587
#endif
 
2588
 
 
2589
   /*
 
2590
    * Each list entry holds a reference on the socket, so we should not even be
 
2591
    * here if the socket is in one of our lists.  If we are we have a stray
 
2592
    * sock_put() that needs to go away.
 
2593
    */
 
2594
   ASSERT(!VSockVmciInBoundTable(sk));
 
2595
   ASSERT(!VSockVmciInConnectedTable(sk));
 
2596
#ifdef VMX86_TOOLS
 
2597
   ASSERT(!VSockVmciIsPending(sk));
 
2598
   ASSERT(!VSockVmciInAcceptQueue(sk));
 
2599
#endif
 
2600
 
 
2601
   /*
 
2602
    * When clearing these addresses, there's no need to set the family and
 
2603
    * possibly register the address family with the kernel.
 
2604
    */
 
2605
   VSockAddr_InitNoFamily(&vsk->localAddr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
 
2606
   VSockAddr_InitNoFamily(&vsk->remoteAddr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
 
2607
 
 
2608
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 5)
 
2609
   ASSERT(vsock_sk(sk) == vsk);
 
2610
   kfree(vsock_sk(sk));
 
2611
#endif
 
2612
 
 
2613
   spin_lock(&registrationLock);
 
2614
   vsockVmciSocketCount--;
 
2615
   VSockVmciTestUnregister();
 
2616
   spin_unlock(&registrationLock);
 
2617
 
 
2618
#ifdef VSOCK_CONTROL_PACKET_COUNT
 
2619
   {
 
2620
      uint32 index;
 
2621
      for (index = 0; index < ARRAYSIZE(controlPacketCount); index++) {
 
2622
         Warning("Control packet count: Type = %u, Count = %"FMT64"u\n",
 
2623
                 index, controlPacketCount[index]);
 
2624
      }
 
2625
   }
 
2626
#endif
 
2627
}
 
2628
 
 
2629
 
 
2630
/*
 
2631
 *----------------------------------------------------------------------------
 
2632
 *
 
2633
 * VSockVmciQueueRcvSkb --
 
2634
 *
 
2635
 *    Receives skb on the socket's receive queue.
 
2636
 *
 
2637
 * Results:
 
2638
 *    Zero on success, negative error code on failure.
 
2639
 *
 
2640
 * Side effects:
 
2641
 *    None.
 
2642
 *
 
2643
 *----------------------------------------------------------------------------
 
2644
 */
 
2645
 
 
2646
static int
 
2647
VSockVmciQueueRcvSkb(struct sock *sk,     // IN
 
2648
                     struct sk_buff *skb) // IN
 
2649
{
 
2650
   int err;
 
2651
 
 
2652
   err = sock_queue_rcv_skb(sk, skb);
 
2653
   if (err) {
 
2654
      kfree_skb(skb);
 
2655
   }
 
2656
 
 
2657
   return err;
 
2658
}
 
2659
 
 
2660
 
 
2661
/*
 
2662
 *----------------------------------------------------------------------------
 
2663
 *
 
2664
 * VSockVmciRegisterProto --
 
2665
 *
 
2666
 *      Registers the vmci sockets protocol family.
 
2667
 *
 
2668
 * Results:
 
2669
 *      Zero on success, error code on failure.
 
2670
 *
 
2671
 * Side effects:
 
2672
 *      None.
 
2673
 *
 
2674
 *----------------------------------------------------------------------------
 
2675
 */
 
2676
 
 
2677
static INLINE int
 
2678
VSockVmciRegisterProto(void)
 
2679
{
 
2680
   int err;
 
2681
 
 
2682
   err = 0;
 
2683
 
 
2684
   /*
 
2685
    * Before 2.6.9, each address family created their own slab (by calling
 
2686
    * kmem_cache_create() directly).  From 2.6.9 until 2.6.11, these address
 
2687
    * families instead called sk_alloc_slab() and the allocated slab was
 
2688
    * assigned to the slab variable in the proto struct and was created of size
 
2689
    * slab_obj_size.  As of 2.6.12 and later, this slab allocation was moved
 
2690
    * into proto_register() and only done if you specified a non-zero value for
 
2691
    * the second argument (alloc_slab); the size of the slab element was
 
2692
    * changed to obj_size.
 
2693
    */
 
2694
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 5)
 
2695
   /* Simply here for clarity and so else case at end implies > rest. */
 
2696
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 9)
 
2697
   vsockCachep = kmem_cache_create("vsock", sizeof (VSockVmciSock),
 
2698
                                   0, SLAB_HWCACHE_ALIGN, NULL, NULL);
 
2699
   if (!vsockCachep) {
 
2700
      err = -ENOMEM;
 
2701
   }
 
2702
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 12)
 
2703
   err = sk_alloc_slab(&vsockVmciProto, "vsock");
 
2704
   if (err != 0) {
 
2705
      sk_alloc_slab_error(&vsockVmciProto);
 
2706
   }
 
2707
#else
 
2708
   /* Specify 1 as the second argument so the slab is created for us. */
 
2709
   err = proto_register(&vsockVmciProto, 1);
 
2710
#endif
 
2711
 
 
2712
   return err;
 
2713
}
 
2714
 
 
2715
 
 
2716
/*
 
2717
 *----------------------------------------------------------------------------
 
2718
 *
 
2719
 * VSockVmciUnregisterProto --
 
2720
 *
 
2721
 *      Unregisters the vmci sockets protocol family.
 
2722
 *
 
2723
 * Results:
 
2724
 *      None.
 
2725
 *
 
2726
 * Side effects:
 
2727
 *      None.
 
2728
 *
 
2729
 *----------------------------------------------------------------------------
 
2730
 */
 
2731
 
 
2732
static INLINE void
 
2733
VSockVmciUnregisterProto(void)
 
2734
{
 
2735
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 5)
 
2736
   /* Simply here for clarity and so else case at end implies > rest. */
 
2737
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 9)
 
2738
   kmem_cache_destroy(vsockCachep);
 
2739
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 12)
 
2740
   sk_free_slab(&vsockVmciProto);
 
2741
#else
 
2742
   proto_unregister(&vsockVmciProto);
 
2743
#endif
 
2744
 
 
2745
#ifdef VSOCK_CONTROL_PACKET_COUNT
 
2746
   {
 
2747
      uint32 index;
 
2748
      for (index = 0; index < ARRAYSIZE(controlPacketCount); index++) {
 
2749
         controlPacketCount[index] = 0;
 
2750
      }
 
2751
   }
 
2752
#endif
 
2753
}
 
2754
 
 
2755
 
 
2756
/*
 
2757
 *----------------------------------------------------------------------------
 
2758
 *
 
2759
 * VSockVmciRegisterAddressFamily --
 
2760
 *
 
2761
 *      Registers our socket address family with the kernel.
 
2762
 *
 
2763
 *      Note that this assumes the registration lock is held.
 
2764
 *
 
2765
 * Results:
 
2766
 *      The address family value on success, negative error code on failure.
 
2767
 *
 
2768
 * Side effects:
 
2769
 *      Callers of socket operations with the returned value, on success, will
 
2770
 *      be able to use our socket implementation.
 
2771
 *
 
2772
 *----------------------------------------------------------------------------
 
2773
 */
 
2774
 
 
2775
static int
 
2776
VSockVmciRegisterAddressFamily(void)
 
2777
{
 
2778
   int err = 0;
 
2779
   int i;
 
2780
 
 
2781
#ifdef VMX86_TOOLS
 
2782
   /*
 
2783
    * We don't call into the vmci module or register our socket family if the
 
2784
    * vmci device isn't present.
 
2785
    */
 
2786
   vmciDevicePresent = VMCI_DeviceGet();
 
2787
   if (!vmciDevicePresent) {
 
2788
      Log("Could not register VMCI Sockets because VMCI device is not present.\n");
 
2789
      return -1;
 
2790
   }
 
2791
 
 
2792
   /*
 
2793
    * Create the datagram handle that we will use to send and receive all
 
2794
    * VSocket control messages for this context.
 
2795
    */
 
2796
   err = VMCIDatagram_CreateHnd(VSOCK_PACKET_RID, 0,
 
2797
                                VSockVmciRecvStreamCB, NULL, &vmciStreamHandle);
 
2798
   if (err != VMCI_SUCCESS ||
 
2799
       vmciStreamHandle.context == VMCI_INVALID_ID ||
 
2800
       vmciStreamHandle.resource == VMCI_INVALID_ID) {
 
2801
      Warning("Unable to create datagram handle. (%d)\n", err);
 
2802
      return -ENOMEM;
 
2803
   }
 
2804
 
 
2805
   err = VMCIEvent_Subscribe(VMCI_EVENT_QP_RESUMED,
 
2806
                             VSockVmciQPResumedCB,
 
2807
                             NULL,
 
2808
                             &qpResumedSubId);
 
2809
   if (err < VMCI_SUCCESS) {
 
2810
      Warning("Unable to subscribe to QP resumed event. (%d)\n", err);
 
2811
      err = -ENOMEM;
 
2812
      qpResumedSubId = VMCI_INVALID_ID;
 
2813
      goto error;
 
2814
   }
 
2815
#endif
 
2816
 
 
2817
   /*
 
2818
    * Linux will not allocate an address family to code that is not part of the
 
2819
    * kernel proper, so until that time comes we need a workaround.  Here we
 
2820
    * loop through the allowed values and claim the first one that's not
 
2821
    * currently used.  Users will then make an ioctl(2) into our module to
 
2822
    * retrieve this value before calling socket(2).
 
2823
    *
 
2824
    * This is undesirable, but it's better than having users' programs break
 
2825
    * when a hard-coded, currently-available value gets assigned to someone
 
2826
    * else in the future.
 
2827
    */
 
2828
   for (i = NPROTO - 1; i >= 0; i--) {
 
2829
      vsockVmciFamilyOps.family = i;
 
2830
      err = sock_register(&vsockVmciFamilyOps);
 
2831
      if (err) {
 
2832
         Warning("Could not register address family %d.\n", i);
 
2833
         vsockVmciFamilyOps.family = VSOCK_INVALID_FAMILY;
 
2834
      } else {
 
2835
         vsockVmciDgramOps.family = i;
 
2836
#ifdef VMX86_TOOLS
 
2837
         vsockVmciStreamOps.family = i;
 
2838
#endif
 
2839
         break;
 
2840
      }
 
2841
   }
 
2842
 
 
2843
   if (err) {
 
2844
      goto error;
 
2845
   }
 
2846
 
 
2847
   return vsockVmciFamilyOps.family;
 
2848
 
 
2849
error:
 
2850
#ifdef VMX86_TOOLS
 
2851
   if (qpResumedSubId != VMCI_INVALID_ID) {
 
2852
      VMCIEvent_Unsubscribe(qpResumedSubId);
 
2853
      qpResumedSubId = VMCI_INVALID_ID;
 
2854
   }
 
2855
   VMCIDatagram_DestroyHnd(vmciStreamHandle);
 
2856
#endif
 
2857
   return err;
 
2858
}
 
2859
 
 
2860
 
 
2861
/*
 
2862
 *----------------------------------------------------------------------------
 
2863
 *
 
2864
 * VSockVmciUnregisterAddressFamily --
 
2865
 *
 
2866
 *      Unregisters the address family with the kernel.
 
2867
 *
 
2868
 *      Note that this assumes the registration lock is held.
 
2869
 *
 
2870
 * Results:
 
2871
 *      None.
 
2872
 *
 
2873
 * Side effects:
 
2874
 *      Our socket implementation is no longer accessible.
 
2875
 *
 
2876
 *----------------------------------------------------------------------------
 
2877
 */
 
2878
 
 
2879
static void
 
2880
VSockVmciUnregisterAddressFamily(void)
 
2881
{
 
2882
#ifdef VMX86_TOOLS
 
2883
   if (!vmciDevicePresent) {
 
2884
      /* Nothing was registered. */
 
2885
      return;
 
2886
   }
 
2887
 
 
2888
   if (!VMCI_HANDLE_INVALID(vmciStreamHandle)) {
 
2889
      if (VMCIDatagram_DestroyHnd(vmciStreamHandle) != VMCI_SUCCESS) {
 
2890
         Warning("Could not destroy VMCI datagram handle.\n");
 
2891
      }
 
2892
   }
 
2893
 
 
2894
   if (qpResumedSubId != VMCI_INVALID_ID) {
 
2895
      VMCIEvent_Unsubscribe(qpResumedSubId);
 
2896
      qpResumedSubId = VMCI_INVALID_ID;
 
2897
   }
 
2898
#endif
 
2899
 
 
2900
   if (vsockVmciFamilyOps.family != VSOCK_INVALID_FAMILY) {
 
2901
      sock_unregister(vsockVmciFamilyOps.family);
 
2902
   }
 
2903
 
 
2904
   vsockVmciDgramOps.family = vsockVmciFamilyOps.family = VSOCK_INVALID_FAMILY;
 
2905
#ifdef VMX86_TOOLS
 
2906
   vsockVmciStreamOps.family = vsockVmciFamilyOps.family;
 
2907
#endif
 
2908
 
 
2909
}
 
2910
 
 
2911
 
 
2912
/*
 
2913
 * Socket operations.
 
2914
 */
 
2915
 
 
2916
/*
 
2917
 *----------------------------------------------------------------------------
 
2918
 *
 
2919
 * VSockVmciRelease --
 
2920
 *
 
2921
 *    Releases the provided socket by freeing the contents of its queue.  This
 
2922
 *    is called when a user process calls close(2) on the socket.
 
2923
 *
 
2924
 * Results:
 
2925
 *    Zero on success, negative error code on failure.
 
2926
 *
 
2927
 * Side effects:
 
2928
 *    None.
 
2929
 *
 
2930
 *----------------------------------------------------------------------------
 
2931
 */
 
2932
 
 
2933
static int
 
2934
VSockVmciRelease(struct socket *sock) // IN
 
2935
{
 
2936
   __VSockVmciRelease(sock->sk);
 
2937
   sock->sk = NULL;
 
2938
   sock->state = SS_FREE;
 
2939
 
 
2940
   return 0;
 
2941
}
 
2942
 
 
2943
 
 
2944
/*
 
2945
 *----------------------------------------------------------------------------
 
2946
 *
 
2947
 * VSockVmciBind --
 
2948
 *
 
2949
 *    Binds the provided address to the provided socket.
 
2950
 *
 
2951
 * Results:
 
2952
 *    Zero on success, negative error code on failure.
 
2953
 *
 
2954
 * Side effects:
 
2955
 *    None.
 
2956
 *
 
2957
 *----------------------------------------------------------------------------
 
2958
 */
 
2959
 
 
2960
static int
 
2961
VSockVmciBind(struct socket *sock,    // IN
 
2962
              struct sockaddr *addr,  // IN
 
2963
              int addrLen)            // IN
 
2964
{
 
2965
   int err;
 
2966
   struct sock *sk;
 
2967
   struct sockaddr_vm *vmciAddr;
 
2968
 
 
2969
   sk = sock->sk;
 
2970
 
 
2971
   if (VSockAddr_Cast(addr, addrLen, &vmciAddr) != 0) {
 
2972
      return -EINVAL;
 
2973
   }
 
2974
 
 
2975
   lock_sock(sk);
 
2976
   err = __VSockVmciBind(sk, vmciAddr);
 
2977
   release_sock(sk);
 
2978
 
 
2979
   return err;
 
2980
}
 
2981
 
 
2982
 
 
2983
/*
 
2984
 *----------------------------------------------------------------------------
 
2985
 *
 
2986
 * VSockVmciDgramConnect --
 
2987
 *
 
2988
 *    Connects a datagram socket.  This can be called multiple times to change
 
2989
 *    the socket's association and can be called with a sockaddr whose family
 
2990
 *    is set to AF_UNSPEC to dissolve any existing association.
 
2991
 *
 
2992
 * Results:
 
2993
 *    Zero on success, negative error code on failure.
 
2994
 *
 
2995
 * Side effects:
 
2996
 *    None.
 
2997
 *
 
2998
 *----------------------------------------------------------------------------
 
2999
 */
 
3000
 
 
3001
static int
 
3002
VSockVmciDgramConnect(struct socket *sock,   // IN
 
3003
                      struct sockaddr *addr, // IN
 
3004
                      int addrLen,           // IN
 
3005
                      int flags)             // IN
 
3006
{
 
3007
   int err;
 
3008
   struct sock *sk;
 
3009
   VSockVmciSock *vsk;
 
3010
   struct sockaddr_vm *remoteAddr;
 
3011
 
 
3012
   sk = sock->sk;
 
3013
   vsk = vsock_sk(sk);
 
3014
 
 
3015
   err = VSockAddr_Cast(addr, addrLen, &remoteAddr);
 
3016
   if (err == -EAFNOSUPPORT && remoteAddr->svm_family == AF_UNSPEC) {
 
3017
      lock_sock(sk);
 
3018
      VSockAddr_Init(&vsk->remoteAddr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
 
3019
      sock->state = SS_UNCONNECTED;
 
3020
      release_sock(sk);
 
3021
      return 0;
 
3022
   } else if (err != 0) {
 
3023
      return -EINVAL;
 
3024
   }
 
3025
 
 
3026
   lock_sock(sk);
 
3027
 
 
3028
 
 
3029
   if (!VSockAddr_Bound(&vsk->localAddr)) {
 
3030
      struct sockaddr_vm localAddr;
 
3031
 
 
3032
      VSockAddr_Init(&localAddr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
 
3033
      if ((err = __VSockVmciBind(sk, &localAddr))) {
 
3034
         goto out;
 
3035
      }
 
3036
   }
 
3037
 
 
3038
   memcpy(&vsk->remoteAddr, remoteAddr, sizeof vsk->remoteAddr);
 
3039
   sock->state = SS_CONNECTED;
 
3040
 
 
3041
out:
 
3042
   release_sock(sk);
 
3043
   return err;
 
3044
}
 
3045
 
 
3046
 
 
3047
#ifdef VMX86_TOOLS
 
3048
/*
 
3049
 *----------------------------------------------------------------------------
 
3050
 *
 
3051
 * VSockVmciStreamConnect --
 
3052
 *
 
3053
 *    Connects a stream socket.
 
3054
 *
 
3055
 * Results:
 
3056
 *    Zero on success, negative error code on failure.
 
3057
 *
 
3058
 * Side effects:
 
3059
 *    None.
 
3060
 *
 
3061
 *----------------------------------------------------------------------------
 
3062
 */
 
3063
 
 
3064
static int
 
3065
VSockVmciStreamConnect(struct socket *sock,   // IN
 
3066
                       struct sockaddr *addr, // IN
 
3067
                       int addrLen,           // IN
 
3068
                       int flags)             // IN
 
3069
{
 
3070
   int err;
 
3071
   struct sock *sk;
 
3072
   VSockVmciSock *vsk;
 
3073
   struct sockaddr_vm *remoteAddr;
 
3074
   long timeout;
 
3075
   COMPAT_DEFINE_WAIT(wait);
 
3076
 
 
3077
   err = 0;
 
3078
   sk = sock->sk;
 
3079
   vsk = vsock_sk(sk);
 
3080
 
 
3081
   lock_sock(sk);
 
3082
 
 
3083
   /* XXX AF_UNSPEC should make us disconnect like AF_INET. */
 
3084
 
 
3085
   switch (sock->state) {
 
3086
   case SS_CONNECTED:
 
3087
      err = -EISCONN;
 
3088
      goto out;
 
3089
   case SS_DISCONNECTING:
 
3090
   case SS_LISTEN:
 
3091
      err = -EINVAL;
 
3092
      goto out;
 
3093
   case SS_CONNECTING:
 
3094
      /*
 
3095
       * This continues on so we can move sock into the SS_CONNECTED state once
 
3096
       * the connection has completed (at which point err will be set to zero
 
3097
       * also).  Otherwise, we will either wait for the connection or return
 
3098
       * -EALREADY should this be a non-blocking call.
 
3099
       */
 
3100
      err = -EALREADY;
 
3101
      break;
 
3102
   default:
 
3103
      ASSERT(sk->compat_sk_state == SS_FREE ||
 
3104
             sk->compat_sk_state == SS_UNCONNECTED);
 
3105
      if (VSockAddr_Cast(addr, addrLen, &remoteAddr) != 0) {
 
3106
         err = -EINVAL;
 
3107
         goto out;
 
3108
      }
 
3109
 
 
3110
      /* The hypervisor and well-known contexts do not have socket endpoints. */
 
3111
      if (!VSockAddr_SocketContext(remoteAddr->svm_cid)) {
 
3112
         err = -ENETUNREACH;
 
3113
         goto out;
 
3114
      }
 
3115
 
 
3116
      /* Set the remote address that we are connecting to. */
 
3117
      memcpy(&vsk->remoteAddr, remoteAddr, sizeof vsk->remoteAddr);
 
3118
 
 
3119
      /* Autobind this socket to the local address if necessary. */
 
3120
      if (!VSockAddr_Bound(&vsk->localAddr)) {
 
3121
         struct sockaddr_vm localAddr;
 
3122
 
 
3123
         VSockAddr_Init(&localAddr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
 
3124
         if ((err = __VSockVmciBind(sk, &localAddr))) {
 
3125
            goto out;
 
3126
         }
 
3127
      }
 
3128
 
 
3129
      sk->compat_sk_state = SS_CONNECTING;
 
3130
 
 
3131
      err = VSOCK_SEND_CONN_REQUEST(sk, vsk->queuePairSize);
 
3132
      if (err < 0) {
 
3133
         sk->compat_sk_state = SS_UNCONNECTED;
 
3134
         goto out;
 
3135
      }
 
3136
 
 
3137
      /*
 
3138
       * Mark sock as connecting and set the error code to in progress in case
 
3139
       * this is a non-blocking connect.
 
3140
       */
 
3141
      sock->state = SS_CONNECTING;
 
3142
      err = -EINPROGRESS;
 
3143
   }
 
3144
 
 
3145
   /*
 
3146
    * The receive path will handle all communication until we are able to enter
 
3147
    * the connected state.  Here we wait for the connection to be completed or
 
3148
    * a notification of an error.
 
3149
    */
 
3150
   timeout = sock_sndtimeo(sk, flags & O_NONBLOCK);
 
3151
   compat_init_prepare_to_wait(sk->compat_sk_sleep, &wait, TASK_INTERRUPTIBLE);
 
3152
 
 
3153
   while (sk->compat_sk_state != SS_CONNECTED && sk->compat_sk_err == 0) {
 
3154
      if (timeout == 0) {
 
3155
         /*
 
3156
          * If we're not going to block, skip ahead to preserve error code set
 
3157
          * above.
 
3158
          */
 
3159
         goto outWait;
 
3160
      }
 
3161
 
 
3162
      release_sock(sk);
 
3163
      timeout = schedule_timeout(timeout);
 
3164
      lock_sock(sk);
 
3165
 
 
3166
      if (signal_pending(current)) {
 
3167
         err = sock_intr_errno(timeout);
 
3168
         goto outWaitError;
 
3169
      } else if (timeout == 0) {
 
3170
         err = -ETIMEDOUT;
 
3171
         goto outWaitError;
 
3172
      }
 
3173
 
 
3174
      compat_cont_prepare_to_wait(sk->compat_sk_sleep, &wait, TASK_INTERRUPTIBLE);
 
3175
   }
 
3176
 
 
3177
   if (sk->compat_sk_err) {
 
3178
      err = -sk->compat_sk_err;
 
3179
      goto outWaitError;
 
3180
   } else {
 
3181
      ASSERT(sk->compat_sk_state == SS_CONNECTED);
 
3182
      err = 0;
 
3183
   }
 
3184
 
 
3185
outWait:
 
3186
   compat_finish_wait(sk->compat_sk_sleep, &wait, TASK_RUNNING);
 
3187
out:
 
3188
   release_sock(sk);
 
3189
   return err;
 
3190
 
 
3191
outWaitError:
 
3192
   sk->compat_sk_state = SS_UNCONNECTED;
 
3193
   sock->state = SS_UNCONNECTED;
 
3194
   goto outWait;
 
3195
}
 
3196
 
 
3197
 
 
3198
/*
 
3199
 *----------------------------------------------------------------------------
 
3200
 *
 
3201
 * VSockVmciAccept --
 
3202
 *
 
3203
 *      Accepts next available connection request for this socket.
 
3204
 *
 
3205
 * Results:
 
3206
 *      Zero on success, negative error code on failure.
 
3207
 *
 
3208
 * Side effects:
 
3209
 *      None.
 
3210
 *
 
3211
 *----------------------------------------------------------------------------
 
3212
 */
 
3213
 
 
3214
static int
 
3215
VSockVmciAccept(struct socket *sock,     // IN
 
3216
                struct socket *newsock,  // IN/OUT
 
3217
                int flags)               // IN
 
3218
{
 
3219
   struct sock *listener;
 
3220
   int err;
 
3221
   struct sock *connected;
 
3222
   VSockVmciSock *vconnected;
 
3223
   long timeout;
 
3224
   COMPAT_DEFINE_WAIT(wait);
 
3225
 
 
3226
   err = 0;
 
3227
   listener = sock->sk;
 
3228
 
 
3229
   lock_sock(listener);
 
3230
 
 
3231
   if (sock->type != SOCK_STREAM) {
 
3232
      err = -EOPNOTSUPP;
 
3233
      goto out;
 
3234
   }
 
3235
 
 
3236
   if (listener->compat_sk_state != SS_LISTEN) {
 
3237
      err = -EINVAL;
 
3238
      goto out;
 
3239
   }
 
3240
 
 
3241
   /*
 
3242
    * Wait for children sockets to appear; these are the new sockets created
 
3243
    * upon connection establishment.
 
3244
    */
 
3245
   timeout = sock_sndtimeo(listener, flags & O_NONBLOCK);
 
3246
   compat_init_prepare_to_wait(listener->compat_sk_sleep, &wait, TASK_INTERRUPTIBLE);
 
3247
 
 
3248
   while ((connected = VSockVmciDequeueAccept(listener)) == NULL &&
 
3249
          listener->compat_sk_err == 0) {
 
3250
      release_sock(listener);
 
3251
      timeout = schedule_timeout(timeout);
 
3252
      lock_sock(listener);
 
3253
 
 
3254
      if (signal_pending(current)) {
 
3255
         err = sock_intr_errno(timeout);
 
3256
         goto outWait;
 
3257
      } else if (timeout == 0) {
 
3258
         err = -ETIMEDOUT;
 
3259
         goto outWait;
 
3260
      }
 
3261
 
 
3262
      compat_cont_prepare_to_wait(listener->compat_sk_sleep, &wait, TASK_INTERRUPTIBLE);
 
3263
   }
 
3264
 
 
3265
   if (listener->compat_sk_err) {
 
3266
      err = -listener->compat_sk_err;
 
3267
   }
 
3268
 
 
3269
   if (connected) {
 
3270
      listener->compat_sk_ack_backlog--;
 
3271
 
 
3272
      lock_sock(connected);
 
3273
      vconnected = vsock_sk(connected);
 
3274
 
 
3275
      /*
 
3276
       * If the listener socket has received an error, then we should reject
 
3277
       * this socket and return.  Note that we simply mark the socket rejected,
 
3278
       * drop our reference, and let the cleanup function handle the cleanup;
 
3279
       * the fact that we found it in the listener's accept queue guarantees
 
3280
       * that the cleanup function hasn't run yet.
 
3281
       */
 
3282
      if (err) {
 
3283
         vconnected->rejected = TRUE;
 
3284
         release_sock(connected);
 
3285
         sock_put(connected);
 
3286
         goto outWait;
 
3287
      }
 
3288
 
 
3289
      newsock->state = SS_CONNECTED;
 
3290
      sock_graft(connected, newsock);
 
3291
      release_sock(connected);
 
3292
      sock_put(connected);
 
3293
   }
 
3294
 
 
3295
outWait:
 
3296
   compat_finish_wait(listener->compat_sk_sleep, &wait, TASK_RUNNING);
 
3297
out:
 
3298
   release_sock(listener);
 
3299
   return err;
 
3300
}
 
3301
#endif
 
3302
 
 
3303
 
 
3304
/*
 
3305
 *----------------------------------------------------------------------------
 
3306
 *
 
3307
 * VSockVmciGetname --
 
3308
 *
 
3309
 *    Provides the local or remote address for the socket.
 
3310
 *
 
3311
 * Results:
 
3312
 *    Zero on success, negative error code otherwise.
 
3313
 *
 
3314
 * Side effects:
 
3315
 *    None.
 
3316
 *
 
3317
 *----------------------------------------------------------------------------
 
3318
 */
 
3319
 
 
3320
static int
 
3321
VSockVmciGetname(struct socket *sock,    // IN
 
3322
                 struct sockaddr *addr,  // OUT
 
3323
                 int *addrLen,           // OUT
 
3324
                 int peer)               // IN
 
3325
{
 
3326
   int err;
 
3327
   struct sock *sk;
 
3328
   VSockVmciSock *vsk;
 
3329
   struct sockaddr_vm *vmciAddr;
 
3330
 
 
3331
   sk = sock->sk;
 
3332
   vsk = vsock_sk(sk);
 
3333
   err = 0;
 
3334
 
 
3335
   lock_sock(sk);
 
3336
 
 
3337
   if (peer) {
 
3338
      if (sock->state != SS_CONNECTED) {
 
3339
         err = -ENOTCONN;
 
3340
         goto out;
 
3341
      }
 
3342
      vmciAddr = &vsk->remoteAddr;
 
3343
   } else {
 
3344
      vmciAddr = &vsk->localAddr;
 
3345
   }
 
3346
 
 
3347
   if (!vmciAddr) {
 
3348
      err = -EINVAL;
 
3349
      goto out;
 
3350
   }
 
3351
 
 
3352
   /*
 
3353
    * sys_getsockname() and sys_getpeername() pass us a MAX_SOCK_ADDR-sized
 
3354
    * buffer and don't set addrLen.  Unfortunately that macro is defined in
 
3355
    * socket.c instead of .h, so we hardcode its value here.
 
3356
    */
 
3357
   ASSERT_ON_COMPILE(sizeof *vmciAddr <= 128);
 
3358
   memcpy(addr, vmciAddr, sizeof *vmciAddr);
 
3359
   *addrLen = sizeof *vmciAddr;
 
3360
 
 
3361
out:
 
3362
   release_sock(sk);
 
3363
   return err;
 
3364
}
 
3365
 
 
3366
 
 
3367
/*
 
3368
 *----------------------------------------------------------------------------
 
3369
 *
 
3370
 * VSockVmciPoll --
 
3371
 *
 
3372
 *    Waits on file for activity then provides mask indicating state of socket.
 
3373
 *
 
3374
 * Results:
 
3375
 *    Mask of flags containing socket state.
 
3376
 *
 
3377
 * Side effects:
 
3378
 *    None.
 
3379
 *
 
3380
 *----------------------------------------------------------------------------
 
3381
 */
 
3382
 
 
3383
static unsigned int
 
3384
VSockVmciPoll(struct file *file,    // IN
 
3385
              struct socket *sock,  // IN
 
3386
              poll_table *wait)     // IN
 
3387
{
 
3388
   struct sock *sk;
 
3389
   unsigned int mask;
 
3390
 
 
3391
   sk = sock->sk;
 
3392
 
 
3393
   poll_wait(file, sk->compat_sk_sleep, wait);
 
3394
   mask = 0;
 
3395
 
 
3396
   if (sk->compat_sk_err) {
 
3397
      mask |= POLLERR;
 
3398
   }
 
3399
 
 
3400
   if (sk->compat_sk_shutdown == SHUTDOWN_MASK) {
 
3401
      mask |= POLLHUP;
 
3402
   }
 
3403
 
 
3404
   /* POLLRDHUP wasn't added until 2.6.17. */
 
3405
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17)
 
3406
   if (sk->compat_sk_shutdown & RCV_SHUTDOWN) {
 
3407
      mask |= POLLRDHUP;
 
3408
   }
 
3409
#endif
 
3410
 
 
3411
   if (sock->type == SOCK_DGRAM) {
 
3412
      /*
 
3413
       * For datagram sockets we can read if there is something in the queue
 
3414
       * and write as long as the socket isn't shutdown for sending.
 
3415
       */
 
3416
      if (!skb_queue_empty(&sk->compat_sk_receive_queue) ||
 
3417
          (sk->compat_sk_shutdown & RCV_SHUTDOWN)) {
 
3418
         mask |= POLLIN | POLLRDNORM;
 
3419
      }
 
3420
 
 
3421
      if (!(sk->compat_sk_shutdown & SEND_SHUTDOWN)) {
 
3422
         mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
 
3423
      }
 
3424
#ifdef VMX86_TOOLS
 
3425
   } else if (sock->type == SOCK_STREAM) {
 
3426
      VSockVmciSock *vsk;
 
3427
 
 
3428
      lock_sock(sk);
 
3429
 
 
3430
      vsk = vsock_sk(sk);
 
3431
 
 
3432
      /*
 
3433
       * Listening sockets that have connections in their accept queue and
 
3434
       * connected sockets that have consumable data can be read.  Sockets
 
3435
       * whose connections have been close, reset, or terminated should also be
 
3436
       * considered read, and we check the shutdown flag for that.
 
3437
       */
 
3438
      if ((sk->compat_sk_state == SS_LISTEN &&
 
3439
           !VSockVmciIsAcceptQueueEmpty(sk)) ||
 
3440
          (!VMCI_HANDLE_INVALID(vsk->qpHandle) &&
 
3441
           !(sk->compat_sk_shutdown & RCV_SHUTDOWN) &&
 
3442
           VMCIQueue_BufReady(vsk->consumeQ,
 
3443
                              vsk->produceQ, vsk->consumeSize)) ||
 
3444
           sk->compat_sk_shutdown) {
 
3445
          mask |= POLLIN | POLLRDNORM;
 
3446
      }
 
3447
 
 
3448
      /*
 
3449
       * Connected sockets that can produce data can be written.
 
3450
       */
 
3451
      if (sk->compat_sk_state == SS_CONNECTED &&
 
3452
          !(sk->compat_sk_shutdown & SEND_SHUTDOWN) &&
 
3453
          VMCIQueue_FreeSpace(vsk->produceQ,
 
3454
                              vsk->consumeQ, vsk->produceSize) > 0) {
 
3455
         mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
 
3456
      }
 
3457
 
 
3458
      /*
 
3459
       * Connected sockets also need to notify their peer that they are
 
3460
       * waiting.  Optimally these calls would happen in the code that decides
 
3461
       * whether the caller will wait or not, but that's core kernel code and
 
3462
       * this is the best we can do.  If the caller doesn't sleep, the worst
 
3463
       * that happens is a few extra datagrams are sent.
 
3464
       */
 
3465
      if (sk->compat_sk_state == SS_CONNECTED) {
 
3466
         VSockVmciSendWaitingWrite(sk, 1);
 
3467
         VSockVmciSendWaitingRead(sk, 1);
 
3468
      }
 
3469
 
 
3470
      release_sock(sk);
 
3471
#endif
 
3472
   }
 
3473
 
 
3474
   return mask;
 
3475
}
 
3476
 
 
3477
 
 
3478
#ifdef VMX86_TOOLS
 
3479
/*
 
3480
 *----------------------------------------------------------------------------
 
3481
 *
 
3482
 * VSockVmciListen --
 
3483
 *
 
3484
 *      Signify that this socket is listening for connection requests.
 
3485
 *
 
3486
 * Results:
 
3487
 *      Zero on success, negative error code on failure.
 
3488
 *
 
3489
 * Side effects:
 
3490
 *      None.
 
3491
 *
 
3492
 *----------------------------------------------------------------------------
 
3493
 */
 
3494
 
 
3495
static int
 
3496
VSockVmciListen(struct socket *sock,    // IN
 
3497
                int backlog)            // IN
 
3498
{
 
3499
   int err;
 
3500
   struct sock *sk;
 
3501
   VSockVmciSock *vsk;
 
3502
 
 
3503
   sk = sock->sk;
 
3504
 
 
3505
   lock_sock(sk);
 
3506
 
 
3507
   if (sock->type != SOCK_STREAM) {
 
3508
      err = -EOPNOTSUPP;
 
3509
      goto out;
 
3510
   }
 
3511
 
 
3512
   if (sock->state != SS_UNCONNECTED) {
 
3513
      err = -EINVAL;
 
3514
      goto out;
 
3515
   }
 
3516
 
 
3517
   vsk = vsock_sk(sk);
 
3518
 
 
3519
   if (!VSockAddr_Bound(&vsk->localAddr)) {
 
3520
      err = -EINVAL;
 
3521
      goto out;
 
3522
   }
 
3523
 
 
3524
   sk->compat_sk_max_ack_backlog = backlog;
 
3525
   sk->compat_sk_state = SS_LISTEN;
 
3526
 
 
3527
   err = 0;
 
3528
 
 
3529
out:
 
3530
   release_sock(sk);
 
3531
   return err;
 
3532
}
 
3533
#endif
 
3534
 
 
3535
 
 
3536
/*
 
3537
 *----------------------------------------------------------------------------
 
3538
 *
 
3539
 * VSockVmciShutdown --
 
3540
 *
 
3541
 *    Shuts down the provided socket in the provided method.
 
3542
 *
 
3543
 * Results:
 
3544
 *    Zero on success, negative error code on failure.
 
3545
 *
 
3546
 * Side effects:
 
3547
 *    None.
 
3548
 *
 
3549
 *----------------------------------------------------------------------------
 
3550
 */
 
3551
 
 
3552
static int
 
3553
VSockVmciShutdown(struct socket *sock,  // IN
 
3554
                  int mode)             // IN
 
3555
{
 
3556
   struct sock *sk;
 
3557
 
 
3558
   /*
 
3559
    * User level uses SHUT_RD (0) and SHUT_WR (1), but the kernel uses
 
3560
    * RCV_SHUTDOWN (1) and SEND_SHUTDOWN (2), so we must increment mode here
 
3561
    * like the other address families do.  Note also that the increment makes
 
3562
    * SHUT_RDWR (2) into RCV_SHUTDOWN | SEND_SHUTDOWN (3), which is what we
 
3563
    * want.
 
3564
    */
 
3565
   mode++;
 
3566
 
 
3567
   if ((mode & ~SHUTDOWN_MASK) || !mode) {
 
3568
      return -EINVAL;
 
3569
   }
 
3570
 
 
3571
   if (sock->state == SS_UNCONNECTED) {
 
3572
      return -ENOTCONN;
 
3573
   }
 
3574
 
 
3575
   sk = sock->sk;
 
3576
   sock->state = SS_DISCONNECTING;
 
3577
 
 
3578
   /* Receive and send shutdowns are treated alike. */
 
3579
   mode = mode & (RCV_SHUTDOWN | SEND_SHUTDOWN);
 
3580
   if (mode) {
 
3581
      lock_sock(sk);
 
3582
      sk->compat_sk_shutdown |= mode;
 
3583
      sk->compat_sk_state_change(sk);
 
3584
      release_sock(sk);
 
3585
   }
 
3586
 
 
3587
#ifdef VMX86_TOOLS
 
3588
   if (sk->compat_sk_type == SOCK_STREAM && mode) {
 
3589
      VSOCK_SEND_SHUTDOWN(sk, mode);
 
3590
   }
 
3591
#endif
 
3592
 
 
3593
   return 0;
 
3594
}
 
3595
 
 
3596
 
 
3597
/*
 
3598
 *----------------------------------------------------------------------------
 
3599
 *
 
3600
 * VSockVmciDgramSendmsg --
 
3601
 *
 
3602
 *    Sends a datagram.
 
3603
 *
 
3604
 * Results:
 
3605
 *    Number of bytes sent on success, negative error code on failure.
 
3606
 *
 
3607
 * Side effects:
 
3608
 *    None.
 
3609
 *
 
3610
 *----------------------------------------------------------------------------
 
3611
 */
 
3612
 
 
3613
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 43)
 
3614
static int
 
3615
VSockVmciDgramSendmsg(struct socket *sock,          // IN: socket to send on
 
3616
                      struct msghdr *msg,           // IN: message to send
 
3617
                      int len,                      // IN: length of message
 
3618
                      struct scm_cookie *scm)       // UNUSED
 
3619
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 65)
 
3620
static int
 
3621
VSockVmciDgramSendmsg(struct kiocb *kiocb,          // UNUSED
 
3622
                      struct socket *sock,          // IN: socket to send on
 
3623
                      struct msghdr *msg,           // IN: message to send
 
3624
                      int len,                      // IN: length of message
 
3625
                      struct scm_cookie *scm);      // UNUSED
 
3626
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 2)
 
3627
static int
 
3628
VSockVmciDgramSendmsg(struct kiocb *kiocb,          // UNUSED
 
3629
                      struct socket *sock,          // IN: socket to send on
 
3630
                      struct msghdr *msg,           // IN: message to send
 
3631
                      int len)                      // IN: length of message
 
3632
#else
 
3633
static int
 
3634
VSockVmciDgramSendmsg(struct kiocb *kiocb,          // UNUSED
 
3635
                      struct socket *sock,          // IN: socket to send on
 
3636
                      struct msghdr *msg,           // IN: message to send
 
3637
                      size_t len)                   // IN: length of message
 
3638
#endif
 
3639
{
 
3640
   int err;
 
3641
   struct sock *sk;
 
3642
   VSockVmciSock *vsk;
 
3643
   struct sockaddr_vm *remoteAddr;
 
3644
   VMCIDatagram *dg;
 
3645
 
 
3646
   if (msg->msg_flags & MSG_OOB) {
 
3647
      return -EOPNOTSUPP;
 
3648
   }
 
3649
 
 
3650
   if (len > VMCI_MAX_DG_PAYLOAD_SIZE) {
 
3651
      return -EMSGSIZE;
 
3652
   }
 
3653
 
 
3654
   /* For now, MSG_DONTWAIT is always assumed... */
 
3655
   err = 0;
 
3656
   sk = sock->sk;
 
3657
   vsk = vsock_sk(sk);
 
3658
 
 
3659
   lock_sock(sk);
 
3660
 
 
3661
   if (!VSockAddr_Bound(&vsk->localAddr)) {
 
3662
      struct sockaddr_vm localAddr;
 
3663
 
 
3664
      VSockAddr_Init(&localAddr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
 
3665
      if ((err = __VSockVmciBind(sk, &localAddr))) {
 
3666
         goto out;
 
3667
      }
 
3668
   }
 
3669
 
 
3670
   /*
 
3671
    * If the provided message contains an address, use that.  Otherwise fall
 
3672
    * back on the socket's remote handle (if it has been connected).
 
3673
    */
 
3674
   if (msg->msg_name &&
 
3675
       VSockAddr_Cast(msg->msg_name, msg->msg_namelen, &remoteAddr) == 0) {
 
3676
      /* Ensure this address is of the right type and is a valid destination. */
 
3677
      // XXXAB Temporary to handle test program
 
3678
      if (remoteAddr->svm_cid == VMADDR_CID_ANY) {
 
3679
         remoteAddr->svm_cid = VMCI_GetContextID();
 
3680
      }
 
3681
 
 
3682
      if (!VSockAddr_Bound(remoteAddr)) {
 
3683
         err = -EINVAL;
 
3684
         goto out;
 
3685
      }
 
3686
   } else if (sock->state == SS_CONNECTED) {
 
3687
      remoteAddr = &vsk->remoteAddr;
 
3688
      // XXXAB Temporary to handle test program
 
3689
      if (remoteAddr->svm_cid == VMADDR_CID_ANY) {
 
3690
         remoteAddr->svm_cid = VMCI_GetContextID();
 
3691
      }
 
3692
 
 
3693
      /* XXX Should connect() or this function ensure remoteAddr is bound? */
 
3694
      if (!VSockAddr_Bound(&vsk->remoteAddr)) {
 
3695
         err = -EINVAL;
 
3696
         goto out;
 
3697
      }
 
3698
   } else {
 
3699
      err = -EINVAL;
 
3700
      goto out;
 
3701
   }
 
3702
 
 
3703
   /*
 
3704
    * Allocate a buffer for the user's message and our packet header.
 
3705
    */
 
3706
   dg = kmalloc(len + sizeof *dg, GFP_KERNEL);
 
3707
   if (!dg) {
 
3708
      err = -ENOMEM;
 
3709
      goto out;
 
3710
   }
 
3711
 
 
3712
   memcpy_fromiovec(VMCI_DG_PAYLOAD(dg), msg->msg_iov, len);
 
3713
 
 
3714
   dg->dst = VMCI_MAKE_HANDLE(remoteAddr->svm_cid, remoteAddr->svm_port);
 
3715
   dg->src = VMCI_MAKE_HANDLE(vsk->localAddr.svm_cid, vsk->localAddr.svm_port);
 
3716
   dg->payloadSize = len;
 
3717
 
 
3718
   err = VMCIDatagram_Send(dg);
 
3719
   kfree(dg);
 
3720
   if (err < 0) {
 
3721
      err = VSockVmci_ErrorToVSockError(err);
 
3722
      goto out;
 
3723
   }
 
3724
 
 
3725
   /*
 
3726
    * err is the number of bytes sent on success.  We need to subtract the
 
3727
    * VSock-specific header portions of what we've sent.
 
3728
    */
 
3729
   err -= sizeof *dg;
 
3730
 
 
3731
out:
 
3732
   release_sock(sk);
 
3733
   return err;
 
3734
}
 
3735
 
 
3736
#ifdef VMX86_TOOLS
 
3737
/*
 
3738
 *----------------------------------------------------------------------------
 
3739
 *
 
3740
 * VSockVmciStreamSetsockopt --
 
3741
 *
 
3742
 *    Set a socket option on a stream socket
 
3743
 *
 
3744
 * Results:
 
3745
 *    0 on success, negative error code on failure.
 
3746
 *
 
3747
 * Side effects:
 
3748
 *    None.
 
3749
 *
 
3750
 *----------------------------------------------------------------------------
 
3751
 */
 
3752
 
 
3753
int
 
3754
VSockVmciStreamSetsockopt(struct socket *sock,       // IN/OUT
 
3755
                          int level,                 // IN
 
3756
                          int optname,               // IN
 
3757
                          char __user *optval,       // IN
 
3758
                          int optlen)                // IN
 
3759
{
 
3760
   int err;
 
3761
   struct sock *sk;
 
3762
   VSockVmciSock *vsk;
 
3763
   uint64 val;
 
3764
 
 
3765
   if (level != VSockVmci_GetAFValue()) {
 
3766
      return -ENOPROTOOPT;
 
3767
   }
 
3768
 
 
3769
   if (optlen < sizeof val) {
 
3770
      return -EINVAL;
 
3771
   }
 
3772
 
 
3773
   if (copy_from_user(&val, optval, sizeof val) != 0) {
 
3774
      return -EFAULT;
 
3775
   }
 
3776
 
 
3777
   err = 0;
 
3778
   sk = sock->sk;
 
3779
   vsk = vsock_sk(sk);
 
3780
 
 
3781
   ASSERT(vsk->queuePairMinSize <= vsk->queuePairSize &&
 
3782
          vsk->queuePairSize <= vsk->queuePairMaxSize);
 
3783
 
 
3784
   lock_sock(sk);
 
3785
 
 
3786
   switch (optname) {
 
3787
   case SO_VMCI_BUFFER_SIZE:
 
3788
      if (val < vsk->queuePairMinSize || val > vsk->queuePairMaxSize) {
 
3789
         err = -EINVAL;
 
3790
         goto out;
 
3791
      }
 
3792
      vsk->queuePairSize = val;
 
3793
      break;
 
3794
 
 
3795
   case SO_VMCI_BUFFER_MAX_SIZE:
 
3796
      if (val < vsk->queuePairSize) {
 
3797
         err = -EINVAL;
 
3798
         goto out;
 
3799
      }
 
3800
      vsk->queuePairMaxSize = val;
 
3801
      break;
 
3802
 
 
3803
   case SO_VMCI_BUFFER_MIN_SIZE:
 
3804
      if (val > vsk->queuePairSize) {
 
3805
         err = -EINVAL;
 
3806
         goto out;
 
3807
      }
 
3808
      vsk->queuePairMinSize = val;
 
3809
      break;
 
3810
 
 
3811
   default:
 
3812
      err = -ENOPROTOOPT;
 
3813
      break;
 
3814
   }
 
3815
 
 
3816
out:
 
3817
 
 
3818
   ASSERT(vsk->queuePairMinSize <= vsk->queuePairSize &&
 
3819
          vsk->queuePairSize <= vsk->queuePairMaxSize);
 
3820
 
 
3821
   release_sock(sk);
 
3822
   return err;
 
3823
}
 
3824
 
 
3825
#endif
 
3826
 
 
3827
#ifdef VMX86_TOOLS
 
3828
/*
 
3829
 *----------------------------------------------------------------------------
 
3830
 *
 
3831
 * VSockVmciStreamGetsockopt --
 
3832
 *
 
3833
 *    Get a socket option for a stream socket
 
3834
 *
 
3835
 * Results:
 
3836
 *    0 on success, negative error code on failure.
 
3837
 *
 
3838
 * Side effects:
 
3839
 *    None.
 
3840
 *
 
3841
 *----------------------------------------------------------------------------
 
3842
 */
 
3843
 
 
3844
int
 
3845
VSockVmciStreamGetsockopt(struct socket *sock,          // IN
 
3846
                          int level,                    // IN
 
3847
                          int optname,                  // IN
 
3848
                          char __user *optval,          // OUT
 
3849
                          int __user * optlen)          // IN/OUT
 
3850
{
 
3851
   int err;
 
3852
   int len;
 
3853
   struct sock *sk;
 
3854
   VSockVmciSock *vsk;
 
3855
   uint64 val;
 
3856
 
 
3857
   if (level != VSockVmci_GetAFValue()) {
 
3858
      return -ENOPROTOOPT;
 
3859
   }
 
3860
 
 
3861
   if ((err = get_user(len, optlen)) != 0) {
 
3862
      return err;
 
3863
   }
 
3864
   if (len < sizeof val) {
 
3865
      return -EINVAL;
 
3866
   }
 
3867
 
 
3868
   len = sizeof val;
 
3869
 
 
3870
   err = 0;
 
3871
   sk = sock->sk;
 
3872
   vsk = vsock_sk(sk);
 
3873
 
 
3874
   switch (optname) {
 
3875
   case SO_VMCI_BUFFER_SIZE:
 
3876
      val = vsk->queuePairSize;
 
3877
      break;
 
3878
 
 
3879
   case SO_VMCI_BUFFER_MAX_SIZE:
 
3880
      val = vsk->queuePairMaxSize;
 
3881
      break;
 
3882
 
 
3883
   case SO_VMCI_BUFFER_MIN_SIZE:
 
3884
      val = vsk->queuePairMinSize;
 
3885
      break;
 
3886
 
 
3887
   default:
 
3888
      return -ENOPROTOOPT;
 
3889
   }
 
3890
 
 
3891
   if ((err = put_user(val, (uint64 __user *)optval)) != 0) {
 
3892
      return err;
 
3893
   }
 
3894
   if ((err = put_user(len, optlen)) != 0) {
 
3895
      return err;
 
3896
   }
 
3897
   return 0;
 
3898
}
 
3899
#endif
 
3900
 
 
3901
 
 
3902
#ifdef VMX86_TOOLS
 
3903
/*
 
3904
 *----------------------------------------------------------------------------
 
3905
 *
 
3906
 * VSockVmciStreamSendmsg --
 
3907
 *
 
3908
 *    Sends a message on the socket.
 
3909
 *
 
3910
 * Results:
 
3911
 *    Number of bytes sent on success, negative error code on failure.
 
3912
 *
 
3913
 * Side effects:
 
3914
 *    None.
 
3915
 *
 
3916
 *----------------------------------------------------------------------------
 
3917
 */
 
3918
 
 
3919
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 43)
 
3920
static int
 
3921
VSockVmciStreamSendmsg(struct socket *sock,          // IN: socket to send on
 
3922
                       struct msghdr *msg,           // IN: message to send
 
3923
                       int len,                      // IN: length of message
 
3924
                       struct scm_cookie *scm)       // UNUSED
 
3925
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 65)
 
3926
static int
 
3927
VSockVmciStreamSendmsg(struct kiocb *kiocb,          // UNUSED
 
3928
                       struct socket *sock,          // IN: socket to send on
 
3929
                       struct msghdr *msg,           // IN: message to send
 
3930
                       int len,                      // IN: length of message
 
3931
                       struct scm_cookie *scm);      // UNUSED
 
3932
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 2)
 
3933
static int
 
3934
VSockVmciStreamSendmsg(struct kiocb *kiocb,          // UNUSED
 
3935
                       struct socket *sock,          // IN: socket to send on
 
3936
                       struct msghdr *msg,           // IN: message to send
 
3937
                       int len)                      // IN: length of message
 
3938
#else
 
3939
static int
 
3940
VSockVmciStreamSendmsg(struct kiocb *kiocb,          // UNUSED
 
3941
                       struct socket *sock,          // IN: socket to send on
 
3942
                       struct msghdr *msg,           // IN: message to send
 
3943
                       size_t len)                   // IN: length of message
 
3944
#endif
 
3945
{
 
3946
   struct sock *sk;
 
3947
   VSockVmciSock *vsk;
 
3948
   ssize_t totalWritten;
 
3949
   long timeout;
 
3950
   int err;
 
3951
#if defined(VMX86_TOOLS) && defined(VSOCK_OPTIMIZATION_WAITING_NOTIFY)
 
3952
   uint64 produceTail;
 
3953
   uint64 consumeHead;
 
3954
#endif
 
3955
   COMPAT_DEFINE_WAIT(wait);
 
3956
 
 
3957
   sk = sock->sk;
 
3958
   vsk = vsock_sk(sk);
 
3959
   totalWritten = 0;
 
3960
   err = 0;
 
3961
 
 
3962
   if (msg->msg_flags & MSG_OOB) {
 
3963
      return -EOPNOTSUPP;
 
3964
   }
 
3965
 
 
3966
   lock_sock(sk);
 
3967
 
 
3968
   /* Callers should not provide a destination with stream sockets. */
 
3969
   if (msg->msg_namelen) {
 
3970
      err = sk->compat_sk_state == SS_CONNECTED ? -EISCONN : -EOPNOTSUPP;
 
3971
      goto out;
 
3972
   }
 
3973
 
 
3974
   if (sk->compat_sk_shutdown & SEND_SHUTDOWN) {
 
3975
      err = -EPIPE;
 
3976
      goto out;
 
3977
   }
 
3978
 
 
3979
   if (sk->compat_sk_state != SS_CONNECTED ||
 
3980
       !VSockAddr_Bound(&vsk->localAddr)) {
 
3981
      err = -ENOTCONN;
 
3982
      goto out;
 
3983
   }
 
3984
 
 
3985
   if (!VSockAddr_Bound(&vsk->remoteAddr)) {
 
3986
      err = -EDESTADDRREQ;
 
3987
      goto out;
 
3988
   }
 
3989
 
 
3990
   /*
 
3991
    * Wait for room in the produce queue to enqueue our user's data.
 
3992
    */
 
3993
   timeout = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
 
3994
   compat_init_prepare_to_wait(sk->compat_sk_sleep, &wait, TASK_INTERRUPTIBLE);
 
3995
 
 
3996
   while (totalWritten < len) {
 
3997
      Bool sentWrote;
 
3998
      unsigned int retries;
 
3999
      ssize_t written;
 
4000
 
 
4001
      sentWrote = FALSE;
 
4002
      retries = 0;
 
4003
 
 
4004
      while (VMCIQueue_FreeSpace(vsk->produceQ,
 
4005
                                 vsk->consumeQ, vsk->produceSize) == 0 &&
 
4006
             sk->compat_sk_err == 0 &&
 
4007
             !(sk->compat_sk_shutdown & SEND_SHUTDOWN) &&
 
4008
             !(vsk->peerShutdown & RCV_SHUTDOWN)) {
 
4009
 
 
4010
         /* Don't wait for non-blocking sockets. */
 
4011
         if (timeout == 0) {
 
4012
            err = -EAGAIN;
 
4013
            goto outWait;
 
4014
         }
 
4015
 
 
4016
         /* Notify our peer that we are waiting for room to write. */
 
4017
         if (!VSockVmciSendWaitingWrite(sk, 1)) {
 
4018
            err = -EHOSTUNREACH;
 
4019
            goto outWait;
 
4020
         }
 
4021
 
 
4022
         release_sock(sk);
 
4023
         timeout = schedule_timeout(timeout);
 
4024
         lock_sock(sk);
 
4025
         if (signal_pending(current)) {
 
4026
            err = sock_intr_errno(timeout);
 
4027
            goto outWait;
 
4028
         } else if (timeout == 0) {
 
4029
            err = -EAGAIN;
 
4030
            goto outWait;
 
4031
         }
 
4032
 
 
4033
         compat_cont_prepare_to_wait(sk->compat_sk_sleep,
 
4034
                                     &wait, TASK_INTERRUPTIBLE);
 
4035
      }
 
4036
 
 
4037
      /*
 
4038
       * These checks occur both as part of and after the loop conditional
 
4039
       * since we need to check before and after sleeping.
 
4040
       */
 
4041
      if (sk->compat_sk_err) {
 
4042
         err = -sk->compat_sk_err;
 
4043
         goto outWait;
 
4044
      } else if ((sk->compat_sk_shutdown & SEND_SHUTDOWN) ||
 
4045
                 (vsk->peerShutdown & RCV_SHUTDOWN)) {
 
4046
         err = -EPIPE;
 
4047
         goto outWait;
 
4048
      }
 
4049
 
 
4050
      /*
 
4051
       * Note that enqueue will only write as many bytes as are free in the
 
4052
       * produce queue, so we don't need to ensure len is smaller than the queue
 
4053
       * size.  It is the caller's responsibility to check how many bytes we were
 
4054
       * able to send.
 
4055
       */
 
4056
#if defined(VMX86_TOOLS) && defined(VSOCK_OPTIMIZATION_WAITING_NOTIFY)
 
4057
      VMCIQueue_GetPointers(vsk->produceQ, vsk->consumeQ,
 
4058
                            &produceTail, &consumeHead);
 
4059
#endif
 
4060
 
 
4061
      written = VMCIQueue_EnqueueV(vsk->produceQ, vsk->consumeQ,
 
4062
                                   vsk->produceSize, msg->msg_iov,
 
4063
                                   len - totalWritten);
 
4064
      if (written < 0) {
 
4065
         err = -ENOMEM;
 
4066
         goto outWait;
 
4067
      }
 
4068
 
 
4069
#if defined(VMX86_TOOLS) && defined(VSOCK_OPTIMIZATION_WAITING_NOTIFY)
 
4070
      /*
 
4071
       * Detect a wrap-around to maintain queue generation.  Note that this is
 
4072
       * safe since we hold the socket lock across the two queue pair
 
4073
       * operations.
 
4074
       */
 
4075
      if (written >= vsk->produceSize - produceTail) {
 
4076
         vsk->produceQGeneration++;
 
4077
      }
 
4078
#endif
 
4079
 
 
4080
      totalWritten += written;
 
4081
 
 
4082
      if (VSockVmciNotifyWaitingRead(vsk)) {
 
4083
         /*
 
4084
          * Notify the peer that we have written, retrying the send on failure up to
 
4085
          * our maximum value. See the XXX comment for the corresponding piece of
 
4086
          * code in StreamRecvmsg() for potential improvements.
 
4087
          */
 
4088
         while (!(vsk->peerShutdown & RCV_SHUTDOWN) &&
 
4089
                !sentWrote &&
 
4090
                retries < VSOCK_MAX_DGRAM_RESENDS) {
 
4091
            err = VSOCK_SEND_WROTE(sk);
 
4092
            if (err >= 0) {
 
4093
               sentWrote = TRUE;
 
4094
            }
 
4095
 
 
4096
            retries++;
 
4097
         }
 
4098
 
 
4099
         if (retries >= VSOCK_MAX_DGRAM_RESENDS) {
 
4100
            Warning("unable to send wrote notification to peer for socket %p.\n", sk);
 
4101
            goto outWait;
 
4102
         } else {
 
4103
#if defined(VMX86_TOOLS) && defined(VSOCK_OPTIMIZATION_WAITING_NOTIFY)
 
4104
            vsk->peerWaitingRead = FALSE;
 
4105
#endif
 
4106
         }
 
4107
      }
 
4108
   }
 
4109
 
 
4110
   ASSERT(totalWritten <= INT_MAX);
 
4111
 
 
4112
outWait:
 
4113
   if (totalWritten > 0) {
 
4114
      err = totalWritten;
 
4115
   }
 
4116
   compat_finish_wait(sk->compat_sk_sleep, &wait, TASK_RUNNING);
 
4117
out:
 
4118
   release_sock(sk);
 
4119
   return err;
 
4120
}
 
4121
#endif
 
4122
 
 
4123
 
 
4124
/*
 
4125
 *----------------------------------------------------------------------------
 
4126
 *
 
4127
 * VSockVmciDgramRecvmsg --
 
4128
 *
 
4129
 *    Receives a datagram and places it in the caller's msg.
 
4130
 *
 
4131
 * Results:
 
4132
 *    The size of the payload on success, negative value on failure.
 
4133
 *
 
4134
 * Side effects:
 
4135
 *    None.
 
4136
 *
 
4137
 *----------------------------------------------------------------------------
 
4138
 */
 
4139
 
 
4140
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 43)
 
4141
static int
 
4142
VSockVmciDgramRecvmsg(struct socket *sock,           // IN: socket to receive from
 
4143
                      struct msghdr *msg,            // IN/OUT: message to receive into
 
4144
                      int len,                       // IN: length of receive buffer
 
4145
                      int flags,                     // IN: receive flags
 
4146
                      struct scm_cookie *scm)        // UNUSED
 
4147
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 65)
 
4148
static int
 
4149
VSockVmciDgramRecvmsg(struct kiocb *kiocb,          // UNUSED
 
4150
                      struct socket *sock,          // IN: socket to receive from
 
4151
                      struct msghdr *msg,           // IN/OUT: message to receive into
 
4152
                      int len,                      // IN: length of receive buffer
 
4153
                      int flags,                    // IN: receive flags
 
4154
                      struct scm_cookie *scm)       // UNUSED
 
4155
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 2)
 
4156
static int
 
4157
VSockVmciDgramRecvmsg(struct kiocb *kiocb,          // UNUSED
 
4158
                      struct socket *sock,          // IN: socket to receive from
 
4159
                      struct msghdr *msg,           // IN/OUT: message to receive into
 
4160
                      int len,                      // IN: length of receive buffer
 
4161
                      int flags)                    // IN: receive flags
 
4162
#else
 
4163
static int
 
4164
VSockVmciDgramRecvmsg(struct kiocb *kiocb,          // UNUSED
 
4165
                      struct socket *sock,          // IN: socket to receive from
 
4166
                      struct msghdr *msg,           // IN/OUT: message to receive into
 
4167
                      size_t len,                   // IN: length of receive buffer
 
4168
                      int flags)                    // IN: receive flags
 
4169
#endif
 
4170
{
 
4171
   int err;
 
4172
   int noblock;
 
4173
   struct sock *sk;
 
4174
   VMCIDatagram *dg;
 
4175
   size_t payloadLen;
 
4176
   struct sk_buff *skb;
 
4177
   struct sockaddr_vm *vmciAddr;
 
4178
 
 
4179
   err = 0;
 
4180
   sk = sock->sk;
 
4181
   payloadLen = 0;
 
4182
   noblock = flags & MSG_DONTWAIT;
 
4183
   vmciAddr = (struct sockaddr_vm *)msg->msg_name;
 
4184
 
 
4185
   if (flags & MSG_OOB || flags & MSG_ERRQUEUE) {
 
4186
      return -EOPNOTSUPP;
 
4187
   }
 
4188
 
 
4189
   /* Retrieve the head sk_buff from the socket's receive queue. */
 
4190
   skb = skb_recv_datagram(sk, flags, noblock, &err);
 
4191
   if (err) {
 
4192
      return err;
 
4193
   }
 
4194
 
 
4195
   if (!skb) {
 
4196
      return -EAGAIN;
 
4197
   }
 
4198
 
 
4199
   dg = (VMCIDatagram *)skb->data;
 
4200
   if (!dg) {
 
4201
      /* err is 0, meaning we read zero bytes. */
 
4202
      goto out;
 
4203
   }
 
4204
 
 
4205
   payloadLen = dg->payloadSize;
 
4206
   /* Ensure the sk_buff matches the payload size claimed in the packet. */
 
4207
   if (payloadLen != skb->len - sizeof *dg) {
 
4208
      err = -EINVAL;
 
4209
      goto out;
 
4210
   }
 
4211
 
 
4212
   if (payloadLen > len) {
 
4213
      payloadLen = len;
 
4214
      msg->msg_flags |= MSG_TRUNC;
 
4215
   }
 
4216
 
 
4217
   /* Place the datagram payload in the user's iovec. */
 
4218
   err = skb_copy_datagram_iovec(skb, sizeof *dg, msg->msg_iov, payloadLen);
 
4219
   if (err) {
 
4220
      goto out;
 
4221
   }
 
4222
 
 
4223
   msg->msg_namelen = 0;
 
4224
   if (vmciAddr) {
 
4225
      /* Provide the address of the sender. */
 
4226
      VSockAddr_Init(vmciAddr,
 
4227
                     VMCI_HANDLE_TO_CONTEXT_ID(dg->src),
 
4228
                     VMCI_HANDLE_TO_RESOURCE_ID(dg->src));
 
4229
      msg->msg_namelen = sizeof *vmciAddr;
 
4230
   }
 
4231
   err = payloadLen;
 
4232
 
 
4233
out:
 
4234
   skb_free_datagram(sk, skb);
 
4235
   return err;
 
4236
}
 
4237
 
 
4238
 
 
4239
#ifdef VMX86_TOOLS
 
4240
/*
 
4241
 *----------------------------------------------------------------------------
 
4242
 *
 
4243
 * VSockVmciStreamRecvmsg --
 
4244
 *
 
4245
 *    Receives a datagram and places it in the caller's msg.
 
4246
 *
 
4247
 * Results:
 
4248
 *    The size of the payload on success, negative value on failure.
 
4249
 *
 
4250
 * Side effects:
 
4251
 *    None.
 
4252
 *
 
4253
 *----------------------------------------------------------------------------
 
4254
 */
 
4255
 
 
4256
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 43)
 
4257
static int
 
4258
VSockVmciStreamRecvmsg(struct socket *sock,           // IN: socket to receive from
 
4259
                       struct msghdr *msg,            // IN/OUT: message to receive into
 
4260
                       int len,                       // IN: length of receive buffer
 
4261
                       int flags,                     // IN: receive flags
 
4262
                       struct scm_cookie *scm)        // UNUSED
 
4263
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 65)
 
4264
static int
 
4265
VSockVmciStreamRecvmsg(struct kiocb *kiocb,          // UNUSED
 
4266
                       struct socket *sock,          // IN: socket to receive from
 
4267
                       struct msghdr *msg,           // IN/OUT: message to receive into
 
4268
                       int len,                      // IN: length of receive buffer
 
4269
                       int flags,                    // IN: receive flags
 
4270
                       struct scm_cookie *scm)       // UNUSED
 
4271
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 2)
 
4272
static int
 
4273
VSockVmciStreamRecvmsg(struct kiocb *kiocb,          // UNUSED
 
4274
                       struct socket *sock,          // IN: socket to receive from
 
4275
                       struct msghdr *msg,           // IN/OUT: message to receive into
 
4276
                       int len,                      // IN: length of receive buffer
 
4277
                       int flags)                    // IN: receive flags
 
4278
#else
 
4279
static int
 
4280
VSockVmciStreamRecvmsg(struct kiocb *kiocb,          // UNUSED
 
4281
                       struct socket *sock,          // IN: socket to receive from
 
4282
                       struct msghdr *msg,           // IN/OUT: message to receive into
 
4283
                       size_t len,                   // IN: length of receive buffer
 
4284
                       int flags)                    // IN: receive flags
 
4285
#endif
 
4286
{
 
4287
   struct sock *sk;
 
4288
   VSockVmciSock *vsk;
 
4289
   int err;
 
4290
   int target;
 
4291
   int64 ready;
 
4292
   long timeout;
 
4293
   ssize_t copied;
 
4294
   Bool sentRead;
 
4295
   unsigned int retries;
 
4296
#if defined(VMX86_TOOLS) && defined(VSOCK_OPTIMIZATION_WAITING_NOTIFY)
 
4297
   uint64 consumeHead;
 
4298
   uint64 produceTail;
 
4299
#endif
 
4300
   COMPAT_DEFINE_WAIT(wait);
 
4301
 
 
4302
   sk = sock->sk;
 
4303
   vsk = vsock_sk(sk);
 
4304
   err = 0;
 
4305
   retries = 0;
 
4306
   sentRead = FALSE;
 
4307
 
 
4308
   lock_sock(sk);
 
4309
 
 
4310
   if (sk->compat_sk_state != SS_CONNECTED) {
 
4311
      err = -ENOTCONN;
 
4312
      goto out;
 
4313
   }
 
4314
 
 
4315
   if (flags & MSG_OOB) {
 
4316
      err = -EOPNOTSUPP;
 
4317
      goto out;
 
4318
   }
 
4319
 
 
4320
   if (sk->compat_sk_shutdown & RCV_SHUTDOWN) {
 
4321
      err = -EPIPE;
 
4322
      goto out;
 
4323
   }
 
4324
 
 
4325
   /*
 
4326
    * We must not copy less than target bytes into the user's buffer before
 
4327
    * returning successfully, so we wait for the consume queue to have that
 
4328
    * much data to consume before dequeueing.  Note that this makes it
 
4329
    * impossible to handle cases where target is greater than the queue size.
 
4330
    */
 
4331
   target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
 
4332
   if (target >= vsk->consumeSize) {
 
4333
      err = -ENOMEM;
 
4334
      goto out;
 
4335
   }
 
4336
   timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
 
4337
   copied = 0;
 
4338
 
 
4339
   compat_init_prepare_to_wait(sk->compat_sk_sleep, &wait, TASK_INTERRUPTIBLE);
 
4340
 
 
4341
   while ((ready = VMCIQueue_BufReady(vsk->consumeQ,
 
4342
                                      vsk->produceQ,
 
4343
                                      vsk->consumeSize)) < target &&
 
4344
          sk->compat_sk_err == 0 &&
 
4345
          !(sk->compat_sk_shutdown & RCV_SHUTDOWN) &&
 
4346
          !(vsk->peerShutdown & SEND_SHUTDOWN)) {
 
4347
 
 
4348
      if (ready < 0) {
 
4349
         /* 
 
4350
          * Invalid queue pair content. XXX This should be changed to
 
4351
          * a connection reset in a later change.
 
4352
          */
 
4353
 
 
4354
         err = -ENOMEM;
 
4355
         goto out;
 
4356
      }
 
4357
 
 
4358
      /* Don't wait for non-blocking sockets. */
 
4359
      if (timeout == 0) {
 
4360
         err = -EAGAIN;
 
4361
         goto outWait;
 
4362
      }
 
4363
 
 
4364
      /* Notify our peer that we are waiting for data to read. */
 
4365
      if (!VSockVmciSendWaitingRead(sk, target)) {
 
4366
         err = -EHOSTUNREACH;
 
4367
         goto outWait;
 
4368
      }
 
4369
 
 
4370
      release_sock(sk);
 
4371
      timeout = schedule_timeout(timeout);
 
4372
      lock_sock(sk);
 
4373
 
 
4374
      if (signal_pending(current)) {
 
4375
         err = sock_intr_errno(timeout);
 
4376
         goto outWait;
 
4377
      } else if (timeout == 0) {
 
4378
         err = -EAGAIN;
 
4379
         goto outWait;
 
4380
      }
 
4381
 
 
4382
      compat_cont_prepare_to_wait(sk->compat_sk_sleep, &wait, TASK_INTERRUPTIBLE);
 
4383
   }
 
4384
 
 
4385
   if (sk->compat_sk_err) {
 
4386
      err = -sk->compat_sk_err;
 
4387
      goto outWait;
 
4388
   } else if (sk->compat_sk_shutdown & RCV_SHUTDOWN) {
 
4389
      err = 0;
 
4390
      goto outWait;
 
4391
   } else if ((vsk->peerShutdown & SEND_SHUTDOWN) &&
 
4392
              VMCIQueue_BufReady(vsk->consumeQ,
 
4393
                                 vsk->produceQ, vsk->consumeSize) < target) {
 
4394
      err = -EPIPE;
 
4395
      goto outWait;
 
4396
   }
 
4397
 
 
4398
   /*
 
4399
    * Now consume up to len bytes from the queue.  Note that since we have the
 
4400
    * socket locked we should copy at least ready bytes.
 
4401
    */
 
4402
#if defined(VMX86_TOOLS) && defined(VSOCK_OPTIMIZATION_WAITING_NOTIFY)
 
4403
   VMCIQueue_GetPointers(vsk->consumeQ, vsk->produceQ,
 
4404
                         &produceTail, &consumeHead);
 
4405
#endif
 
4406
 
 
4407
   copied = VMCIQueue_DequeueV(vsk->produceQ, vsk->consumeQ,
 
4408
                               vsk->consumeSize, msg->msg_iov, len);
 
4409
   if (copied < 0) {
 
4410
      err = -ENOMEM;
 
4411
      goto outWait;
 
4412
   }
 
4413
 
 
4414
#if defined(VMX86_TOOLS) && defined(VSOCK_OPTIMIZATION_WAITING_NOTIFY)
 
4415
   /*
 
4416
    * Detect a wrap-around to maintain queue generation.  Note that this is
 
4417
    * safe since we hold the socket lock across the two queue pair
 
4418
    * operations.
 
4419
    */
 
4420
   if (copied >= vsk->consumeSize - consumeHead) {
 
4421
      vsk->consumeQGeneration++;
 
4422
   }
 
4423
#endif
 
4424
 
 
4425
   ASSERT(copied >= target);
 
4426
 
 
4427
   /*
 
4428
    * If the other side has shutdown for sending and there is nothing more to
 
4429
    * read, then set our socket's RCV_SHUTDOWN flag and modify the socket
 
4430
    * state.
 
4431
    */
 
4432
   if (vsk->peerShutdown & SEND_SHUTDOWN) {
 
4433
      if (VMCIQueue_BufReady(vsk->consumeQ,
 
4434
                             vsk->produceQ, vsk->consumeSize) <= 0) {
 
4435
         sk->compat_sk_shutdown |= RCV_SHUTDOWN;
 
4436
         sk->compat_sk_state = SS_UNCONNECTED;
 
4437
         sk->compat_sk_state_change(sk);
 
4438
      }
 
4439
   }
 
4440
 
 
4441
   if (VSockVmciNotifyWaitingWrite(vsk)) {
 
4442
      /*
 
4443
       * Notify the peer that we have read, retrying the send on failure up to our
 
4444
       * maximum value.  XXX For now we just log the failure, but later we should
 
4445
       * schedule a work item to handle the resend until it succeeds.  That would
 
4446
       * require keeping track of work items in the vsk and cleaning them up upon
 
4447
       * socket close.
 
4448
       */
 
4449
      while (!(vsk->peerShutdown & RCV_SHUTDOWN) &&
 
4450
             !sentRead &&
 
4451
             retries < VSOCK_MAX_DGRAM_RESENDS) {
 
4452
         err = VSOCK_SEND_READ(sk);
 
4453
         if (err >= 0) {
 
4454
            sentRead = TRUE;
 
4455
         }
 
4456
 
 
4457
         retries++;
 
4458
      }
 
4459
 
 
4460
      if (retries >= VSOCK_MAX_DGRAM_RESENDS) {
 
4461
         Warning("unable to send read notification to peer for socket %p.\n", sk);
 
4462
         goto outWait;
 
4463
      } else {
 
4464
#if defined(VMX86_TOOLS) && defined(VSOCK_OPTIMIZATION_WAITING_NOTIFY)
 
4465
         vsk->peerWaitingWrite = FALSE;
 
4466
#endif
 
4467
      }
 
4468
   }
 
4469
 
 
4470
   ASSERT(copied <= INT_MAX);
 
4471
   err = copied;
 
4472
 
 
4473
outWait:
 
4474
   compat_finish_wait(sk->compat_sk_sleep, &wait, TASK_RUNNING);
 
4475
out:
 
4476
   release_sock(sk);
 
4477
   return err;
 
4478
}
 
4479
#endif
 
4480
 
 
4481
 
 
4482
/*
 
4483
 * Protocol operation.
 
4484
 */
 
4485
 
 
4486
/*
 
4487
 *----------------------------------------------------------------------------
 
4488
 *
 
4489
 * VSockVmciCreate --
 
4490
 *
 
4491
 *    Creates a VSocket socket.
 
4492
 *
 
4493
 * Results:
 
4494
 *    Zero on success, negative error code on failure.
 
4495
 *
 
4496
 * Side effects:
 
4497
 *    Socket count is incremented.
 
4498
 *
 
4499
 *----------------------------------------------------------------------------
 
4500
 */
 
4501
 
 
4502
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
 
4503
static int
 
4504
VSockVmciCreate(struct socket *sock,  // IN
 
4505
                int protocol)         // IN
 
4506
#else
 
4507
static int
 
4508
VSockVmciCreate(struct net *net,      // IN
 
4509
                struct socket *sock,  // IN
 
4510
                int protocol)         // IN
 
4511
#endif
 
4512
{
 
4513
   if (!sock) {
 
4514
      return -EINVAL;
 
4515
   }
 
4516
 
 
4517
   if (protocol) {
 
4518
      return -EPROTONOSUPPORT;
 
4519
   }
 
4520
 
 
4521
   switch (sock->type) {
 
4522
   case SOCK_DGRAM:
 
4523
      sock->ops = &vsockVmciDgramOps;
 
4524
      break;
 
4525
#  ifdef VMX86_TOOLS
 
4526
   /*
 
4527
    * Queue pairs are /currently/ only supported within guests, so stream
 
4528
    * sockets are only supported within guests.
 
4529
    */
 
4530
   case SOCK_STREAM:
 
4531
      sock->ops = &vsockVmciStreamOps;
 
4532
      break;
 
4533
#  endif
 
4534
   default:
 
4535
      return -ESOCKTNOSUPPORT;
 
4536
   }
 
4537
 
 
4538
   sock->state = SS_UNCONNECTED;
 
4539
 
 
4540
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
 
4541
   return __VSockVmciCreate(sock, GFP_KERNEL) ? 0 : -ENOMEM;
 
4542
#else
 
4543
   return __VSockVmciCreate(net, sock, GFP_KERNEL) ? 0 : -ENOMEM;
 
4544
#endif
 
4545
}
 
4546
 
 
4547
 
 
4548
/*
 
4549
 *----------------------------------------------------------------------------
 
4550
 *
 
4551
 * VSockVmciIoctl32Handler --
 
4552
 *
 
4553
 *      Handler for 32-bit ioctl(2) on 64-bit.
 
4554
 *
 
4555
 * Results:
 
4556
 *      Same as VsockVmciDevIoctl().
 
4557
 *
 
4558
 * Side effects:
 
4559
 *      None.
 
4560
 *
 
4561
 *----------------------------------------------------------------------------
 
4562
 */
 
4563
 
 
4564
#ifdef VM_X86_64
 
4565
#ifndef HAVE_COMPAT_IOCTL
 
4566
static int
 
4567
VSockVmciIoctl32Handler(unsigned int fd,        // IN
 
4568
                        unsigned int iocmd,     // IN
 
4569
                        unsigned long ioarg,    // IN/OUT
 
4570
                        struct file * filp)     // IN
 
4571
{
 
4572
   int ret;
 
4573
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 26) || \
 
4574
   (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0) && LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 3))
 
4575
   lock_kernel();
 
4576
#endif
 
4577
   ret = -ENOTTY;
 
4578
   if (filp && filp->f_op && filp->f_op->ioctl == VSockVmciDevIoctl) {
 
4579
      ret = VSockVmciDevIoctl(filp->f_dentry->d_inode, filp, iocmd, ioarg);
 
4580
   }
 
4581
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 26) || \
 
4582
   (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0) && LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 3))
 
4583
   unlock_kernel();
 
4584
#endif
 
4585
   return ret;
 
4586
}
 
4587
#endif /* !HAVE_COMPAT_IOCTL */
 
4588
 
 
4589
 
 
4590
/*
 
4591
 *----------------------------------------------------------------------------
 
4592
 *
 
4593
 * register_ioctl32_handlers --
 
4594
 *
 
4595
 *      Registers the ioctl conversion handler.
 
4596
 *
 
4597
 * Results:
 
4598
 *      Zero on success, error code otherwise.
 
4599
 *
 
4600
 * Side effects:
 
4601
 *      None.
 
4602
 *
 
4603
 *----------------------------------------------------------------------------
 
4604
 */
 
4605
 
 
4606
static int
 
4607
register_ioctl32_handlers(void)
 
4608
{
 
4609
#ifndef HAVE_COMPAT_IOCTL
 
4610
   {
 
4611
      int i;
 
4612
      for (i = IOCTL_VMCI_SOCKETS_FIRST; i < IOCTL_VMCI_SOCKETS_LAST; i++) {
 
4613
         int retval = register_ioctl32_conversion(i, VSockVmciIoctl32Handler);
 
4614
         if (retval) {
 
4615
            Warning("Fail to register ioctl32 conversion for cmd %d\n", i);
 
4616
            return retval;
 
4617
         }
 
4618
      }
 
4619
   }
 
4620
#endif /* !HAVE_COMPAT_IOCTL */
 
4621
   return 0;
 
4622
}
 
4623
 
 
4624
 
 
4625
/*
 
4626
 *----------------------------------------------------------------------------
 
4627
 *
 
4628
 * unregister_ioctl32_handlers --
 
4629
 *
 
4630
 *      Unregisters the ioctl converstion handler.
 
4631
 *
 
4632
 * Results:
 
4633
 *      None.
 
4634
 *
 
4635
 * Side effects:
 
4636
 *      None.
 
4637
 *
 
4638
 *----------------------------------------------------------------------------
 
4639
 */
 
4640
 
 
4641
static void
 
4642
unregister_ioctl32_handlers(void)
 
4643
{
 
4644
#ifndef HAVE_COMPAT_IOCTL
 
4645
   {
 
4646
      int i;
 
4647
      for (i = IOCTL_VMCI_SOCKETS_FIRST; i < IOCTL_VMCI_SOCKETS_LAST; i++) {
 
4648
         int retval = unregister_ioctl32_conversion(i);
 
4649
         if (retval) {
 
4650
            Warning("Fail to unregister ioctl32 conversion for cmd %d\n", i);
 
4651
         }
 
4652
      }
 
4653
   }
 
4654
#endif /* !HAVE_COMPAT_IOCTL */
 
4655
}
 
4656
#else /* VM_X86_64 */
 
4657
#define register_ioctl32_handlers() (0)
 
4658
#define unregister_ioctl32_handlers() do { } while (0)
 
4659
#endif /* VM_X86_64 */
 
4660
 
 
4661
 
 
4662
/*
 
4663
 * Device operations.
 
4664
 */
 
4665
 
 
4666
 
 
4667
/*
 
4668
 *----------------------------------------------------------------------------
 
4669
 *
 
4670
 * VSockVmciDevOpen --
 
4671
 *
 
4672
 *      Invoked when the device is opened.  Simply maintains a count of open
 
4673
 *      instances.
 
4674
 *
 
4675
 * Results:
 
4676
 *      Zero on success, negative value otherwise.
 
4677
 *
 
4678
 * Side effects:
 
4679
 *      None.
 
4680
 *
 
4681
 *----------------------------------------------------------------------------
 
4682
 */
 
4683
 
 
4684
int
 
4685
VSockVmciDevOpen(struct inode *inode,  // IN
 
4686
                 struct file *file)    // IN
 
4687
{
 
4688
   spin_lock(&registrationLock);
 
4689
   devOpenCount++;
 
4690
   spin_unlock(&registrationLock);
 
4691
   return 0;
 
4692
}
 
4693
 
 
4694
 
 
4695
/*
 
4696
 *----------------------------------------------------------------------------
 
4697
 *
 
4698
 * VSockVmciDevRelease --
 
4699
 *
 
4700
 *      Invoked when the device is closed.  Updates the open instance count and
 
4701
 *      unregisters the socket family if this is the last user.
 
4702
 *
 
4703
 * Results:
 
4704
 *      Zero on success, negative value otherwise.
 
4705
 *
 
4706
 * Side effects:
 
4707
 *      None.
 
4708
 *
 
4709
 *----------------------------------------------------------------------------
 
4710
 */
 
4711
 
 
4712
int
 
4713
VSockVmciDevRelease(struct inode *inode,  // IN
 
4714
                    struct file *file)    // IN
 
4715
{
 
4716
   spin_lock(&registrationLock);
 
4717
   devOpenCount--;
 
4718
   VSockVmciTestUnregister();
 
4719
   spin_unlock(&registrationLock);
 
4720
   return 0;
 
4721
}
 
4722
 
 
4723
 
 
4724
/*
 
4725
 *----------------------------------------------------------------------------
 
4726
 *
 
4727
 * VSockVmciDevIoctl --
 
4728
 *
 
4729
 *      ioctl(2) handler.
 
4730
 *
 
4731
 * Results:
 
4732
 *      Zero on success, negative error code otherwise.
 
4733
 *
 
4734
 * Side effects:
 
4735
 *      None.
 
4736
 *
 
4737
 *----------------------------------------------------------------------------
 
4738
 */
 
4739
 
 
4740
static int
 
4741
VSockVmciDevIoctl(struct inode *inode,     // IN
 
4742
                  struct file *filp,       // IN
 
4743
                  u_int iocmd,             // IN
 
4744
                  unsigned long ioarg)     // IN/OUT
 
4745
{
 
4746
   int retval;
 
4747
 
 
4748
   retval = 0;
 
4749
 
 
4750
   switch (iocmd) {
 
4751
   case IOCTL_VMCI_SOCKETS_GET_AF_VALUE: {
 
4752
      int family;
 
4753
 
 
4754
      family = VSockVmci_GetAFValue();
 
4755
      if (family < 0) {
 
4756
         Warning("AF_VSOCK is not registered\n");
 
4757
      }
 
4758
      if (copy_to_user((void *)ioarg, &family, sizeof family) != 0) {
 
4759
         retval = -EFAULT;
 
4760
      }
 
4761
      break;
 
4762
   }
 
4763
 
 
4764
   case IOCTL_VMCI_SOCKETS_GET_LOCAL_CID: {
 
4765
      VMCIId cid = VMCI_GetContextID();
 
4766
      if (copy_to_user((void *)ioarg, &cid, sizeof cid) != 0) {
 
4767
         retval = -EFAULT;
 
4768
      }
 
4769
      break;
 
4770
   }
 
4771
 
 
4772
   default:
 
4773
      Warning("Unknown ioctl %d\n", iocmd);
 
4774
      retval = -EINVAL;
 
4775
   }
 
4776
 
 
4777
   return retval;
 
4778
}
 
4779
 
 
4780
 
 
4781
#if defined(HAVE_COMPAT_IOCTL) || defined(HAVE_UNLOCKED_IOCTL)
 
4782
/*
 
4783
 *-----------------------------------------------------------------------------
 
4784
 *
 
4785
 * VSockVmciDevUnlockedIoctl --
 
4786
 *
 
4787
 *      Wrapper for VSockVmciDevIoctl() supporting the compat_ioctl and
 
4788
 *      unlocked_ioctl methods that have signatures different from the
 
4789
 *      old ioctl. Used as compat_ioctl method for 32bit apps running
 
4790
 *      on 64bit kernel and for unlocked_ioctl on systems supporting
 
4791
 *      those.  VSockVmciDevIoctl() may safely be called without holding
 
4792
 *      the BKL.
 
4793
 *
 
4794
 * Results:
 
4795
 *      Same as VSockVmciDevIoctl().
 
4796
 *
 
4797
 * Side effects:
 
4798
 *      None.
 
4799
 *
 
4800
 *-----------------------------------------------------------------------------
 
4801
 */
 
4802
 
 
4803
static long
 
4804
VSockVmciDevUnlockedIoctl(struct file *filp,       // IN
 
4805
                          u_int iocmd,             // IN
 
4806
                          unsigned long ioarg)     // IN/OUT
 
4807
{
 
4808
   return VSockVmciDevIoctl(NULL, filp, iocmd, ioarg);
 
4809
}
 
4810
#endif
 
4811
 
 
4812
/*
 
4813
 * Module operations.
 
4814
 */
 
4815
 
 
4816
/*
 
4817
 *----------------------------------------------------------------------------
 
4818
 *
 
4819
 * VSockVmciInit --
 
4820
 *
 
4821
 *    Initialization routine for the VSockets module.
 
4822
 *
 
4823
 * Results:
 
4824
 *    Zero on success, error code on failure.
 
4825
 *
 
4826
 * Side effects:
 
4827
 *    The VSocket protocol family and socket operations are registered.
 
4828
 *
 
4829
 *----------------------------------------------------------------------------
 
4830
 */
 
4831
 
 
4832
static int __init
 
4833
VSockVmciInit(void)
 
4834
{
 
4835
   int err;
 
4836
 
 
4837
   DriverLog_Init("VSock");
 
4838
 
 
4839
   request_module("vmci");
 
4840
 
 
4841
   err = misc_register(&vsockVmciDevice);
 
4842
   if (err) {
 
4843
      return -ENOENT;
 
4844
   }
 
4845
 
 
4846
   err = register_ioctl32_handlers();
 
4847
   if (err) {
 
4848
      misc_deregister(&vsockVmciDevice);
 
4849
      return err;
 
4850
   }
 
4851
 
 
4852
   err = VSockVmciRegisterProto();
 
4853
   if (err) {
 
4854
      Warning("Cannot register vsock protocol.\n");
 
4855
      unregister_ioctl32_handlers();
 
4856
      misc_deregister(&vsockVmciDevice);
 
4857
      return err;
 
4858
   }
 
4859
 
 
4860
   VSockVmciInitTables();
 
4861
   return 0;
 
4862
}
 
4863
 
 
4864
 
 
4865
/*
 
4866
 *----------------------------------------------------------------------------
 
4867
 *
 
4868
 * VSocketVmciExit --
 
4869
 *
 
4870
 *    VSockets module exit routine.
 
4871
 *
 
4872
 * Results:
 
4873
 *    None.
 
4874
 *
 
4875
 * Side effects:
 
4876
 *    Unregisters VSocket protocol family and socket operations.
 
4877
 *
 
4878
 *----------------------------------------------------------------------------
 
4879
 */
 
4880
 
 
4881
static void __exit
 
4882
VSockVmciExit(void)
 
4883
{
 
4884
   unregister_ioctl32_handlers();
 
4885
   misc_deregister(&vsockVmciDevice);
 
4886
   spin_lock(&registrationLock);
 
4887
   VSockVmciUnregisterAddressFamily();
 
4888
   spin_unlock(&registrationLock);
 
4889
 
 
4890
   VSockVmciUnregisterProto();
 
4891
}
 
4892
 
 
4893
 
 
4894
module_init(VSockVmciInit);
 
4895
module_exit(VSockVmciExit);
 
4896
 
 
4897
MODULE_AUTHOR("VMware, Inc.");
 
4898
MODULE_DESCRIPTION("VMware Virtual Socket Family");
 
4899
MODULE_VERSION(VSOCK_DRIVER_VERSION_STRING);
 
4900
MODULE_LICENSE("GPL v2");