This file is indexed.

/usr/src/open-vm-tools-10.0.7/vsock/linux/vsockAddr.c is in open-vm-tools-dkms 2:10.0.7-3227872-2ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
/*********************************************************
 * Copyright (C) 2007 VMware, Inc. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation version 2 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 GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU 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
 *
 *********************************************************/

/*
 * vsockAddr.c --
 *
 *    VSockets address implementation.
 */

/*
 * These includes come before vsockCommon.h to ensure that VMware's ASSERT
 * macro is used instead of Linux's irda.h definition.
 */
#if defined(linux) && !defined(VMKERNEL)
#  if defined(__KERNEL__)
#    include "driver-config.h"
#    include <linux/socket.h>
#    include "compat_sock.h"
#  else
#    include <string.h>
#    include <errno.h>
#  endif
#elif defined(VMKERNEL)
# include "vm_libc.h"
# include "return_status.h"
#elif defined(__APPLE__)
# include <sys/errno.h>
#endif

#include "vsockCommon.h"


/*
 *-----------------------------------------------------------------------------
 *
 * VSockAddr_Init --
 *
 *      Initialize the given address with the given context id and port. This
 *      will clear the address, set the correct family, and add the given
 *      values.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      None.
 *
 *-----------------------------------------------------------------------------
 */

void
VSockAddr_Init(struct sockaddr_vm *addr, // OUT
               uint32 cid,               // IN
               uint32 port)              // IN
{
   ASSERT(addr);
   VSockAddr_InitNoFamily(addr, cid, port);
   addr->svm_family = VMCISockGetAFValueInt();
   VSOCK_ADDR_ASSERT(addr);
}


/*
 *-----------------------------------------------------------------------------
 *
 * VSockAddr_InitNoFamily --
 *
 *      Initialize the given address with the given context id and port. This
 *      will clear the address and add the given values, but not set the
 *      family.  Note that this is needed because in some places we don't want
 *      to re-register the address family in the Linux kernel and all we need
 *      is to check the context id and port.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      None.
 *
 *-----------------------------------------------------------------------------
 */

void
VSockAddr_InitNoFamily(struct sockaddr_vm *addr, // OUT
                       uint32 cid,               // IN
                       uint32 port)              // IN
{
   ASSERT(addr);

   memset(addr, 0, sizeof *addr);
#if defined(__APPLE__)
   addr->svm_len = sizeof *addr;
#endif
   addr->svm_cid = cid;
   addr->svm_port = port;
   VSOCK_ADDR_NOFAMILY_ASSERT(addr);
}


/*
 *-----------------------------------------------------------------------------
 *
 * VSockAddr_Validate --
 *
 *      Try to validate the given address.  The address must not be null and
 *      must have the correct address family.  Any reserved fields must be
 *      zero.
 *
 * Results:
 *      0 on success, EFAULT if the address is null, EAFNOSUPPORT if the
 *      address is of the wrong family, and EINVAL if the reserved fields are
 *      not zero.
 *
 * Side effects:
 *      None.
 *
 *-----------------------------------------------------------------------------
 */

int32
VSockAddr_Validate(const struct sockaddr_vm *addr) // IN
{
   int32 err;

   if (NULL == addr) {
      err = EFAULT;
      goto exit;
   }

   if (VMCISockGetAFValueInt() != addr->svm_family) {
      err = EAFNOSUPPORT;
      goto exit;
   }

   if (0 != addr->svm_zero[0]) {
      err = EINVAL;
      goto exit;
   }

   err = 0;

exit:
   return sockerr2err(err);
}


/*
 *-----------------------------------------------------------------------------
 *
 * VSockAddr_ValidateNoFamily --
 *
 *      Try to validate the given address.  The address must not be null and
 *      any reserved fields must be zero, but the address family is not
 *      checked.  Note that this is needed because in some places we don't want
 *      to re-register the address family with the Linux kernel.
 *
 *      Also note that we duplicate the code from _Validate() since we want to
 *      retain the ordering or the error return values.
 *
 * Results:
 *      0 on success, EFAULT if the address is null and EINVAL if the reserved
 *      fields are not zero.
 *
 * Side effects:
 *      None.
 *
 *-----------------------------------------------------------------------------
 */

int32
VSockAddr_ValidateNoFamily(const struct sockaddr_vm *addr) // IN
{
   int32 err;

   if (NULL == addr) {
      err = EFAULT;
      goto exit;
   }

   if (0 != addr->svm_zero[0]) {
      err = EINVAL;
      goto exit;
   }

   err = 0;

exit:
   return sockerr2err(err);
}


/*
 *----------------------------------------------------------------------------
 *
 * VSockAddr_Bound --
 *
 *    Determines whether the provided address is bound.
 *
 * Results:
 *    TRUE if the address structure is bound, FALSE otherwise.
 *
 * Side effects:
 *    None.
 *
 *----------------------------------------------------------------------------
 */

Bool
VSockAddr_Bound(struct sockaddr_vm *addr) // IN: socket address to check
{
   ASSERT(addr);
   return addr->svm_port != VMADDR_PORT_ANY;
}


/*
 *----------------------------------------------------------------------------
 *
 * VSockAddr_Unbind --
 *
 *    Unbind the given addresss.
 *
 * Results:
 *    None.
 *
 * Side effects:
 *    None.
 *
 *----------------------------------------------------------------------------
 */

void
VSockAddr_Unbind(struct sockaddr_vm *addr) // IN
{
   VSockAddr_Init(addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
}


/*
 *----------------------------------------------------------------------------
 *
 * VSockAddr_EqualsAddr --
 *
 *    Determine if the given addresses are equal.
 *
 * Results:
 *    TRUE if the addresses are equal, FALSE otherwise.
 *
 * Side effects:
 *    None.
 *
 *----------------------------------------------------------------------------
 */

Bool
VSockAddr_EqualsAddr(struct sockaddr_vm *addr,  // IN
                     struct sockaddr_vm *other) // IN
{
   /*
    * XXX We don't ASSERT on the family here since this is used on the receive
    * path in Linux and we don't want to re-register the address family
    * unnecessarily.
    */
   VSOCK_ADDR_NOFAMILY_ASSERT(addr);
   VSOCK_ADDR_NOFAMILY_ASSERT(other);
   return (addr->svm_cid == other->svm_cid &&
           addr->svm_port == other->svm_port);
}


/*
 *----------------------------------------------------------------------------
 *
 * VSockAddr_EqualsHandlePort --
 *
 *    Determines if the given address matches the given handle and port.
 *
 * Results:
 *    TRUE if the address matches the handle and port, FALSE otherwise.
 *
 * Side effects:
 *    None.
 *
 *----------------------------------------------------------------------------
 */

Bool
VSockAddr_EqualsHandlePort(struct sockaddr_vm *addr, // IN
                           VMCIHandle handle,        // IN
                           uint32 port)              // IN
{
   VSOCK_ADDR_ASSERT(addr);
   return (addr->svm_cid == VMCI_HANDLE_TO_CONTEXT_ID(handle) &&
           addr->svm_port == port);
}


/*
 *-----------------------------------------------------------------------------
 *
 * VSockAddr_Cast --
 *
 *      Try to cast the given generic address to a VM address.  The given
 *      length must match that of a VM address and the address must be valid.
 *      The "outAddr" parameter contains the address if successful.
 *
 * Results:
 *      0 on success, EFAULT if the length is too small.  See
 *      VSockAddr_Validate() for other possible return codes.
 *
 * Side effects:
 *      None.
 *
 *-----------------------------------------------------------------------------
 */

int32
VSockAddr_Cast(const struct sockaddr *addr,  // IN
               int32 len,                    // IN
               struct sockaddr_vm **outAddr) // OUT
{
   int32 err;

   ASSERT(outAddr);

   if (len < sizeof **outAddr) {
      err = EFAULT;
      goto exit;
   }

   *outAddr = (struct sockaddr_vm *) addr;
   err = VSockAddr_Validate(*outAddr);

exit:
   return sockerr2err(err);
}


/*
 *----------------------------------------------------------------------------
 *
 * VSockAddr_SocketContextStream --
 *
 *      Determines whether the provided context id represents a context that
 *      contains a stream socket endpoints.
 *
 * Results:
 *      TRUE if the context does have socket endpoints, FALSE otherwise.
 *
 * Side effects:
 *      None.
 *
 *----------------------------------------------------------------------------
 */

Bool
VSockAddr_SocketContextStream(uint32 cid)  // IN
{
   uint32 i;
   VMCIId nonSocketContexts[] = {
      VMCI_WELL_KNOWN_CONTEXT_ID,
   };

   ASSERT_ON_COMPILE(sizeof cid == sizeof *nonSocketContexts);

   for (i = 0; i < ARRAYSIZE(nonSocketContexts); i++) {
      if (cid == nonSocketContexts[i]) {
         return FALSE;
      }
   }

   return TRUE;
}


/*
 *----------------------------------------------------------------------------
 *
 * VSockAddr_SocketContextDgram --
 *
 *      Determines whether the provided <context id, resource id> represent
 *      a protected datagram endpoint.
 *
 * Results:
 *      TRUE if the context does have socket endpoints, FALSE otherwise.
 *
 * Side effects:
 *      None.
 *
 *----------------------------------------------------------------------------
 */

Bool
VSockAddr_SocketContextDgram(uint32 cid,  // IN
                             uint32 rid)  // IN
{
   if (cid == VMCI_HYPERVISOR_CONTEXT_ID) {
      /*
       * Registrations of PBRPC Servers do not modify VMX/Hypervisor state and
       * are allowed.
       */
      if (rid == VMCI_UNITY_PBRPC_REGISTER) {
         return TRUE;
      } else {
         return FALSE;
      }
   }

   return TRUE;
}