VirtualBox

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

Last change on this file since 43515 was 43515, checked in by vboxsync, 12 years ago

Main/Metrics: another attempt to satisfy rhel 4

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