summaryrefslogtreecommitdiff
path: root/open-vm-tools/lib/netUtil/netUtilLinux.c
blob: 399caefabcc416afdddd4b5aa453bbcf0b04c490 (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
/*********************************************************
 * Copyright (C) 1998 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.
 *
 *********************************************************/


/*
 * netUtilLinux.c --
 *
 *    Network routines for all guest applications.
 *
 *    Linux implementation
 *
 */

#ifndef VMX86_DEVEL

#endif


#if !defined(__linux__) && !defined(__FreeBSD__) && !defined(sun) && !defined(__APPLE__)
#   error This file should not be compiled
#endif


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/times.h>
#include <netdb.h>
#ifdef sun
# include <sys/sockio.h>
#endif

#include <sys/types.h>
#include <sys/socket.h>
/* <netinet/in.h> must precede <arpa/in.h> for FreeBSD to compile. */
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <net/if_arp.h>         // for ARPHRD_ETHER

#if defined(__FreeBSD__) || defined(__APPLE__)
#include "ifaddrs.h"
#endif

#include "vm_assert.h"
#include "netutil.h"
#include "debug.h"
#include "guestApp.h"
#include "util.h"
#include "str.h"

#define MAX_IFACES      4
#define LOOPBACK        "lo"    // XXX: We would have a problem with something like "loa0".
#ifndef INET_ADDRSTRLEN
#define INET_ADDRSTRLEN 16
#endif


/*
 *-----------------------------------------------------------------------------
 *
 * ValidateConvertAddress --
 *
 *      Helper routine validates an address as a return value for
 *      NetUtil_GetPrimaryIP.
 *
 * Results:
 *      Returns TRUE with sufficient result stored in outputBuffer on success.
 *      Returns FALSE with "" stored in outputBuffer on failure.
 *
 * Side effects:
 *      None.
 *
 *-----------------------------------------------------------------------------
 */

static Bool
ValidateConvertAddress(const char *ifaceName,           // IN: interface name
                       const struct sockaddr_in *addr,  // IN: network address to
                                                        //     evaluate
                       char ipstr[INET_ADDRSTRLEN])     // OUT: converted address
                                                        //      stored here
{
   /*
    * 1.  Ensure this isn't a loopback device.
    * 2.  Ensure this is an (IPv4) internet address.
    */
   if (ifaceName[0] == '\0' ||
       strncmp(ifaceName, LOOPBACK, sizeof LOOPBACK - 1) == 0 ||
       addr->sin_family != AF_INET) {
      goto invalid;
   }

   /*
    * Branches separated because it just looked really silly to lump the
    * initial argument checking and actual conversion logic together.
    */

   /*
    * 3.  Attempt network to presentation conversion.
    * 4.  Ensure the IP isn't all zeros.
    */
   if (inet_ntop(AF_INET, (void *)&addr->sin_addr, ipstr, INET_ADDRSTRLEN) != NULL &&
       strcmp(ipstr, "0.0.0.0") != 0) {
      return TRUE;
   }

invalid:
   ipstr[0] = '\0';
   return FALSE;
}


/*
 *----------------------------------------------------------------------
 *
 * NetUtil_GetPrimaryIP --
 *
 *      Get the primary IP for this machine.
 *
 * Results:
 *      If applicable address found, returns string of said IP address.
 *      If applicable address not found, returns an empty string.
 *      If an error occurred, returns NULL.
 *
 * Side effects:
 *	Caller is responsible for free()ing returned string.
 *
 *----------------------------------------------------------------------
 */

#if !defined(__FreeBSD__) && !defined(__APPLE__) /* { */
char *
NetUtil_GetPrimaryIP(void)
{
   int sd, i;
   struct ifconf iflist;
   struct ifreq ifaces[MAX_IFACES];
   char ipstr[INET_ADDRSTRLEN] = "";

   /* Get a socket descriptor to give to ioctl(). */
   sd = socket(PF_INET, SOCK_STREAM, 0);
   if (sd < 0) {
      return NULL;
   }

   memset(&iflist, 0, sizeof iflist);
   memset(ifaces, 0, sizeof ifaces);

   /* Tell ioctl where to write interface list to and how much room it has. */
   iflist.ifc_req = ifaces;
   iflist.ifc_len = sizeof ifaces;

   if (ioctl(sd, SIOCGIFCONF, &iflist) < 0) {
      close(sd);
      return NULL;
   }

   close(sd);

   /* Loop through the list of interfaces provided by ioctl(). */
   for (i = 0; i < (sizeof ifaces/sizeof *ifaces); i++) {
      if (ValidateConvertAddress(ifaces[i].ifr_name,
                                 (struct sockaddr_in *)&ifaces[i].ifr_addr,
                                 ipstr)) {
         break;
      }
   }

   /* Success.  Here, caller, you can throw this away. */
   return strdup(ipstr);
}

#else /* } FreeBSD || APPLE { */

char *
NetUtil_GetPrimaryIP(void)
{
   struct ifaddrs *ifaces;
   struct ifaddrs *curr;
   char ipstr[INET_ADDRSTRLEN] = "";

   /*
    * getifaddrs(3) creates a NULL terminated linked list of interfaces for us
    * to traverse and places a pointer to it in ifaces.
    */
   if (getifaddrs(&ifaces) < 0) {
      return NULL;
   }

   /*
    * We traverse the list until there are no more interfaces or we have found
    * the primary interface.  This function defines the primary interface to be
    * the first non-loopback, internet interface in the interface list.
    */
   for(curr = ifaces; curr != NULL; curr = curr->ifa_next) {
      if (ValidateConvertAddress(curr->ifa_name,
                                 (struct sockaddr_in *)curr->ifa_addr,
                                 ipstr)) {
         break;
      }
   }

   /* Tell FreeBSD to free our linked list. */
   freeifaddrs(ifaces);

   /* Success.  Here, caller, you can throw this away. */
   return strdup(ipstr);
}
#endif /* } */


/*
 *----------------------------------------------------------------------
 *
 * NetUtil_GetPrimaryNic --
 *
 *      Get the primary Nic entry for this machine. Primary Nic is the 
 *      first interface that comes up when you do a ifconfig.
 *
 * Results:
 *      The primary NIC entry or NULL if an error occurred. In nicEntry
 *      returned, only IP address is retuend. All other fields remain zero. 
 *
 * Side effects:
 *      Memory is allocated for the returned NicEntry. Caller is 
 *      supposed to free it after use.
 *
 *----------------------------------------------------------------------
 */

GuestNic *
NetUtil_GetPrimaryNic(void)
{
   GuestNic *nicEntry = NULL;
   VmIpAddress *ip;
   char *ipstr;

   ipstr = NetUtil_GetPrimaryIP();
   if (NULL == ipstr) {
      goto abort;
   }

   nicEntry = Util_SafeCalloc(1, sizeof *nicEntry);
   ip = Util_SafeCalloc(1, sizeof *ip);

   nicEntry->ips.ips_len = 1;
   nicEntry->ips.ips_val = ip;

   Str_Strcpy(ip->ipAddress, ipstr, sizeof ip->ipAddress);
   free(ipstr);

abort:
   return nicEntry;
}


#if defined(linux)
#   ifdef DUMMY_NETUTIL
/*
 *-----------------------------------------------------------------------------
 *
 * NetUtil_GetIfIndex (dummy version) --
 *
 *      Given an interface name, return its index.
 *
 * Results:
 *      Returns a valid interface index on success or -1 on failure.
 *
 * Side effects:
 *      None.
 *
 *-----------------------------------------------------------------------------
 */

int
NetUtil_GetIfIndex(const char *ifName)  // IN: interface name
{
   NetUtilIfTableEntry *iterator;
   int ifIndex = -1;

   ASSERT(netUtilIfTable != NULL);

   for (iterator = netUtilIfTable; iterator->ifName; iterator++) {
      if (strcmp(iterator->ifName, ifName) == 0) {
         ifIndex = iterator->ifIndex;
         break;
      }
   }

   return ifIndex;
}


/*
 *-----------------------------------------------------------------------------
 *
 * NetUtil_GetIfName (dummy version) --
 *
 *      Given an interface index, return its name.
 *
 * Results:
 *      Returns a valid interface name on success, NULL on failure.
 *
 * Side effects:
 *      Caller is responsible for freeing the returned string.
 *
 *-----------------------------------------------------------------------------
 */

char *
NetUtil_GetIfName(int ifIndex)  // IN: interface index
{
   NetUtilIfTableEntry *iterator;
   char *ifName = NULL;

   ASSERT(netUtilIfTable != NULL);

   for (iterator = netUtilIfTable; iterator->ifName; iterator++) {
      if (iterator->ifIndex == ifIndex) {
         ifName = Util_SafeStrdup(iterator->ifName);
         break;
      }
   }

   return ifName;
}


#   else // ifdef DUMMY_NETUTIL
/*
 *-----------------------------------------------------------------------------
 *
 * NetUtil_GetIfIndex --
 *
 *      Given an interface name, return its index.
 *
 * Results:
 *      Returns a valid interface index on success or -1 on failure.
 *
 * Side effects:
 *      None.
 *
 *-----------------------------------------------------------------------------
 */

int
NetUtil_GetIfIndex(const char *ifName)  // IN: interface name
{
   struct ifreq ifreq;
   int fd = -1;
   int ifIndex = -1;

   memset(&ifreq, 0, sizeof ifreq);
   if (Str_Snprintf(ifreq.ifr_name, sizeof ifreq.ifr_name, "%s", ifName) == -1) {
      return -1;
   }

   if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
      return -1;
   }

   if (ioctl(fd, SIOCGIFINDEX, &ifreq) == 0) {
      ifIndex = ifreq.ifr_ifindex;
   }

   close(fd);

   return ifIndex;
}


/*
 *-----------------------------------------------------------------------------
 *
 * NetUtil_GetIfName --
 *
 *      Given an interface index, return its name.
 *
 * Results:
 *      Returns a valid interface name on success, NULL on failure.
 *
 * Side effects:
 *      Caller is responsible for freeing the returned string.
 *
 *-----------------------------------------------------------------------------
 */

char *
NetUtil_GetIfName(int ifIndex)  // IN: interface index
{
   struct ifreq ifreq;
   char *ifName = NULL;
   int fd = -1;

   memset(&ifreq, 0, sizeof ifreq);
   ifreq.ifr_ifindex = ifIndex;

   if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
      return NULL;
   }

   if (ioctl(fd, SIOCGIFNAME, &ifreq) == 0) {
      ifName = Util_SafeStrdup(ifreq.ifr_name);
   }

   close(fd);

   return ifName;
}
#   endif // ifdef DUMMY_NETUTIL


/*
 *-----------------------------------------------------------------------------
 *
 * NetUtil_GetHardwareAddress --
 *
 *      Given an interface name, return its hardware/link layer address.
 *
 * Results:
 *      Returns TRUE and populates hwAddr on success.
 *      Returns FALSE on failure.
 *
 * Side effects:
 *      None.
 *
 *-----------------------------------------------------------------------------
 */

size_t
NetUtil_GetHardwareAddress(int ifIndex,         // IN
                           char *hwAddr,        // OUT
                           size_t hwAddrSize,   // IN
                           IanaIfType *ifType)  // OUT
{
   struct ifreq ifreq;
   int fd = -1;
   size_t ret = 0;

   if (hwAddrSize < IFHWADDRLEN) {
      return FALSE;
   }

   ASSERT(sizeof ifreq.ifr_name >= IF_NAMESIZE);

   memset(&ifreq, 0, sizeof ifreq);
   if (if_indextoname(ifIndex, ifreq.ifr_name) == NULL) {
      return FALSE;
   }

   if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
      return FALSE;
   }

   if (ioctl(fd, SIOCGIFHWADDR, &ifreq) == 0 &&
       ifreq.ifr_hwaddr.sa_family == ARPHRD_ETHER) {
      memcpy(hwAddr, ifreq.ifr_hwaddr.sa_data, IFHWADDRLEN);
      *ifType = IANA_IFTYPE_ETHERNETCSMACD;
      ret = IFHWADDRLEN;
   }

   close(fd);

   return ret;
}


#endif // if defined(linux)