VirtualBox

source: vbox/trunk/src/apps/adpctl/VBoxNetAdpCtl.cpp@ 63215

Last change on this file since 63215 was 62538, checked in by vboxsync, 8 years ago

Grumble, this is still in the wrong place.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.8 KB
Line 
1/* $Id: VBoxNetAdpCtl.cpp 62538 2016-07-22 19:32:47Z vboxsync $ */
2/** @file
3 * Apps - VBoxAdpCtl, Configuration tool for vboxnetX adapters.
4 */
5
6/*
7 * Copyright (C) 2009-2016 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19
20/*********************************************************************************************************************************
21* Header Files *
22*********************************************************************************************************************************/
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <unistd.h>
27#include <sys/wait.h>
28#include <sys/ioctl.h>
29#include <sys/stat.h>
30#include <fcntl.h>
31#ifdef RT_OS_LINUX
32# include <net/if.h>
33# include <linux/types.h>
34/* Older versions of ethtool.h rely on these: */
35typedef unsigned long long u64;
36typedef __uint32_t u32;
37typedef __uint16_t u16;
38typedef __uint8_t u8;
39# include <limits.h> /* for INT_MAX */
40# include <linux/ethtool.h>
41# include <linux/sockios.h>
42#endif
43#ifdef RT_OS_SOLARIS
44# include <sys/ioccom.h>
45#endif
46
47/** @todo Error codes must be moved to some header file */
48#define ADPCTLERR_BAD_NAME 2
49#define ADPCTLERR_NO_CTL_DEV 3
50#define ADPCTLERR_IOCTL_FAILED 4
51#define ADPCTLERR_SOCKET_FAILED 5
52
53/** @todo These are duplicates from src/VBox/HostDrivers/VBoxNetAdp/VBoxNetAdpInternal.h */
54#define VBOXNETADP_CTL_DEV_NAME "/dev/vboxnetctl"
55#define VBOXNETADP_MAX_INSTANCES 128
56#define VBOXNETADP_NAME "vboxnet"
57#define VBOXNETADP_MAX_NAME_LEN 32
58#define VBOXNETADP_CTL_ADD _IOWR('v', 1, VBOXNETADPREQ)
59#define VBOXNETADP_CTL_REMOVE _IOW('v', 2, VBOXNETADPREQ)
60typedef struct VBoxNetAdpReq
61{
62 char szName[VBOXNETADP_MAX_NAME_LEN];
63} VBOXNETADPREQ;
64typedef VBOXNETADPREQ *PVBOXNETADPREQ;
65
66#define VBOXADPCTL_IFCONFIG_PATH1 "/sbin/ifconfig"
67#define VBOXADPCTL_IFCONFIG_PATH2 "/bin/ifconfig"
68static char *g_pszIfConfig;
69
70#if defined(RT_OS_LINUX)
71# define VBOXADPCTL_DEL_CMD "del"
72# define VBOXADPCTL_ADD_CMD "add"
73#elif defined(RT_OS_SOLARIS)
74# define VBOXADPCTL_DEL_CMD "removeif"
75# define VBOXADPCTL_ADD_CMD "addif"
76#else
77# define VBOXADPCTL_DEL_CMD "delete"
78# define VBOXADPCTL_ADD_CMD "add"
79#endif
80
81static void showUsage(void)
82{
83 fprintf(stderr, "Usage: VBoxNetAdpCtl <adapter> <address> ([netmask <address>] | remove)\n");
84 fprintf(stderr, " | VBoxNetAdpCtl [<adapter>] add\n");
85 fprintf(stderr, " | VBoxNetAdpCtl <adapter> remove\n");
86}
87
88static void setPathIfConfig(void)
89{
90 struct stat s;
91 if ( !stat(VBOXADPCTL_IFCONFIG_PATH1, &s)
92 && S_ISREG(s.st_mode))
93 g_pszIfConfig = (char*)VBOXADPCTL_IFCONFIG_PATH1;
94 else
95 g_pszIfConfig = (char*)VBOXADPCTL_IFCONFIG_PATH2;
96}
97
98static int executeIfconfig(const char *pcszAdapterName, const char *pcszArg1,
99 const char *pcszArg2 = NULL,
100 const char *pcszArg3 = NULL,
101 const char *pcszArg4 = NULL,
102 const char *pcszArg5 = NULL)
103{
104 const char * const argv[] =
105 {
106 g_pszIfConfig,
107 pcszAdapterName,
108 pcszArg1, /* [address family] */
109 pcszArg2, /* address */
110 pcszArg3, /* ['netmask'] */
111 pcszArg4, /* [network mask] */
112 pcszArg5, /* [network mask] */
113 NULL /* terminator */
114 };
115 char * const envp[] = { (char*)"LC_ALL=C", NULL };
116 int rc = EXIT_SUCCESS;
117 pid_t childPid = fork();
118 switch (childPid)
119 {
120 case -1: /* Something went wrong. */
121 perror("fork() failed");
122 rc = EXIT_FAILURE;
123 break;
124 case 0: /* Child process. */
125 if (execve(argv[0], (char * const*)argv, envp) == -1)
126 rc = EXIT_FAILURE;
127 break;
128 default: /* Parent process. */
129 waitpid(childPid, &rc, 0);
130 break;
131 }
132
133 return rc;
134}
135
136#define MAX_ADDRESSES 128
137#define MAX_ADDRLEN 64
138
139static bool removeAddresses(char *pszAdapterName)
140{
141 char szBuf[1024];
142 char aszAddresses[MAX_ADDRESSES][MAX_ADDRLEN];
143 int rc;
144 int fds[2];
145 char * const argv[] = { g_pszIfConfig, pszAdapterName, NULL };
146 char * const envp[] = { (char*)"LC_ALL=C", NULL };
147
148 memset(aszAddresses, 0, sizeof(aszAddresses));
149
150 rc = pipe(fds);
151 if (rc < 0)
152 return false;
153
154 pid_t pid = fork();
155 if (pid < 0)
156 return false;
157
158 if (pid == 0)
159 {
160 /* child */
161 close(fds[0]);
162 close(STDOUT_FILENO);
163 rc = dup2(fds[1], STDOUT_FILENO);
164 if (rc >= 0)
165 execve(argv[0], argv, envp);
166 return false;
167 }
168
169 /* parent */
170 close(fds[1]);
171 FILE *fp = fdopen(fds[0], "r");
172 if (!fp)
173 return false;
174
175 int cAddrs;
176 for (cAddrs = 0; cAddrs < MAX_ADDRESSES && fgets(szBuf, sizeof(szBuf), fp);)
177 {
178 int cbSkipWS = strspn(szBuf, " \t");
179 char *pszWord = strtok(szBuf + cbSkipWS, " ");
180 /* We are concerned with IPv6 address lines only. */
181 if (!pszWord || strcmp(pszWord, "inet6"))
182 continue;
183#ifdef RT_OS_LINUX
184 pszWord = strtok(NULL, " ");
185 /* Skip "addr:". */
186 if (!pszWord || strcmp(pszWord, "addr:"))
187 continue;
188#endif
189 pszWord = strtok(NULL, " ");
190 /* Skip link-local addresses. */
191 if (!pszWord || !strncmp(pszWord, "fe80", 4))
192 continue;
193 strncpy(aszAddresses[cAddrs++], pszWord, MAX_ADDRLEN-1);
194 }
195 fclose(fp);
196
197 for (int i = 0; i < cAddrs; i++)
198 {
199 if (executeIfconfig(pszAdapterName, "inet6",
200 VBOXADPCTL_DEL_CMD, aszAddresses[i]) != EXIT_SUCCESS)
201 return false;
202 }
203
204 return true;
205}
206
207static int doIOCtl(unsigned long uCmd, VBOXNETADPREQ *pReq)
208{
209 int fd = open(VBOXNETADP_CTL_DEV_NAME, O_RDWR);
210 if (fd == -1)
211 {
212 fprintf(stderr, "VBoxNetAdpCtl: Error while %s %s: ",
213 uCmd == VBOXNETADP_CTL_REMOVE ? "removing" : "adding",
214 pReq->szName[0] ? pReq->szName : "new interface");
215 perror("failed to open " VBOXNETADP_CTL_DEV_NAME);
216 return ADPCTLERR_NO_CTL_DEV;
217 }
218
219 int rc = ioctl(fd, uCmd, pReq);
220 if (rc == -1)
221 {
222 fprintf(stderr, "VBoxNetAdpCtl: Error while %s %s: ",
223 uCmd == VBOXNETADP_CTL_REMOVE ? "removing" : "adding",
224 pReq->szName[0] ? pReq->szName : "new interface");
225 perror("VBoxNetAdpCtl: ioctl failed for " VBOXNETADP_CTL_DEV_NAME);
226 rc = ADPCTLERR_IOCTL_FAILED;
227 }
228
229 close(fd);
230
231 return rc;
232}
233
234static int checkAdapterName(const char *pcszNameIn, char *pszNameOut)
235{
236 int iAdapterIndex = -1;
237
238 if ( strlen(pcszNameIn) >= VBOXNETADP_MAX_NAME_LEN
239 || sscanf(pcszNameIn, "vboxnet%d", &iAdapterIndex) != 1
240 || iAdapterIndex < 0 || iAdapterIndex >= VBOXNETADP_MAX_INSTANCES )
241 {
242 fprintf(stderr, "VBoxNetAdpCtl: Setting configuration for '%s' is not supported.\n", pcszNameIn);
243 return ADPCTLERR_BAD_NAME;
244 }
245 sprintf(pszNameOut, "vboxnet%d", iAdapterIndex);
246 if (strcmp(pszNameOut, pcszNameIn))
247 {
248 fprintf(stderr, "VBoxNetAdpCtl: Invalid adapter name '%s'.\n", pcszNameIn);
249 return ADPCTLERR_BAD_NAME;
250 }
251
252 return 0;
253}
254
255int main(int argc, char *argv[])
256{
257 char szAdapterName[VBOXNETADP_MAX_NAME_LEN];
258 char *pszAdapterName = NULL;
259 const char *pszAddress = NULL;
260 const char *pszNetworkMask = NULL;
261 const char *pszOption = NULL;
262 int rc = EXIT_SUCCESS;
263 bool fRemove = false;
264 VBOXNETADPREQ Req;
265
266 setPathIfConfig();
267
268 switch (argc)
269 {
270 case 5:
271 {
272 /* Add a netmask to existing interface */
273 if (strcmp("netmask", argv[3]))
274 {
275 fprintf(stderr, "Invalid argument: %s\n\n", argv[3]);
276 showUsage();
277 return 1;
278 }
279 pszOption = "netmask";
280 pszNetworkMask = argv[4];
281 pszAdapterName = argv[1];
282 pszAddress = argv[2];
283 break;
284 }
285
286 case 4:
287 {
288 /* Remove a single address from existing interface */
289 if (strcmp("remove", argv[3]))
290 {
291 fprintf(stderr, "Invalid argument: %s\n\n", argv[3]);
292 showUsage();
293 return 1;
294 }
295 fRemove = true;
296 pszAdapterName = argv[1];
297 pszAddress = argv[2];
298 break;
299 }
300
301 case 3:
302 {
303 pszAdapterName = argv[1];
304 memset(&Req, '\0', sizeof(Req));
305#ifdef RT_OS_LINUX
306 if (strcmp("speed", argv[2]) == 0)
307 {
308 /*
309 * This ugly hack is needed for retrieving the link speed on
310 * pre-2.6.33 kernels (see @bugref{6345}).
311 */
312 if (strlen(pszAdapterName) >= IFNAMSIZ)
313 {
314 showUsage();
315 return -1;
316 }
317 struct ifreq IfReq;
318 struct ethtool_value EthToolVal;
319 struct ethtool_cmd EthToolReq;
320 int fd = socket(AF_INET, SOCK_DGRAM, 0);
321 if (fd < 0)
322 {
323 fprintf(stderr, "VBoxNetAdpCtl: Error while retrieving link "
324 "speed for %s: ", pszAdapterName);
325 perror("VBoxNetAdpCtl: failed to open control socket");
326 return ADPCTLERR_SOCKET_FAILED;
327 }
328 /* Get link status first. */
329 memset(&EthToolVal, 0, sizeof(EthToolVal));
330 memset(&IfReq, 0, sizeof(IfReq));
331 snprintf(IfReq.ifr_name, sizeof(IfReq.ifr_name), "%s", pszAdapterName);
332
333 EthToolVal.cmd = ETHTOOL_GLINK;
334 IfReq.ifr_data = (caddr_t)&EthToolVal;
335 rc = ioctl(fd, SIOCETHTOOL, &IfReq);
336 if (rc == 0)
337 {
338 if (EthToolVal.data)
339 {
340 memset(&IfReq, 0, sizeof(IfReq));
341 snprintf(IfReq.ifr_name, sizeof(IfReq.ifr_name), "%s", pszAdapterName);
342 EthToolReq.cmd = ETHTOOL_GSET;
343 IfReq.ifr_data = (caddr_t)&EthToolReq;
344 rc = ioctl(fd, SIOCETHTOOL, &IfReq);
345 if (rc == 0)
346 {
347 printf("%u", EthToolReq.speed);
348 }
349 else
350 {
351 fprintf(stderr, "VBoxNetAdpCtl: Error while retrieving link "
352 "speed for %s: ", pszAdapterName);
353 perror("VBoxNetAdpCtl: ioctl failed");
354 rc = ADPCTLERR_IOCTL_FAILED;
355 }
356 }
357 else
358 printf("0");
359 }
360 else
361 {
362 fprintf(stderr, "VBoxNetAdpCtl: Error while retrieving link "
363 "status for %s: ", pszAdapterName);
364 perror("VBoxNetAdpCtl: ioctl failed");
365 rc = ADPCTLERR_IOCTL_FAILED;
366 }
367
368 close(fd);
369 return rc;
370 }
371#endif
372 rc = checkAdapterName(pszAdapterName, szAdapterName);
373 if (rc)
374 return rc;
375 snprintf(Req.szName, sizeof(Req.szName), "%s", szAdapterName);
376 pszAddress = argv[2];
377 if (strcmp("remove", pszAddress) == 0)
378 {
379 /* Remove an existing interface */
380#ifdef RT_OS_SOLARIS
381 return 1;
382#else
383 return doIOCtl(VBOXNETADP_CTL_REMOVE, &Req);
384#endif
385 }
386 else if (strcmp("add", pszAddress) == 0)
387 {
388 /* Create an interface with given name */
389#ifdef RT_OS_SOLARIS
390 return 1;
391#else
392 rc = doIOCtl(VBOXNETADP_CTL_ADD, &Req);
393 if (rc == 0)
394 puts(Req.szName);
395#endif
396 return rc;
397 }
398 break;
399 }
400
401 case 2:
402 {
403 /* Create a new interface */
404 if (strcmp("add", argv[1]) == 0)
405 {
406#ifdef RT_OS_SOLARIS
407 return 1;
408#else
409 memset(&Req, '\0', sizeof(Req));
410 rc = doIOCtl(VBOXNETADP_CTL_ADD, &Req);
411 if (rc == 0)
412 puts(Req.szName);
413#endif
414 return rc;
415 }
416 /* Fall through */
417 }
418
419 default:
420 fprintf(stderr, "Invalid number of arguments.\n\n");
421 /* Fall through */
422 case 1:
423 showUsage();
424 return 1;
425 }
426
427 rc = checkAdapterName(pszAdapterName, szAdapterName);
428 if (rc)
429 return rc;
430
431 pszAdapterName = szAdapterName;
432
433 if (fRemove)
434 {
435 if (strchr(pszAddress, ':'))
436 rc = executeIfconfig(pszAdapterName, "inet6", VBOXADPCTL_DEL_CMD, pszAddress);
437 else
438 {
439#if defined(RT_OS_LINUX)
440 rc = executeIfconfig(pszAdapterName, "0.0.0.0");
441#else
442 rc = executeIfconfig(pszAdapterName, VBOXADPCTL_DEL_CMD, pszAddress);
443#endif
444
445#ifdef RT_OS_SOLARIS
446 /* On Solaris we can unplumb the ipv4 interface */
447 executeIfconfig(pszAdapterName, "inet", "unplumb");
448#endif
449 }
450 }
451 else
452 {
453 /* We are setting/replacing address. */
454 if (strchr(pszAddress, ':'))
455 {
456#ifdef RT_OS_SOLARIS
457 /* On Solaris we need to plumb the interface first if it's not already plumbed. */
458 if (executeIfconfig(pszAdapterName, "inet6") != 0)
459 executeIfconfig(pszAdapterName, "inet6", "plumb", "up");
460#endif
461 /*
462 * Before we set IPv6 address we'd like to remove
463 * all previously assigned addresses except the
464 * self-assigned one.
465 */
466 if (!removeAddresses(pszAdapterName))
467 rc = EXIT_FAILURE;
468 else
469 rc = executeIfconfig(pszAdapterName, "inet6", VBOXADPCTL_ADD_CMD, pszAddress, pszOption, pszNetworkMask);
470 }
471 else
472 {
473#ifdef RT_OS_SOLARIS
474 /* On Solaris we need to plumb the interface first if it's not already plumbed. */
475 if (executeIfconfig(pszAdapterName, "inet") != 0)
476 executeIfconfig(pszAdapterName, "plumb", "up");
477#endif
478 rc = executeIfconfig(pszAdapterName, pszAddress, pszOption, pszNetworkMask);
479 }
480 }
481 return rc;
482}
483
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette