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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
|
/* $Id: VBoxNetAdpCtl.cpp $ */
/** @file
* Apps - VBoxAdpCtl, Configuration tool for vboxnetX adapters.
*/
/*
* Copyright (C) 2009-2022 Oracle and/or its affiliates.
*
* This file is part of VirtualBox base platform packages, as
* available from https://www.virtualbox.org.
*
* 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, in version 3 of the
* License.
*
* 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, see <https://www.gnu.org/licenses>.
*
* SPDX-License-Identifier: GPL-3.0-only
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#include <list>
#include <errno.h>
#include <getopt.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <iprt/errcore.h>
#include <iprt/initterm.h>
#include <iprt/message.h>
#include <iprt/net.h>
#include <iprt/string.h>
#include <iprt/uint128.h>
#ifdef RT_OS_LINUX
# include <arpa/inet.h>
# include <net/if.h>
# include <linux/types.h>
/* Older versions of ethtool.h rely on these: */
typedef unsigned long long u64;
typedef __uint32_t u32;
typedef __uint16_t u16;
typedef __uint8_t u8;
# include <limits.h> /* for INT_MAX */
# include <linux/ethtool.h>
# include <linux/sockios.h>
#endif
#ifdef RT_OS_SOLARIS
# include <sys/ioccom.h>
#endif
/** @todo Error codes must be moved to some header file */
#define ADPCTLERR_BAD_NAME 2
#define ADPCTLERR_NO_CTL_DEV 3
#define ADPCTLERR_IOCTL_FAILED 4
#define ADPCTLERR_SOCKET_FAILED 5
/** @todo These are duplicates from src/VBox/HostDrivers/VBoxNetAdp/VBoxNetAdpInternal.h */
#define VBOXNETADP_CTL_DEV_NAME "/dev/vboxnetctl"
#define VBOXNETADP_MAX_INSTANCES 128
#define VBOXNETADP_NAME "vboxnet"
#define VBOXNETADP_MAX_NAME_LEN 32
#define VBOXNETADP_CTL_ADD _IOWR('v', 1, VBOXNETADPREQ)
#define VBOXNETADP_CTL_REMOVE _IOW('v', 2, VBOXNETADPREQ)
typedef struct VBoxNetAdpReq
{
char szName[VBOXNETADP_MAX_NAME_LEN];
} VBOXNETADPREQ;
typedef VBOXNETADPREQ *PVBOXNETADPREQ;
#define VBOXADPCTL_IFCONFIG_PATH1 "/sbin/ifconfig"
#define VBOXADPCTL_IFCONFIG_PATH2 "/bin/ifconfig"
bool verbose;
bool dry_run;
static int usage(void)
{
fprintf(stderr, "Usage: VBoxNetAdpCtl <adapter> <address> ([netmask <address>] | remove)\n");
fprintf(stderr, " | VBoxNetAdpCtl [<adapter>] add\n");
fprintf(stderr, " | VBoxNetAdpCtl <adapter> remove\n");
return EXIT_FAILURE;
}
/*
* A wrapper on standard list that provides '<<' operator for adding several list members in a single
* line dynamically. For example: "CmdList(arg1) << arg2 << arg3" produces a list with three members.
*/
class CmdList
{
public:
/** Creates an empty list. */
CmdList() {};
/** Creates a list with a single member. */
CmdList(const char *pcszCommand) { m_list.push_back(pcszCommand); };
/** Provides access to the underlying standard list. */
const std::list<const char *>& getList(void) const { return m_list; };
/** Adds a member to the list. */
CmdList& operator<<(const char *pcszArgument);
private:
std::list<const char *>m_list;
};
CmdList& CmdList::operator<<(const char *pcszArgument)
{
m_list.push_back(pcszArgument);
return *this;
}
/** Simple helper to distinguish IPv4 and IPv6 addresses. */
inline bool isAddrV6(const char *pcszAddress)
{
return !!(strchr(pcszAddress, ':'));
}
/*********************************************************************************************************************************
* Generic address commands. *
*********************************************************************************************************************************/
/**
* The base class for all address manipulation commands. While being an abstract class,
* it provides a generic implementation of 'set' and 'remove' methods, which rely on
* pure virtual methods like 'addV4' and 'removeV4' to perform actual command execution.
*/
class AddressCommand
{
public:
AddressCommand() : m_pszPath(0) {};
virtual ~AddressCommand() {};
/** returns true if underlying command (executable) is present in the system. */
bool isAvailable(void)
{ struct stat s; return (!stat(m_pszPath, &s) && S_ISREG(s.st_mode)); };
/*
* Someday we may want to support several IP addresses per adapter, but for now we
* have 'set' method only, which replaces all addresses with the one specifed.
*
* virtual int add(const char *pcszAdapter, const char *pcszAddress, const char *pcszNetmask = 0) = 0;
*/
/** replace existing address(es) */
virtual int set(const char *pcszAdapter, const char *pcszAddress, const char *pcszNetmask = 0);
/** remove address */
virtual int remove(const char *pcszAdapter, const char *pcszAddress);
protected:
/** IPv4-specific handler used by generic implementation of 'set' method if 'setV4' is not supported. */
virtual int addV4(const char *pcszAdapter, const char *pcszAddress, const char *pcszNetmask = 0) = 0;
/** IPv6-specific handler used by generic implementation of 'set' method. */
virtual int addV6(const char *pcszAdapter, const char *pcszAddress, const char *pcszNetmask = 0) = 0;
/** IPv4-specific handler used by generic implementation of 'set' method. */
virtual int setV4(const char *pcszAdapter, const char *pcszAddress, const char *pcszNetmask = 0) = 0;
/** IPv4-specific handler used by generic implementation of 'remove' method. */
virtual int removeV4(const char *pcszAdapter, const char *pcszAddress) = 0;
/** IPv6-specific handler used by generic implementation of 'remove' method. */
virtual int removeV6(const char *pcszAdapter, const char *pcszAddress) = 0;
/** Composes the argument list of command that obtains all addresses assigned to the adapter. */
virtual CmdList getShowCommand(const char *pcszAdapter) const = 0;
/** Prepares an array of C strings needed for 'exec' call. */
char * const * allocArgv(const CmdList& commandList);
/** Hides process creation details. To be used in derived classes. */
int execute(CmdList& commandList);
/** A path to executable command. */
const char *m_pszPath;
private:
/** Removes all previously asssigned addresses of a particular protocol family. */
int removeAddresses(const char *pcszAdapter, const char *pcszFamily);
};
/*
* A generic implementation of 'ifconfig' command for all platforms.
*/
class CmdIfconfig : public AddressCommand
{
public:
CmdIfconfig()
{
struct stat s;
if ( !stat(VBOXADPCTL_IFCONFIG_PATH1, &s)
&& S_ISREG(s.st_mode))
m_pszPath = (char*)VBOXADPCTL_IFCONFIG_PATH1;
else
m_pszPath = (char*)VBOXADPCTL_IFCONFIG_PATH2;
};
protected:
/** Returns platform-specific subcommand to add an address. */
virtual const char *addCmdArg(void) const = 0;
/** Returns platform-specific subcommand to remove an address. */
virtual const char *delCmdArg(void) const = 0;
virtual CmdList getShowCommand(const char *pcszAdapter) const
{ return CmdList(pcszAdapter); };
virtual int addV4(const char *pcszAdapter, const char *pcszAddress, const char *pcszNetmask = 0)
{ return ENOTSUP; NOREF(pcszAdapter); NOREF(pcszAddress); NOREF(pcszNetmask); };
virtual int addV6(const char *pcszAdapter, const char *pcszAddress, const char *pcszNetmask = 0)
{
return execute(CmdList(pcszAdapter) << "inet6" << addCmdArg() << pcszAddress);
NOREF(pcszNetmask);
};
virtual int setV4(const char *pcszAdapter, const char *pcszAddress, const char *pcszNetmask = 0)
{
if (!pcszNetmask)
return execute(CmdList(pcszAdapter) << pcszAddress);
return execute(CmdList(pcszAdapter) << pcszAddress << "netmask" << pcszNetmask);
};
virtual int removeV4(const char *pcszAdapter, const char *pcszAddress)
{ return execute(CmdList(pcszAdapter) << delCmdArg() << pcszAddress); };
virtual int removeV6(const char *pcszAdapter, const char *pcszAddress)
{ return execute(CmdList(pcszAdapter) << "inet6" << delCmdArg() << pcszAddress); };
};
/*********************************************************************************************************************************
* Platform-specific commands *
*********************************************************************************************************************************/
class CmdIfconfigLinux : public CmdIfconfig
{
protected:
virtual int removeV4(const char *pcszAdapter, const char *pcszAddress)
{ return execute(CmdList(pcszAdapter) << "0.0.0.0"); NOREF(pcszAddress); };
virtual const char *addCmdArg(void) const { return "add"; };
virtual const char *delCmdArg(void) const { return "del"; };
};
class CmdIfconfigDarwin : public CmdIfconfig
{
protected:
virtual const char *addCmdArg(void) const { return "add"; };
virtual const char *delCmdArg(void) const { return "delete"; };
};
class CmdIfconfigSolaris : public CmdIfconfig
{
public:
virtual int set(const char *pcszAdapter, const char *pcszAddress, const char *pcszNetmask = 0)
{
const char *pcszFamily = isAddrV6(pcszAddress) ? "inet6" : "inet";
int status;
status = execute(CmdList(pcszAdapter) << pcszFamily);
if (status != EXIT_SUCCESS)
status = execute(CmdList(pcszAdapter) << pcszFamily << "plumb" << "up");
if (status != EXIT_SUCCESS)
return status;
return CmdIfconfig::set(pcszAdapter, pcszAddress, pcszNetmask);
};
protected:
/* We can umplumb IPv4 interfaces only! */
virtual int removeV4(const char *pcszAdapter, const char *pcszAddress)
{
int rc = CmdIfconfig::removeV4(pcszAdapter, pcszAddress);
/** @todo Do we really need to unplumb inet here? */
execute(CmdList(pcszAdapter) << "inet" << "unplumb");
return rc;
};
virtual const char *addCmdArg(void) const { return "addif"; };
virtual const char *delCmdArg(void) const { return "removeif"; };
};
#ifdef RT_OS_LINUX
/*
* Helper class to incapsulate IPv4 address conversion.
*
* Note that this class relies on NetworkAddress to have been used for
* checking validity of IP addresses prior calling any methods of this
* class.
*/
class AddressIPv4
{
public:
AddressIPv4(const char *pcszAddress, const char *pcszNetmask = 0)
{
m_Prefix = 0;
memset(&m_Address, 0, sizeof(m_Address));
if (pcszNetmask)
m_Prefix = maskToPrefix(pcszNetmask);
else
{
/*
* Since guessing network mask is probably futile we simply use 24,
* as it matches our defaults. When non-default values are used
* providing a proper netmask is up to the user.
*/
m_Prefix = 24;
}
int rc = RTNetStrToIPv4Addr(pcszAddress, &m_Address);
AssertRCReturnVoid(rc);
snprintf(m_szAddressAndMask, sizeof(m_szAddressAndMask), "%s/%d", pcszAddress, m_Prefix);
deriveBroadcast(&m_Address, m_Prefix);
}
const char *getBroadcast() const { return m_szBroadcast; };
const char *getAddressAndMask() const { return m_szAddressAndMask; };
private:
int maskToPrefix(const char *pcszNetmask);
void deriveBroadcast(PCRTNETADDRIPV4 pcAddress, int uPrefix);
int m_Prefix;
RTNETADDRIPV4 m_Address;
char m_szAddressAndMask[INET_ADDRSTRLEN + 3]; /* e.g. 192.168.56.101/24 */
char m_szBroadcast[INET_ADDRSTRLEN];
};
int AddressIPv4::maskToPrefix(const char *pcszNetmask)
{
RTNETADDRIPV4 mask;
int prefix = 0;
int rc = RTNetStrToIPv4Addr(pcszNetmask, &mask);
AssertRCReturn(rc, 0);
rc = RTNetMaskToPrefixIPv4(&mask, &prefix);
AssertRCReturn(rc, 0);
return prefix;
}
void AddressIPv4::deriveBroadcast(PCRTNETADDRIPV4 pcAddress, int iPrefix)
{
RTNETADDRIPV4 mask, broadcast;
int rc = RTNetPrefixToMaskIPv4(iPrefix, &mask);
AssertRCReturnVoid(rc);
broadcast.au32[0] = (pcAddress->au32[0] & mask.au32[0]) | ~mask.au32[0];
inet_ntop(AF_INET, broadcast.au32, m_szBroadcast, sizeof(m_szBroadcast));
}
/*
* Linux-specific implementation of 'ip' command, as other platforms do not support it.
*/
class CmdIpLinux : public AddressCommand
{
public:
CmdIpLinux() { m_pszPath = "/sbin/ip"; };
/**
* IPv4 and IPv6 syntax is the same, so we override `remove` instead of implementing
* family-specific commands. It would be easier to use the same body in both
* 'removeV4' and 'removeV6', so we override 'remove' to illustrate how to do common
* implementation.
*/
virtual int remove(const char *pcszAdapter, const char *pcszAddress)
{ return execute(CmdList("addr") << "del" << pcszAddress << "dev" << pcszAdapter); };
protected:
virtual int addV4(const char *pcszAdapter, const char *pcszAddress, const char *pcszNetmask = 0)
{
AddressIPv4 addr(pcszAddress, pcszNetmask);
bringUp(pcszAdapter);
return execute(CmdList("addr") << "add" << addr.getAddressAndMask() <<
"broadcast" << addr.getBroadcast() << "dev" << pcszAdapter);
};
virtual int addV6(const char *pcszAdapter, const char *pcszAddress, const char *pcszNetmask = 0)
{
bringUp(pcszAdapter);
return execute(CmdList("addr") << "add" << pcszAddress << "dev" << pcszAdapter);
NOREF(pcszNetmask);
};
/**
* Our command does not support 'replacing' addresses. Reporting this fact to generic implementation
* of 'set' causes it to remove all assigned addresses, then 'add' the new one.
*/
virtual int setV4(const char *pcszAdapter, const char *pcszAddress, const char *pcszNetmask = 0)
{ return ENOTSUP; NOREF(pcszAdapter); NOREF(pcszAddress); NOREF(pcszNetmask); };
/** We use family-agnostic command syntax. See 'remove' above. */
virtual int removeV4(const char *pcszAdapter, const char *pcszAddress)
{ return ENOTSUP; NOREF(pcszAdapter); NOREF(pcszAddress); };
/** We use family-agnostic command syntax. See 'remove' above. */
virtual int removeV6(const char *pcszAdapter, const char *pcszAddress)
{ return ENOTSUP; NOREF(pcszAdapter); NOREF(pcszAddress); };
virtual CmdList getShowCommand(const char *pcszAdapter) const
{ return CmdList("addr") << "show" << "dev" << pcszAdapter; };
private:
/** Brings up the adapter */
void bringUp(const char *pcszAdapter)
{ execute(CmdList("link") << "set" << "dev" << pcszAdapter << "up"); };
};
#endif /* RT_OS_LINUX */
/*********************************************************************************************************************************
* Generic address command implementations *
*********************************************************************************************************************************/
int AddressCommand::set(const char *pcszAdapter, const char *pcszAddress, const char *pcszNetmask)
{
if (isAddrV6(pcszAddress))
{
removeAddresses(pcszAdapter, "inet6");
return addV6(pcszAdapter, pcszAddress, pcszNetmask);
}
int rc = setV4(pcszAdapter, pcszAddress, pcszNetmask);
if (rc == ENOTSUP)
{
removeAddresses(pcszAdapter, "inet");
rc = addV4(pcszAdapter, pcszAddress, pcszNetmask);
}
return rc;
}
int AddressCommand::remove(const char *pcszAdapter, const char *pcszAddress)
{
if (isAddrV6(pcszAddress))
return removeV6(pcszAdapter, pcszAddress);
return removeV4(pcszAdapter, pcszAddress);
}
/*
* Allocate an array of exec arguments. In addition to arguments provided
* we need to include the full path to the executable as well as "terminating"
* null pointer marking the end of the array.
*/
char * const * AddressCommand::allocArgv(const CmdList& list)
{
int i = 0;
std::list<const char *>::const_iterator it;
const char **argv = (const char **)calloc(list.getList().size() + 2, sizeof(const char *));
if (argv)
{
argv[i++] = m_pszPath;
for (it = list.getList().begin(); it != list.getList().end(); ++it)
argv[i++] = *it;
argv[i++] = NULL;
}
return (char * const*)argv;
}
int AddressCommand::execute(CmdList& list)
{
char * const pEnv[] = { (char*)"LC_ALL=C", NULL };
char * const* argv = allocArgv(list);
if (argv == NULL)
return EXIT_FAILURE;
if (verbose)
{
const char *sep = "";
for (const char * const *pArg = argv; *pArg != NULL; ++pArg)
{
printf("%s%s", sep, *pArg);
sep = " ";
}
printf("\n");
}
if (dry_run)
{
free((void *)argv);
return EXIT_SUCCESS;
}
int rc = EXIT_FAILURE; /* o/~ hope for the best, expect the worst */
pid_t childPid = fork();
switch (childPid)
{
case -1: /* Something went wrong. */
perror("fork");
break;
case 0: /* Child process. */
if (execve(argv[0], argv, pEnv) == -1)
{
perror("execve");
exit(EXIT_FAILURE);
/* NOTREACHED */
}
break;
default: /* Parent process. */
{
int status;
pid_t waited = waitpid(childPid, &status, 0);
if (waited == childPid) /* likely*/
{
if (WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS)
rc = EXIT_SUCCESS;
}
else if (waited == (pid_t)-1)
{
perror("waitpid");
}
else
{
/* should never happen */
fprintf(stderr, "waitpid: unexpected pid %lld\n",
(long long int)waited);
}
break;
}
}
free((void*)argv);
return rc;
}
#define MAX_ADDRESSES 128
#define MAX_ADDRLEN 64
int AddressCommand::removeAddresses(const char *pcszAdapter, const char *pcszFamily)
{
char szBuf[1024];
char aszAddresses[MAX_ADDRESSES][MAX_ADDRLEN];
int rc = EXIT_SUCCESS;
int fds[2];
memset(aszAddresses, 0, sizeof(aszAddresses));
rc = pipe(fds);
if (rc < 0)
return errno;
pid_t pid = fork();
if (pid < 0)
return errno;
if (pid == 0)
{
/* child */
close(fds[0]);
close(STDOUT_FILENO);
rc = dup2(fds[1], STDOUT_FILENO);
if (rc >= 0)
{
char * const * argv = allocArgv(getShowCommand(pcszAdapter));
char * const envp[] = { (char*)"LC_ALL=C", NULL };
if (execve(argv[0], argv, envp) == -1)
{
free((void *)argv);
return errno;
}
free((void *)argv);
}
return rc;
}
/* parent */
close(fds[1]);
FILE *fp = fdopen(fds[0], "r");
if (!fp)
return false;
int cAddrs;
for (cAddrs = 0; cAddrs < MAX_ADDRESSES && fgets(szBuf, sizeof(szBuf), fp);)
{
int cbSkipWS = strspn(szBuf, " \t");
char *pszWord = strtok(szBuf + cbSkipWS, " ");
/* We are concerned with particular family address lines only. */
if (!pszWord || strcmp(pszWord, pcszFamily))
continue;
pszWord = strtok(NULL, " ");
/* Skip "addr:" word if present. */
if (pszWord && !strcmp(pszWord, "addr:"))
pszWord = strtok(NULL, " ");
/* Skip link-local address lines. */
if (!pszWord || !strncmp(pszWord, "fe80", 4))
continue;
strncpy(aszAddresses[cAddrs++], pszWord, MAX_ADDRLEN-1);
}
fclose(fp);
for (int i = 0; i < cAddrs && rc == EXIT_SUCCESS; i++)
rc = remove(pcszAdapter, aszAddresses[i]);
return rc;
}
/*********************************************************************************************************************************
* Adapter creation/removal implementations *
*********************************************************************************************************************************/
/*
* A generic implementation of adapter creation/removal ioctl calls.
*/
class Adapter
{
public:
int add(char *pszNameInOut);
int remove(const char *pcszName);
int checkName(const char *pcszNameIn, char *pszNameOut, size_t cbNameOut);
protected:
virtual int doIOCtl(unsigned long iCmd, VBOXNETADPREQ *pReq);
};
/*
* Solaris does not support dynamic creation/removal of adapters.
*/
class AdapterSolaris : public Adapter
{
protected:
virtual int doIOCtl(unsigned long iCmd, VBOXNETADPREQ *pReq)
{ return 1 /*ENOTSUP*/; NOREF(iCmd); NOREF(pReq); };
};
#if defined(RT_OS_LINUX)
/*
* Linux implementation provides a 'workaround' to obtain adapter speed.
*/
class AdapterLinux : public Adapter
{
public:
int getSpeed(const char *pszName, unsigned *puSpeed);
};
int AdapterLinux::getSpeed(const char *pszName, unsigned *puSpeed)
{
struct ifreq IfReq;
struct ethtool_value EthToolVal;
struct ethtool_cmd EthToolReq;
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0)
{
fprintf(stderr, "VBoxNetAdpCtl: Error while retrieving link "
"speed for %s: ", pszName);
perror("VBoxNetAdpCtl: failed to open control socket");
return ADPCTLERR_SOCKET_FAILED;
}
/* Get link status first. */
memset(&EthToolVal, 0, sizeof(EthToolVal));
memset(&IfReq, 0, sizeof(IfReq));
snprintf(IfReq.ifr_name, sizeof(IfReq.ifr_name), "%s", pszName);
EthToolVal.cmd = ETHTOOL_GLINK;
IfReq.ifr_data = (caddr_t)&EthToolVal;
int rc = ioctl(fd, SIOCETHTOOL, &IfReq);
if (rc == 0)
{
if (EthToolVal.data)
{
memset(&IfReq, 0, sizeof(IfReq));
snprintf(IfReq.ifr_name, sizeof(IfReq.ifr_name), "%s", pszName);
EthToolReq.cmd = ETHTOOL_GSET;
IfReq.ifr_data = (caddr_t)&EthToolReq;
rc = ioctl(fd, SIOCETHTOOL, &IfReq);
if (rc == 0)
{
*puSpeed = EthToolReq.speed;
}
else
{
fprintf(stderr, "VBoxNetAdpCtl: Error while retrieving link "
"speed for %s: ", pszName);
perror("VBoxNetAdpCtl: ioctl failed");
rc = ADPCTLERR_IOCTL_FAILED;
}
}
else
*puSpeed = 0;
}
else
{
fprintf(stderr, "VBoxNetAdpCtl: Error while retrieving link "
"status for %s: ", pszName);
perror("VBoxNetAdpCtl: ioctl failed");
rc = ADPCTLERR_IOCTL_FAILED;
}
close(fd);
return rc;
}
#endif /* defined(RT_OS_LINUX) */
int Adapter::add(char *pszName /* in/out */)
{
VBOXNETADPREQ Req;
memset(&Req, '\0', sizeof(Req));
snprintf(Req.szName, sizeof(Req.szName), "%s", pszName);
int rc = doIOCtl(VBOXNETADP_CTL_ADD, &Req);
if (rc == 0)
strncpy(pszName, Req.szName, VBOXNETADP_MAX_NAME_LEN);
return rc;
}
int Adapter::remove(const char *pcszName)
{
VBOXNETADPREQ Req;
memset(&Req, '\0', sizeof(Req));
snprintf(Req.szName, sizeof(Req.szName), "%s", pcszName);
return doIOCtl(VBOXNETADP_CTL_REMOVE, &Req);
}
int Adapter::checkName(const char *pcszNameIn, char *pszNameOut, size_t cbNameOut)
{
int iAdapterIndex = -1;
if ( strlen(pcszNameIn) >= VBOXNETADP_MAX_NAME_LEN
|| sscanf(pcszNameIn, "vboxnet%d", &iAdapterIndex) != 1
|| iAdapterIndex < 0 || iAdapterIndex >= VBOXNETADP_MAX_INSTANCES )
{
fprintf(stderr, "VBoxNetAdpCtl: Setting configuration for '%s' is not supported.\n", pcszNameIn);
return ADPCTLERR_BAD_NAME;
}
snprintf(pszNameOut, cbNameOut, "vboxnet%d", iAdapterIndex);
if (strcmp(pszNameOut, pcszNameIn))
{
fprintf(stderr, "VBoxNetAdpCtl: Invalid adapter name '%s'.\n", pcszNameIn);
return ADPCTLERR_BAD_NAME;
}
return 0;
}
int Adapter::doIOCtl(unsigned long iCmd, VBOXNETADPREQ *pReq)
{
int fd = open(VBOXNETADP_CTL_DEV_NAME, O_RDWR);
if (fd == -1)
{
fprintf(stderr, "VBoxNetAdpCtl: Error while %s %s: ",
iCmd == VBOXNETADP_CTL_REMOVE ? "removing" : "adding",
pReq->szName[0] ? pReq->szName : "new interface");
perror("failed to open " VBOXNETADP_CTL_DEV_NAME);
return ADPCTLERR_NO_CTL_DEV;
}
int rc = ioctl(fd, iCmd, pReq);
if (rc == -1)
{
fprintf(stderr, "VBoxNetAdpCtl: Error while %s %s: ",
iCmd == VBOXNETADP_CTL_REMOVE ? "removing" : "adding",
pReq->szName[0] ? pReq->szName : "new interface");
perror("VBoxNetAdpCtl: ioctl failed for " VBOXNETADP_CTL_DEV_NAME);
rc = ADPCTLERR_IOCTL_FAILED;
}
close(fd);
return rc;
}
/*********************************************************************************************************************************
* Global config file implementation *
*********************************************************************************************************************************/
#define VBOX_GLOBAL_NETWORK_CONFIG_PATH "/etc/vbox/networks.conf"
#define VBOXNET_DEFAULT_IPV4MASK "255.255.255.0"
class NetworkAddress
{
public:
bool isValidString(const char *pcszNetwork);
bool isValid() { return m_fValid; };
virtual bool matches(const char *pcszNetwork) = 0;
virtual const char *defaultNetwork() = 0;
protected:
bool m_fValid;
};
bool NetworkAddress::isValidString(const char *pcszNetwork)
{
RTNETADDRIPV4 addrv4;
RTNETADDRIPV6 addrv6;
int prefix;
int rc = RTNetStrToIPv4Cidr(pcszNetwork, &addrv4, &prefix);
if (RT_SUCCESS(rc))
return true;
rc = RTNetStrToIPv6Cidr(pcszNetwork, &addrv6, &prefix);
return RT_SUCCESS(rc);
}
class NetworkAddressIPv4 : public NetworkAddress
{
public:
NetworkAddressIPv4(const char *pcszIpAddress, const char *pcszNetMask = VBOXNET_DEFAULT_IPV4MASK);
virtual bool matches(const char *pcszNetwork);
virtual const char *defaultNetwork() { return "192.168.56.1/21"; }; /* Matches defaults in VBox/Main/include/netif.h, see @bugref{10077}. */
protected:
bool isValidUnicastAddress(PCRTNETADDRIPV4 address);
private:
RTNETADDRIPV4 m_address;
int m_prefix;
};
NetworkAddressIPv4::NetworkAddressIPv4(const char *pcszIpAddress, const char *pcszNetMask)
{
int rc = RTNetStrToIPv4Addr(pcszIpAddress, &m_address);
if (RT_SUCCESS(rc))
{
RTNETADDRIPV4 mask;
rc = RTNetStrToIPv4Addr(pcszNetMask, &mask);
if (RT_FAILURE(rc))
m_fValid = false;
else
rc = RTNetMaskToPrefixIPv4(&mask, &m_prefix);
}
#if 0 /* cmd.set() does not support CIDR syntax */
else
rc = RTNetStrToIPv4Cidr(pcszIpAddress, &m_address, &m_prefix);
#endif
m_fValid = RT_SUCCESS(rc) && isValidUnicastAddress(&m_address);
}
bool NetworkAddressIPv4::isValidUnicastAddress(PCRTNETADDRIPV4 address)
{
/* Multicast addresses are not allowed. */
if ((address->au8[0] & 0xF0) == 0xE0)
return false;
/* Broadcast address is not allowed. */
if (address->au32[0] == 0xFFFFFFFF) /* Endianess does not matter in this particual case. */
return false;
/* Loopback addresses are not allowed. */
if ((address->au8[0] & 0xFF) == 0x7F)
return false;
return true;
}
bool NetworkAddressIPv4::matches(const char *pcszNetwork)
{
RTNETADDRIPV4 allowedNet, allowedMask;
int allowedPrefix;
int rc = RTNetStrToIPv4Cidr(pcszNetwork, &allowedNet, &allowedPrefix);
if (RT_SUCCESS(rc))
rc = RTNetPrefixToMaskIPv4(allowedPrefix, &allowedMask);
if (RT_FAILURE(rc))
return false;
return m_prefix >= allowedPrefix && (m_address.au32[0] & allowedMask.au32[0]) == (allowedNet.au32[0] & allowedMask.au32[0]);
}
class NetworkAddressIPv6 : public NetworkAddress
{
public:
NetworkAddressIPv6(const char *pcszIpAddress);
virtual bool matches(const char *pcszNetwork);
virtual const char *defaultNetwork() { return "FE80::/10"; };
private:
RTNETADDRIPV6 m_address;
int m_prefix;
};
NetworkAddressIPv6::NetworkAddressIPv6(const char *pcszIpAddress)
{
int rc = RTNetStrToIPv6Cidr(pcszIpAddress, &m_address, &m_prefix);
m_fValid = RT_SUCCESS(rc);
}
bool NetworkAddressIPv6::matches(const char *pcszNetwork)
{
RTNETADDRIPV6 allowedNet, allowedMask;
int allowedPrefix;
int rc = RTNetStrToIPv6Cidr(pcszNetwork, &allowedNet, &allowedPrefix);
if (RT_SUCCESS(rc))
rc = RTNetPrefixToMaskIPv6(allowedPrefix, &allowedMask);
if (RT_FAILURE(rc))
return false;
RTUINT128U u128Provided, u128Allowed;
return m_prefix >= allowedPrefix
&& RTUInt128Compare(RTUInt128And(&u128Provided, &m_address, &allowedMask), RTUInt128And(&u128Allowed, &allowedNet, &allowedMask)) == 0;
}
class GlobalNetworkPermissionsConfig
{
public:
bool forbids(const char *pcszIpAddress); /* address or address with mask in cidr */
bool forbids(const char *pcszIpAddress, const char *pcszNetMask);
private:
bool forbids(NetworkAddress& address);
};
bool GlobalNetworkPermissionsConfig::forbids(const char *pcszIpAddress)
{
NetworkAddressIPv6 addrv6(pcszIpAddress);
if (addrv6.isValid())
return forbids(addrv6);
NetworkAddressIPv4 addrv4(pcszIpAddress);
if (addrv4.isValid())
return forbids(addrv4);
fprintf(stderr, "Error: invalid address '%s'\n", pcszIpAddress);
return true;
}
bool GlobalNetworkPermissionsConfig::forbids(const char *pcszIpAddress, const char *pcszNetMask)
{
NetworkAddressIPv4 addrv4(pcszIpAddress, pcszNetMask);
if (addrv4.isValid())
return forbids(addrv4);
fprintf(stderr, "Error: invalid address '%s' with mask '%s'\n", pcszIpAddress, pcszNetMask);
return true;
}
bool GlobalNetworkPermissionsConfig::forbids(NetworkAddress& address)
{
FILE *fp = fopen(VBOX_GLOBAL_NETWORK_CONFIG_PATH, "r");
if (!fp)
{
if (verbose)
fprintf(stderr, "Info: matching against default '%s' => %s\n", address.defaultNetwork(),
address.matches(address.defaultNetwork()) ? "MATCH" : "no match");
return !address.matches(address.defaultNetwork());
}
char *pszToken, szLine[1024];
for (int line = 1; fgets(szLine, sizeof(szLine), fp); ++line)
{
pszToken = strtok(szLine, " \t\n");
/* Skip anything except '*' lines */
if (pszToken == NULL || strcmp("*", pszToken))
continue;
/* Match the specified address against each network */
while ((pszToken = strtok(NULL, " \t\n")) != NULL)
{
if (!address.isValidString(pszToken))
{
fprintf(stderr, "Warning: %s(%d) invalid network '%s'\n", VBOX_GLOBAL_NETWORK_CONFIG_PATH, line, pszToken);
continue;
}
if (verbose)
fprintf(stderr, "Info: %s(%d) matching against '%s' => %s\n", VBOX_GLOBAL_NETWORK_CONFIG_PATH, line, pszToken,
address.matches(pszToken) ? "MATCH" : "no match");
if (address.matches(pszToken))
return false;
}
}
fclose(fp);
return true;
}
/*********************************************************************************************************************************
* Main logic, argument parsing, etc. *
*********************************************************************************************************************************/
#if defined(RT_OS_LINUX)
static CmdIfconfigLinux g_ifconfig;
static AdapterLinux g_adapter;
#elif defined(RT_OS_SOLARIS)
static CmdIfconfigSolaris g_ifconfig;
static AdapterSolaris g_adapter;
#else
static CmdIfconfigDarwin g_ifconfig;
static Adapter g_adapter;
#endif
static AddressCommand& chooseAddressCommand()
{
#if defined(RT_OS_LINUX)
static CmdIpLinux g_ip;
if (g_ip.isAvailable())
return g_ip;
#endif
return g_ifconfig;
}
int main(int argc, char *argv[])
{
char szAdapterName[VBOXNETADP_MAX_NAME_LEN];
int rc = RTR3InitExe(argc, &argv, 0 /*fFlags*/);
if (RT_FAILURE(rc))
return RTMsgInitFailure(rc);
AddressCommand& cmd = chooseAddressCommand();
static const struct option options[] = {
{ "dry-run", no_argument, NULL, 'n' },
{ "verbose", no_argument, NULL, 'v' },
{ NULL, 0, NULL, 0 }
};
int ch;
while ((ch = getopt_long(argc, argv, "nv", options, NULL)) != -1)
{
switch (ch)
{
case 'n':
dry_run = true;
verbose = true;
break;
case 'v':
verbose = true;
break;
case '?':
default:
return usage();
}
}
argc -= optind;
argv += optind;
if (argc == 0)
return usage();
/*
* VBoxNetAdpCtl add
*/
if (strcmp(argv[0], "add") == 0)
{
if (argc > 1) /* extraneous args */
return usage();
/* Create a new interface, print its name. */
*szAdapterName = '\0';
rc = g_adapter.add(szAdapterName);
if (rc == EXIT_SUCCESS)
puts(szAdapterName);
return rc;
}
/*
* All other variants are of the form:
* VBoxNetAdpCtl if0 ...action...
*/
const char * const ifname = argv[0];
const char * const action = argv[1];
if (argc < 2)
return usage();
#ifdef RT_OS_LINUX
/*
* VBoxNetAdpCtl iface42 speed
*
* This ugly hack is needed for retrieving the link speed on
* pre-2.6.33 kernels (see @bugref{6345}).
*
* This variant is used with any interface, not just host-only.
*/
if (strcmp(action, "speed") == 0)
{
if (argc > 2) /* extraneous args */
return usage();
if (strlen(ifname) >= IFNAMSIZ)
{
fprintf(stderr, "Interface name too long\n");
return EXIT_FAILURE;
}
unsigned uSpeed = 0;
rc = g_adapter.getSpeed(ifname, &uSpeed);
if (rc == EXIT_SUCCESS)
printf("%u", uSpeed);
return rc;
}
#endif /* RT_OS_LINUX */
/*
* The rest of the actions only operate on host-only interfaces.
*/
/** @todo Why the code below uses both ifname and szAdapterName? */
rc = g_adapter.checkName(ifname, szAdapterName, sizeof(szAdapterName));
if (rc != EXIT_SUCCESS)
return rc;
/*
* VBoxNetAdpCtl vboxnetN remove
*/
if (strcmp(action, "remove") == 0)
{
if (argc > 2) /* extraneous args */
return usage();
/* Remove an existing interface */
return g_adapter.remove(ifname);
}
/*
* VBoxNetAdpCtl vboxnetN add
*/
if (strcmp(action, "add") == 0)
{
if (argc > 2) /* extraneous args */
return usage();
/* Create an interface with the given name, print its name. */
rc = g_adapter.add(szAdapterName);
if (rc == EXIT_SUCCESS)
puts(szAdapterName);
return rc;
}
/*
* The rest of the actions are of the form
* VBoxNetAdpCtl vboxnetN $addr [...]
*
* Use the argument after the address to select the action.
*/
/** @todo Do early verification of addr format here? */
const char * const addr = argv[1];
const char * const keyword = argv[2];
GlobalNetworkPermissionsConfig config;
/*
* VBoxNetAdpCtl vboxnetN 1.2.3.4
*/
if (keyword == NULL)
{
if (config.forbids(addr))
{
fprintf(stderr, "Error: permission denied\n");
return -VERR_ACCESS_DENIED;
}
return cmd.set(ifname, addr);
}
/*
* VBoxNetAdpCtl vboxnetN 1.2.3.4 netmask 255.255.255.0
*/
if (strcmp(keyword, "netmask") == 0)
{
if (argc != 4) /* too few or too many args */
return usage();
const char * const mask = argv[3];
if (config.forbids(addr, mask))
{
fprintf(stderr, "Error: permission denied\n");
return -VERR_ACCESS_DENIED;
}
return cmd.set(ifname, addr, mask);
}
/*
* VBoxNetAdpCtl vboxnetN 1.2.3.4 remove
*/
if (strcmp(keyword, "remove") == 0)
{
if (argc > 3) /* extraneous args */
return usage();
return cmd.remove(ifname, addr);
}
return usage();
}
|