summaryrefslogtreecommitdiff
path: root/open-vm-tools/lib/hgfsServerManagerGuest/hgfsChannelGuestBd.c
blob: 72c9905336eced73ec7051f2c40d46f216e9f013 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
/*********************************************************
 * Copyright (C) 2010 VMware, Inc. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation version 2.1 and no later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the Lesser GNU General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA.
 *
 *********************************************************/

/*
 * hgfsChannel.c --
 *
 *    Channel abstraction for the HGFS server.
 *
 */

#include <stdlib.h>
#include "vm_assert.h"
#include "vm_atomic.h"
#include "util.h"
#include "debug.h"
#include "hgfsChannelGuestInt.h"
#include "hgfsServer.h"
#include "hgfsServerManager.h"

typedef enum {
   HGFS_GST_CONN_UNINITIALIZED,
   HGFS_GST_CONN_NOTCONNECTED,
   HGFS_GST_CONN_CONNECTED,
} HgfsGuestConnState;


/* Since there is only one connection we use globals. */
typedef struct HgfsGuestConn {
   Atomic_uint32 refCount;                   /* Reference count. */
   HgfsGuestConnState state;
   HgfsServerSessionCallbacks *serverCbTable; /* Server session callbacks. */
   HgfsServerChannelCallbacks channelCbTable;
   void *serverSession;
   size_t packetOutLen;
   unsigned char *clientPacketOut;                 /* Client supplied buffer. */
   unsigned char packetOut[HGFS_LARGE_PACKET_MAX]; /* For RPC msg callbacks. */
} HgfsGuestConn;


/* Callback functions. */
static Bool HgfsChannelGuestBdInit(HgfsServerSessionCallbacks *serverCBTable,
                                   void *rpc,
                                   void *rpcCallback,
                                   HgfsGuestConn **connection);
static void HgfsChannelGuestBdExit(HgfsGuestConn *data);
static Bool HgfsChannelGuestBdSend(void *data,
                                   HgfsPacket *packet,
                                   HgfsSendFlags flags);
static Bool HgfsChannelGuestBdReceive(HgfsGuestConn *data,
                                      char const *packetIn,
                                      size_t packetInSize,
                                      char *packetOut,
                                      size_t *packetOutSize);
static uint32 HgfsChannelGuestBdInvalidateInactiveSessions(HgfsGuestConn *data);

HgfsGuestChannelCBTable gGuestBackdoorOps = {
   HgfsChannelGuestBdInit,
   HgfsChannelGuestBdExit,
   HgfsChannelGuestBdReceive,
   HgfsChannelGuestBdInvalidateInactiveSessions,
};

/* Private functions. */
static Bool HgfsChannelGuestConnConnect(HgfsGuestConn *connData);
static void HgfsChannelGuestConnDestroy(HgfsGuestConn *connData);
static Bool HgfsChannelGuestReceiveInternal(HgfsGuestConn *connData,
                                            char const *packetIn,
                                            size_t packetInSize,
                                            char *packetOut,
                                            size_t *packetOutSize);


/*
 *----------------------------------------------------------------------------
 *
 * CONNECTION DATA FUNCTIONS
 *
 *----------------------------------------------------------------------------
 */


/*
 *----------------------------------------------------------------------------
 *
 * HgfsChannelGuestConnGet --
 *
 *      Increment connection reference count.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      None.
 *
 *----------------------------------------------------------------------------
 */

static void
HgfsChannelGuestConnGet(HgfsGuestConn *connData)   // IN: connection
{
   ASSERT(connData);
   Atomic_Inc(&connData->refCount);
}


/*
 *----------------------------------------------------------------------------
 *
 * HgfsChannelGuestConnPut --
 *
 *      Decrement connection reference count.
 *
 *      Free connection data if this is the last reference.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      None.
 *
 *----------------------------------------------------------------------------
 */

static void
HgfsChannelGuestConnPut(HgfsGuestConn *connData)   // IN: connection
{
   ASSERT(connData);
   if (Atomic_FetchAndDec(&connData->refCount) == 1) {
      HgfsChannelGuestConnDestroy(connData);
   }
}


/*
 *-----------------------------------------------------------------------------
 *
 * HgfsChannelGuestConnInit --
 *
 *      Initializes the connection.
 *
 * Results:
 *      TRUE always and the channel initialized.
 *
 * Side effects:
 *      None.
 *
 *-----------------------------------------------------------------------------
 */

static Bool
HgfsChannelGuestConnInit(HgfsGuestConn **connData,                   // IN/OUT: channel object
                         HgfsServerSessionCallbacks *serverCBTable)  // IN: server callbacks
{
   HgfsGuestConn *conn = NULL;

   conn = Util_SafeCalloc(1, sizeof *conn);

   /* Give ourselves a reference of one. */
   HgfsChannelGuestConnGet(conn);
   conn->serverCbTable = serverCBTable;
   conn->state = HGFS_GST_CONN_NOTCONNECTED;

   *connData = conn;
   return TRUE;
}


/*
 *-----------------------------------------------------------------------------
 *
 * HgfsChannelGuestConnExit --
 *
 *      Teardown the connection.
 *
 *      Removes the reference and if it is the last will cause the connection
 *      to be destroyed.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      None.
 *
 *-----------------------------------------------------------------------------
 */

static void
HgfsChannelGuestConnExit(HgfsGuestConn *connData) // IN/OUT: channel object
{
   connData->state = HGFS_GST_CONN_UNINITIALIZED;

   HgfsChannelGuestConnPut(connData);
}


/*
 *-----------------------------------------------------------------------------
 *
 * HgfsChannelGuestConnDestroy --
 *
 *      Destroy the connection.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      None.
 *
 *-----------------------------------------------------------------------------
 */

static void
HgfsChannelGuestConnDestroy(HgfsGuestConn *connData) // IN/OUT: channel object
{
   /* Make sure the server closes it's own session data. */
   if (NULL != connData->serverSession) {
      connData->serverCbTable->close(connData->serverSession);
      connData->serverSession = NULL;
   }
   free(connData);
}


/*
 *-----------------------------------------------------------------------------
 *
 * HgfsChannelGuestConnCreate --
 *
 *      Create's the RPC connection for the HGFS guest if asked.
 *
 *      Create the pseudo connection for the guest - state transition.
 *      (See the comment in the function where the RPC initialization
 *      is expected to be added.
 *      This entails is registering our callback to receive messages for the
 *      connection object passed. We will have the ability to receive
 *      requests until we unregister our callback.)
 *
 *      NOTE: There is only handler and connction that can be used for
 *      all HGFS guest requests.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      None.
 *
 *-----------------------------------------------------------------------------
 */

static void
HgfsChannelGuestConnCreate(HgfsGuestConn *connData,      // IN: connection
                           void *rpc,                    // IN: Rpc channel unused
                           void *rpcCallback)            // IN: Rpc callback unused
{
   ASSERT(connData->state == HGFS_GST_CONN_NOTCONNECTED);

   /*
    * Rpc may be NULL for some cases. For example, if we
    * just need to provide an HGFS server connection
    * not associated with an HGFS only RPC connection.
    */
   if (connData->state == HGFS_GST_CONN_NOTCONNECTED) {

      /* XXX - Here is where we would register an RPC callback if required. */

      connData->state = HGFS_GST_CONN_CONNECTED;
      HgfsChannelGuestConnGet(connData);
   }
}


/*
 *-----------------------------------------------------------------------------
 *
 * HgfsChannelGuestConnClose --
 *
 *      Closes the connection for the HGFS guest.
 *
 *      If required unregisters the callback will prevent us from
 *      receiving any more requests closing the connection.
 *
 * Results:
 *      TRUE if closed, FALSE if was not connected.
 *
 * Side effects:
 *      None.
 *
 *-----------------------------------------------------------------------------
 */

static Bool
HgfsChannelGuestConnClose(HgfsGuestConn *connData,    // IN: Connection
                          void *rpc,                  // IN: Rpc channel unused
                          void *rpcCallback)          // IN: Rpc callback unused
{
   Bool result = FALSE;

   if (connData->state == HGFS_GST_CONN_CONNECTED) {
      /* XXX - Here is where we would unregister an RPC callback. */

      /* Clear the connection object since we are unregistered. */
      connData->state = HGFS_GST_CONN_NOTCONNECTED;
      HgfsChannelGuestConnPut(connData);
      result = TRUE;
   }
   return result;
}


/*
 *-----------------------------------------------------------------------------
 *
 * HgfsChannelGuestConnConnect --
 *
 *      Send connection to the server.
 *
 * Results:
 *      TRUE if server returns a data object, FALSE if not.
 *
 * Side effects:
 *      None
 *
 *-----------------------------------------------------------------------------
 */

static Bool
HgfsChannelGuestConnConnect(HgfsGuestConn *connData)  // IN: our connection data
{
   Bool result;
   static HgfsServerChannelData HgfsBdCapData = {
      0,
      HGFS_LARGE_PACKET_MAX
   };

   connData->channelCbTable.getWriteVa = NULL;
   connData->channelCbTable.getReadVa = NULL;
   connData->channelCbTable.putVa = NULL;
   connData->channelCbTable.send = HgfsChannelGuestBdSend;
   result = connData->serverCbTable->connect(connData,
                                             &connData->channelCbTable,
                                             &HgfsBdCapData,
                                             &connData->serverSession);
   if (result) {
      HgfsChannelGuestConnGet(connData);
   }
   return result;
}


/*
 *-----------------------------------------------------------------------------
 *
 * HgfsChannelGuestConnDisconnect --
 *
 *      Send disconnect to the server.
 *
 *      NOTE: The server data will be maintained until
 *      the connection is totally closed (last reference is gone).
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      None.
 *
 *-----------------------------------------------------------------------------
 */

static void
HgfsChannelGuestConnDisconnect(HgfsGuestConn *connData)  // IN: connection
{
   if (connData->serverSession != NULL) {
      /* Tell the server to to disconnect the session. */
      connData->serverCbTable->disconnect(connData->serverSession);
      HgfsChannelGuestConnPut(connData);
   }
}


/*
 *----------------------------------------------------------------------------
 *
 * HgfsChannelGuestConnCloseInternal --
 *
 *      Close the client and send a disconnect to the server for the session.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      Closes the client connection and empties the queues.
 *
 *----------------------------------------------------------------------------
 */

static void
HgfsChannelGuestConnCloseInternal(HgfsGuestConn *connData, // IN: Connection data
                                  void *rpc,               // IN: Rpc channel unused
                                  void *rpcCallback)       // IN: Rpc callback unused
{
   /* Close (unregister the backdoor RPC) connection. */
   if (HgfsChannelGuestConnClose(connData, rpc, rpcCallback)) {
      /* Disconnect the connection from the server. */
      HgfsChannelGuestConnDisconnect(connData);
   }
}


/*
 *----------------------------------------------------------------------------
 *
 * HgfsChannelGuestReceiveInternal --
 *
 *    Process packet not associated with any session.
 *
 *    This function is used in the HGFS server inside Tools.
 *
 *    Create an internal session if not already created, and process the packet.
 *
 * Results:
 *    TRUE if received packet ok and processed, FALSE otherwise.
 *
 * Side effects:
 *    None
 *
 *----------------------------------------------------------------------------
 */

static Bool
HgfsChannelGuestReceiveInternal(HgfsGuestConn *connData,  // IN: connection
                                char const *packetIn,     // IN: incoming packet
                                size_t packetInSize,      // IN: incoming packet size
                                char *packetOut,          // OUT: outgoing packet
                                size_t *packetOutSize)    // IN/OUT: outgoing packet size
{
   HgfsPacket packet;

   ASSERT(packetIn);
   ASSERT(packetOut);
   ASSERT(packetOutSize);

   if (connData->state == HGFS_GST_CONN_UNINITIALIZED) {
      /* The connection was closed as we are exiting, so bail. */
      *packetOutSize = 0;
      return FALSE;
   }

   /* This is just a ping, return nothing. */
   if (*packetOutSize == 0) {
      return TRUE;
   }

   /*
    * Create the session if not already created.
    * This session is destroyed in HgfsServer_ExitState.
    */
   if (connData->serverSession == NULL) {
      /* Do our guest connect now which will inform the server. */
      if (!HgfsChannelGuestConnConnect(connData)) {
         *packetOutSize = 0;
         return FALSE;
      }
   }

   memset(&packet, 0, sizeof packet);
   /* For backdoor there is only one iov */
   packet.iov[0].va = (void *)packetIn;
   packet.iov[0].len = packetInSize;
   packet.iovCount = 1;
   packet.metaPacket = (void *)packetIn;
   packet.metaPacketDataSize = packetInSize;
   packet.metaPacketSize = packetInSize;
   packet.replyPacket = packetOut;
   packet.replyPacketSize = *packetOutSize;
   packet.state |= HGFS_STATE_CLIENT_REQUEST;

   /* The server will perform a synchronous processing of requests. */
   connData->serverCbTable->receive(&packet, connData->serverSession);

   *packetOutSize = connData->packetOutLen;

   return TRUE;
}


/*
 *----------------------------------------------------------------------------
 *
 * REGISTERED CALLBACK FUNCTIONS
 *
 * XXX - Where we would have any internally registered callback routines.
 * This routine would call HgfsChannelGuestReceiveInternal to process the
 * request.
 *
 *----------------------------------------------------------------------------
 */


/*
 *----------------------------------------------------------------------------
 *
 * GUEST CHANNEL CALLBACKS
 *
 *----------------------------------------------------------------------------
 */


/*
 *----------------------------------------------------------------------------
 *
 * HgfsChannelGuestBdReceive --
 *
 *    Process packet not associated with our registered callback.
 *
 *
 * Results:
 *    TRUE if received packet ok and processed, FALSE otherwise.
 *
 * Side effects:
 *    None
 *
 *----------------------------------------------------------------------------
 */

Bool
HgfsChannelGuestBdReceive(HgfsGuestConn *connData,    // IN: connection
                          char const *packetIn,       // IN: incoming packet
                          size_t packetInSize,        // IN: incoming packet size
                          char *packetOut,            // OUT: outgoing packet
                          size_t *packetOutSize)      // IN/OUT: outgoing packet size
{
   Bool result = TRUE;

   ASSERT(NULL != packetIn);
   ASSERT(NULL != packetOut);
   ASSERT(NULL != packetOutSize);
   ASSERT(NULL != connData);

   if (NULL == connData) {
      result = FALSE;
      goto exit;
   }

   connData->packetOutLen = *packetOutSize;
   connData->clientPacketOut = packetOut;

   result = HgfsChannelGuestReceiveInternal(connData,
                                            packetIn,
                                            packetInSize,
                                            connData->clientPacketOut,
                                            packetOutSize);

   connData->clientPacketOut = NULL;
   connData->packetOutLen = sizeof connData->packetOut;

exit:
   return result;
}


/*
 *----------------------------------------------------------------------------
 *
 * HgfsChannelGuestBdInvalidateInactiveSessions --
 *
 *    Sends a request to invalidate all the inactive HGFS server sessions.
 *
 * Results:
 *    Number of active sessions remaining inside the HGFS server.
 *
 * Side effects:
 *    None
 *
 *----------------------------------------------------------------------------
 */

uint32
HgfsChannelGuestBdInvalidateInactiveSessions(HgfsGuestConn *connData)  // IN: connection
{
   ASSERT(NULL != connData);

   if (NULL == connData) {
      return 0;
   }

   if (connData->state == HGFS_GST_CONN_UNINITIALIZED) {
      /* The connection was closed as we are exiting, so bail. */
      return 0;
   }

   /* The server will perform a synchronous processing of requests. */
   if (connData->serverSession) {
      return connData->serverCbTable->invalidateInactiveSessions(connData->serverSession);
   }

   return 0;
}


/*
 *-----------------------------------------------------------------------------
 *
 * HgfsChannelGuestBdSend --
 *
 *      Send reply to the request
 *
 * Results:
 *      Always TRUE.
 *
 * Side effects:
 *      None
 *
 *-----------------------------------------------------------------------------
 */

static Bool
HgfsChannelGuestBdSend(void *conn,              // IN: our connection data
                       HgfsPacket *packet,      // IN/OUT: Hgfs Packet
                       HgfsSendFlags flags)     // IN: Flags to say how to process
{
   HgfsGuestConn *connData = conn;

   ASSERT(NULL != connData);
   ASSERT(NULL != packet);
   ASSERT(NULL != packet->replyPacket);
   ASSERT(packet->replyPacketDataSize <= connData->packetOutLen);
   ASSERT(packet->replyPacketSize == connData->packetOutLen);

   if (packet->replyPacketDataSize > connData->packetOutLen) {
      packet->replyPacketDataSize = connData->packetOutLen;
   }
   connData->packetOutLen = (uint32)packet->replyPacketDataSize;

   if (!(flags & HGFS_SEND_NO_COMPLETE)) {
      connData->serverCbTable->sendComplete(packet,
                                            connData->serverSession);
   }

   return TRUE;
}


/*
 *----------------------------------------------------------------------------
 *
 * HgfsChannelGuestBdInit --
 *
 *      Called from channel manager.
 *
 *      Initializes our channel connections.
 *
 * Results:
 *      Always TRUE.
 *
 * Side effects:
 *      Registers RPC call.
 *
 *----------------------------------------------------------------------------
 */

static Bool
HgfsChannelGuestBdInit(HgfsServerSessionCallbacks *serverCBTable,   // IN: server callbacks
                       void *rpc,                                   // IN: Rpc channel unused
                       void *rpcCallback,                           // IN: Rpc callback unused
                       HgfsGuestConn **connection)                  // OUT: connection object
{
   HgfsGuestConn *connData = NULL;
   Bool result;

   ASSERT(NULL != connection);

   /* Create our connection object. */
   result = HgfsChannelGuestConnInit(&connData,
                                     serverCBTable);
   if (!result) {
      Debug("%s: Error: guest connection initialized.\n", __FUNCTION__);
      goto exit;
   }

   /*
    * Create our connection now with any rpc handle and callback.
    */
   HgfsChannelGuestConnCreate(connData,
                              rpc,
                              rpcCallback);

exit:
   if (!result) {
      if (NULL != connData) {
         HgfsChannelGuestBdExit(connData);
         connData = NULL;
      }
   }
   *connection = connData;
   Debug("%s: guest initialized.\n", __FUNCTION__);
   return result;
}


/*
 *----------------------------------------------------------------------------
 *
 * HgfsChannelGuestBdExit --
 *
 *      Tearsdown our channel connections.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      Unregisters RPC call.
 *
 *----------------------------------------------------------------------------
 */

static void
HgfsChannelGuestBdExit(HgfsGuestConn *connData)
{
   ASSERT(NULL != connData);

   if (NULL != connData) {
      /* Currently no rpc to unregister. */
      HgfsChannelGuestConnCloseInternal(connData, NULL, NULL);
      HgfsChannelGuestConnExit(connData);
   }
}