Skip to content

Commit 88d975d

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 Failla <[email protected]>
1 parent a7cc880 commit 88d975d

File tree

4 files changed

+83
-40
lines changed

4 files changed

+83
-40
lines changed

README.md

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,29 @@ Unlike `vde_vmnet`, `socket_vmnet` does not depend on VDE.
1212
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
1313
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
1414

15-
- [Install](#install)
16-
- [From binary](#from-binary)
17-
- [From source](#from-source)
18-
- [From Homebrew](#from-homebrew)
19-
- [From MacPorts](#from-macports)
20-
- [Usage](#usage)
21-
- [QEMU](#qemu)
22-
- [Lima](#lima)
23-
- [Advanced usage](#advanced-usage)
24-
- [Multi VM](#multi-vm)
25-
- [Bridged mode](#bridged-mode)
26-
- [FAQs](#faqs)
27-
- [Why does `socket_vmnet` require root?](#why-does-socket_vmnet-require-root)
28-
- [Is it possible to run `socket_vmnet` with SETUID?](#is-it-possible-to-run-socket_vmnet-with-setuid)
29-
- [How is socket_vmnet related to vde_vmnet?](#how-is-socket_vmnet-related-to-vde_vmnet)
30-
- [How is socket_vmnet related to QEMU-builtin vmnet support?](#how-is-socket_vmnet-related-to-qemu-builtin-vmnet-support)
31-
- [How to use static IP addresses?](#how-to-use-static-ip-addresses)
32-
- [How to reserve DHCP addresses?](#how-to-reserve-dhcp-addresses)
33-
- [IP address is not assigned](#ip-address-is-not-assigned)
34-
- [Links](#links)
35-
- [Troubleshooting](#troubleshooting)
15+
- [socket_vmnet: vmnet.framework support for rootless and VDE-less QEMU](#socket_vmnet-vmnetframework-support-for-rootless-and-vde-less-qemu)
16+
- [Install](#install)
17+
- [From binary](#from-binary)
18+
- [From source](#from-source)
19+
- [From Homebrew](#from-homebrew)
20+
- [From MacPorts](#from-macports)
21+
- [Usage](#usage)
22+
- [QEMU](#qemu)
23+
- [Lima](#lima)
24+
- [Advanced usage](#advanced-usage)
25+
- [Multi VM](#multi-vm)
26+
- [Bridged mode](#bridged-mode)
27+
- [FAQs](#faqs)
28+
- [Why does `socket_vmnet` require root?](#why-does-socket_vmnet-require-root)
29+
- [Is it possible to run `socket_vmnet` with SETUID?](#is-it-possible-to-run-socket_vmnet-with-setuid)
30+
- [How is socket_vmnet related to vde_vmnet?](#how-is-socket_vmnet-related-to-vde_vmnet)
31+
- [How is socket_vmnet related to QEMU-builtin vmnet support?](#how-is-socket_vmnet-related-to-qemu-builtin-vmnet-support)
32+
- [How to use static IP addresses?](#how-to-use-static-ip-addresses)
33+
- [How to reserve DHCP addresses?](#how-to-reserve-dhcp-addresses)
34+
- [IP address is not assigned](#ip-address-is-not-assigned)
35+
- [How to setup a vmnet host network without DHCP](#how-to-setup-a-vmnet-host-network-without-dhcp)
36+
- [Links](#links)
37+
- [Troubleshooting](#troubleshooting)
3638

3739
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
3840

@@ -122,8 +124,7 @@ Run the following command to start the daemon:
122124
sudo /opt/socket_vmnet/bin/socket_vmnet --vmnet-gateway=192.168.105.1 /var/run/socket_vmnet
123125
```
124126

125-
> [!TIP]
126-
> `sudo make install` is also available in addition to `sudo make install.bin`.
127+
> [!TIP] > `sudo make install` is also available in addition to `sudo make install.bin`.
127128
> The former one installs the launchd service (see below) too.
128129
129130
<details>
@@ -430,6 +431,17 @@ sudo /usr/libexec/ApplicationFirewall/socketfilterfw --add /usr/libexec/bootpd
430431
/usr/libexec/ApplicationFirewall/socketfilterfw --unblock /usr/libexec/bootpd
431432
```
432433

434+
### How to setup a vmnet host network without DHCP
435+
436+
You may need to disable the vmnet framework's DHCP to:
437+
438+
- Create a host network where all VMs have static IPs.
439+
- Run a custom DHCP server on one VM to assign IPs to others on the same network.
440+
441+
To disable the MacOS DHCP you must use `--vmnet-mode=host` and provide a `--vmnet-network-idenfitier` UUID.
442+
You **_must not_** provide `--vmnet-gateway`. That is the signal to Apple's vmnet.framework to enable MacOS DHCP.
443+
You can use `--vmnet-network-idenfitier=random` to get a random UUID assigned.
444+
433445
## Links
434446

435447
- 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)