Skip to content

Commit 69b6d58

Browse files
committed
feat: Allow specifying vmnet network UUID to disable DHCP (on vmnet.host network only)
This commit introduces a new `--vmnet-network-uuid` command-line option to allow setting the `vmnet_network_identifier_key` for vmnet. This property is only applicable to a vmnet_interface in VMNET_HOST_MODE. If this property is set, the vmnet_interface is added to an isolated network with the specified identifier. No DHCP service is provided on this network. This is useful for certain applications where the users need an isolated network and are running their own dhcp to assign IPs in such network. See issue [#139](#139) Signed-off-by: Angelo (pallotron) Failla <[email protected]>
1 parent a7cc880 commit 69b6d58

File tree

4 files changed

+63
-17
lines changed

4 files changed

+63
-17
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,12 @@ sudo launchctl bootout system /Library/LaunchDaemons/io.github.lima-vm.socket_vm
351351
sudo rm /Library/LaunchDaemons/io.github.lima-vm.socket_vmnet.bridged.${BRIDGED}.plist
352352
```
353353

354+
### No DHCP mode
355+
356+
By passing `--vmnet-mode=host` without `--vmnet-gateway` to `socket_vmnet`, the internal DHCP will be disabled.
357+
This is useful for situations where the users want to create a network with statically assigned IP addresses or where another VMs is running its own DHCP server.
358+
This mode does not come with a launchd service.
359+
354360
## FAQs
355361

356362
### Why does `socket_vmnet` require root?
@@ -430,6 +436,15 @@ sudo /usr/libexec/ApplicationFirewall/socketfilterfw --add /usr/libexec/bootpd
430436
/usr/libexec/ApplicationFirewall/socketfilterfw --unblock /usr/libexec/bootpd
431437
```
432438

439+
### How to setup a vmnet host network without DHCP
440+
441+
You may need to disable the vmnet framework's DHCP to:
442+
- Create a host network where all VMs have static IPs.
443+
- Run a custom DHCP server on one VM to assign IPs to others on the same network.
444+
445+
To disable the internal DHCP, use `--vmnet-mode=host` without `--vmnet-gateway`. This allows you to use static IP addresses for your VMs.
446+
You can optionally provide a `--vmnet-network-uuid`.
447+
433448
## Links
434449

435450
- https://developer.apple.com/documentation/vmnet

cli.c

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ static void print_usage(const char *argv0) {
4848
"specified\n");
4949
printf("--vmnet-interface-id=UUID vmnet interface ID (default: "
5050
"random)\n");
51+
printf("--vmnet-network-identifier=UUID vmnet network identifier (UUID string, \"random\", "
52+
"or \"\")\n"
53+
" When vmnet mode is \"host\" and --vmnet-gateway is "
54+
"not set, the internal DHCP will be disabled.\n"
55+
" (default: \"random\")\n");
56+
5157
printf("--vmnet-nat66-prefix=PREFIX:: The IPv6 prefix to use with "
5258
"shared mode.\n");
5359
printf(" The prefix must be a ULA i.e. "
@@ -72,6 +78,7 @@ enum {
7278
CLI_OPT_VMNET_MASK,
7379
CLI_OPT_VMNET_INTERFACE_ID,
7480
CLI_OPT_VMNET_NAT66_PREFIX,
81+
CLI_OPT_VMNET_NETWORK_IDENTIFIER,
7582
};
7683

7784
struct cli_options *cli_options_parse(int argc, char *argv[]) {
@@ -82,18 +89,19 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) {
8289
}
8390

8491
const struct option longopts[] = {
85-
{"socket-group", required_argument, NULL, CLI_OPT_SOCKET_GROUP },
86-
{"vmnet-mode", required_argument, NULL, CLI_OPT_VMNET_MODE },
87-
{"vmnet-interface", required_argument, NULL, CLI_OPT_VMNET_INTERFACE },
88-
{"vmnet-gateway", required_argument, NULL, CLI_OPT_VMNET_GATEWAY },
89-
{"vmnet-dhcp-end", required_argument, NULL, CLI_OPT_VMNET_DHCP_END },
90-
{"vmnet-mask", required_argument, NULL, CLI_OPT_VMNET_MASK },
91-
{"vmnet-interface-id", required_argument, NULL, CLI_OPT_VMNET_INTERFACE_ID},
92-
{"vmnet-nat66-prefix", required_argument, NULL, CLI_OPT_VMNET_NAT66_PREFIX},
93-
{"pidfile", required_argument, NULL, 'p' },
94-
{"help", no_argument, NULL, 'h' },
95-
{"version", no_argument, NULL, 'v' },
96-
{0, 0, 0, 0 },
92+
{"socket-group", required_argument, NULL, CLI_OPT_SOCKET_GROUP },
93+
{"vmnet-mode", required_argument, NULL, CLI_OPT_VMNET_MODE },
94+
{"vmnet-interface", required_argument, NULL, CLI_OPT_VMNET_INTERFACE },
95+
{"vmnet-gateway", required_argument, NULL, CLI_OPT_VMNET_GATEWAY },
96+
{"vmnet-dhcp-end", required_argument, NULL, CLI_OPT_VMNET_DHCP_END },
97+
{"vmnet-mask", required_argument, NULL, CLI_OPT_VMNET_MASK },
98+
{"vmnet-interface-id", required_argument, NULL, CLI_OPT_VMNET_INTERFACE_ID },
99+
{"vmnet-nat66-prefix", required_argument, NULL, CLI_OPT_VMNET_NAT66_PREFIX },
100+
{"vmnet-network-identifier", required_argument, NULL, CLI_OPT_VMNET_NETWORK_IDENTIFIER},
101+
{"pidfile", required_argument, NULL, 'p' },
102+
{"help", no_argument, NULL, 'h' },
103+
{"version", no_argument, NULL, 'v' },
104+
{0, 0, 0, 0 },
97105
};
98106
int opt = 0;
99107
while ((opt = getopt_long(argc, argv, "hvp:", longopts, NULL)) != -1) {
@@ -134,6 +142,16 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) {
134142
case CLI_OPT_VMNET_NAT66_PREFIX:
135143
res->vmnet_nat66_prefix = strdup(optarg);
136144
break;
145+
case CLI_OPT_VMNET_NETWORK_IDENTIFIER:
146+
if (strcmp(optarg, "random") == 0) {
147+
uuid_generate_random(res->vmnet_network_identifier);
148+
} else if (strcmp(optarg, "") == 0) {
149+
uuid_clear(res->vmnet_network_identifier);
150+
} else if (uuid_parse(optarg, res->vmnet_network_identifier) < 0) {
151+
ERRORF("Failed to parse UUID \"%s\"", optarg);
152+
goto error;
153+
}
154+
break;
137155
case 'p':
138156
res->pidfile = strdup(optarg);
139157
break;
@@ -191,7 +209,7 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) {
191209
goto error;
192210
}
193211
if (res->vmnet_gateway == NULL) {
194-
if (res->vmnet_mode != VMNET_BRIDGED_MODE) {
212+
if (res->vmnet_mode != VMNET_BRIDGED_MODE && res->vmnet_mode != VMNET_HOST_MODE) {
195213
WARN("--vmnet-gateway=IP should be explicitly specified to "
196214
"avoid conflicting with other applications");
197215
}

cli.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ struct cli_options {
2020
char *vmnet_mask;
2121
// --vmnet-interface-id, corresponds to vmnet_interface_id_key
2222
uuid_t vmnet_interface_id;
23+
// --vmnet-network-identifier, corresponds to vmnet_network_identifier_key
24+
uuid_t vmnet_network_identifier;
2325
// --vmnet-nat66-prefix, corresponds to vmnet_nat66_prefix_key
2426
char *vmnet_nat66_prefix;
2527
// -p, --pidfile; writes pidfile using permissions of socket_vmnet

main.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,21 @@ static interface_ref start(struct state *state, struct cli_options *cliopt) {
230230
INFOF("Using network interface \"%s\"", cliopt->vmnet_interface);
231231
xpc_dictionary_set_string(dict, vmnet_shared_interface_name_key, cliopt->vmnet_interface);
232232
}
233-
if (cliopt->vmnet_gateway != NULL) {
234-
xpc_dictionary_set_string(dict, vmnet_start_address_key, cliopt->vmnet_gateway);
235-
xpc_dictionary_set_string(dict, vmnet_end_address_key, cliopt->vmnet_dhcp_end);
236-
xpc_dictionary_set_string(dict, vmnet_subnet_mask_key, cliopt->vmnet_mask);
233+
234+
// no dhcp
235+
if (cliopt->vmnet_mode == VMNET_HOST_MODE && cliopt->vmnet_gateway == NULL) {
236+
if (!uuid_is_null(cliopt->vmnet_network_identifier)) {
237+
xpc_dictionary_set_uuid(dict, vmnet_network_identifier_key, cliopt->vmnet_network_identifier);
238+
uuid_string_t uuid_str;
239+
uuid_unparse(cliopt->vmnet_network_identifier, uuid_str);
240+
INFOF("Using network identifier \"%s\" and no vmnet gateway -> NO DHCP will be enabled on this vmnet", uuid_str);
241+
}
242+
} else {
243+
if (cliopt->vmnet_gateway != NULL) {
244+
xpc_dictionary_set_string(dict, vmnet_start_address_key, cliopt->vmnet_gateway);
245+
xpc_dictionary_set_string(dict, vmnet_end_address_key, cliopt->vmnet_dhcp_end);
246+
xpc_dictionary_set_string(dict, vmnet_subnet_mask_key, cliopt->vmnet_mask);
247+
}
237248
}
238249

239250
xpc_dictionary_set_uuid(dict, vmnet_interface_id_key, cliopt->vmnet_interface_id);

0 commit comments

Comments
 (0)