VirtualBox

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

Last change on this file since 35785 was 35785, checked in by vboxsync, 14 years ago

netadp: Re-create configured vboxnetX interfaces (#4213) on Linux

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