Skip to content

Commit 534d141

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) ``` ❯ sudo ./socket_vmnet "/opt/homebrew/var/run/socket_vmnet" --vmnet-mode=host --vmnet-gateway=192.168.100.1 --vmnet-network-uuid=99eddb74-3193-48e3-bafb-b3ca99e949c1 Password: INFO | Initializing vmnet.framework (mode 1000) INFO | * vmnet_write_max_packets: 256 INFO | * vmnet_read_max_packets: 256 INFO | * vmnet_subnet_mask: 0.0.0.0 INFO | * vmnet_mtu: 1500 INFO | * vmnet_end_address: 255.255.255.254 INFO | * vmnet_start_address: 0.0.0.0 INFO | * vmnet_interface_id: 0E21C1D4-43D9-4363-B767-A64FE591EA7D INFO | * vmnet_max_packet_size: 1514 INFO | * vmnet_mac_address: 66:77:98:e1:52:9c ``` I could run my LAB vms, I saw no DHCP been handed over by MacOS/bootp, my Lab runs a special VM with DHCPD and I saw that not colliding. ``` ❯ sudo ./socket_vmnet "/opt/homebrew/var/run/socket_vmnet" --vmnet-gateway=192.168.100.1 --vmnet-network-uuid=99eddb74-3193-48e3-bafb-b3ca99e949c1 INFO | Initializing vmnet.framework (mode 1001) INFO | * vmnet_write_max_packets: 256 INFO | * vmnet_read_max_packets: 256 INFO | * vmnet_subnet_mask: 255.255.255.0 INFO | * vmnet_mtu: 1500 INFO | * vmnet_end_address: 192.168.100.254 INFO | * vmnet_start_address: 192.168.100.1 INFO | * vmnet_interface_id: 99AC02F6-59D3-4192-820B-C8DAED0F2B81 INFO | * vmnet_max_packet_size: 1514 INFO | * vmnet_nat66_prefix: fd86:9a96:2682:1344:: INFO | * vmnet_mac_address: 3a:be:d4:0d:33:11 ``` Signed-off-by: Angelo Failla <[email protected]>
1 parent a7cc880 commit 534d141

File tree

4 files changed

+45
-5
lines changed

4 files changed

+45
-5
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: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ 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-uuid=UUID vmnet network UUID, if provided in \"host\" vmnet mode withouht --vmnet-gateway, the\n"
52+
" internal DHCP will be disabled (default: " "random)\n");
5153
printf("--vmnet-nat66-prefix=PREFIX:: The IPv6 prefix to use with "
5254
"shared mode.\n");
5355
printf(" The prefix must be a ULA i.e. "
@@ -72,6 +74,7 @@ enum {
7274
CLI_OPT_VMNET_MASK,
7375
CLI_OPT_VMNET_INTERFACE_ID,
7476
CLI_OPT_VMNET_NAT66_PREFIX,
77+
CLI_OPT_VMNET_NETWORK_UUID,
7578
};
7679

7780
struct cli_options *cli_options_parse(int argc, char *argv[]) {
@@ -80,6 +83,7 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) {
8083
ERRORN("calloc");
8184
exit(EXIT_FAILURE);
8285
}
86+
uuid_clear(res->vmnet_network_uuid);
8387

8488
const struct option longopts[] = {
8589
{"socket-group", required_argument, NULL, CLI_OPT_SOCKET_GROUP },
@@ -90,6 +94,7 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) {
9094
{"vmnet-mask", required_argument, NULL, CLI_OPT_VMNET_MASK },
9195
{"vmnet-interface-id", required_argument, NULL, CLI_OPT_VMNET_INTERFACE_ID},
9296
{"vmnet-nat66-prefix", required_argument, NULL, CLI_OPT_VMNET_NAT66_PREFIX},
97+
{"vmnet-network-uuid", required_argument, NULL, CLI_OPT_VMNET_NETWORK_UUID},
9398
{"pidfile", required_argument, NULL, 'p' },
9499
{"help", no_argument, NULL, 'h' },
95100
{"version", no_argument, NULL, 'v' },
@@ -134,6 +139,12 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) {
134139
case CLI_OPT_VMNET_NAT66_PREFIX:
135140
res->vmnet_nat66_prefix = strdup(optarg);
136141
break;
142+
case CLI_OPT_VMNET_NETWORK_UUID:
143+
if (uuid_parse(optarg, res->vmnet_network_uuid) < 0) {
144+
ERRORF("Failed to parse UUID \"%s\"", optarg);
145+
goto error;
146+
}
147+
break;
137148
case 'p':
138149
res->pidfile = strdup(optarg);
139150
break;
@@ -184,14 +195,17 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) {
184195
if (uuid_is_null(res->vmnet_interface_id)) {
185196
uuid_generate_random(res->vmnet_interface_id);
186197
}
198+
if (uuid_is_null(res->vmnet_network_uuid)) {
199+
uuid_generate_random(res->vmnet_network_uuid);
200+
}
187201

188202
/* validate */
189203
if (res->vmnet_mode == VMNET_BRIDGED_MODE && res->vmnet_interface == NULL) {
190204
ERROR("vmnet mode \"bridged\" require --vmnet-interface to be specified");
191205
goto error;
192206
}
193207
if (res->vmnet_gateway == NULL) {
194-
if (res->vmnet_mode != VMNET_BRIDGED_MODE) {
208+
if (res->vmnet_mode != VMNET_BRIDGED_MODE && res->vmnet_mode != VMNET_HOST_MODE) {
195209
WARN("--vmnet-gateway=IP should be explicitly specified to "
196210
"avoid conflicting with other applications");
197211
}

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-uuid, corresponds to vmnet_network_identifier_key
24+
uuid_t vmnet_network_uuid;
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: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,19 @@ 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+
xpc_dictionary_set_uuid(dict, vmnet_network_identifier_key, cliopt->vmnet_network_uuid);
237+
uuid_string_t uuid_str;
238+
uuid_unparse(cliopt->vmnet_network_uuid, uuid_str);
239+
INFOF("Using network identifier \"%s\" and no vmnet gateway -> NO DHCP will be enabled on this vmnet", uuid_str);
240+
} else {
241+
if (cliopt->vmnet_gateway != NULL) {
242+
xpc_dictionary_set_string(dict, vmnet_start_address_key, cliopt->vmnet_gateway);
243+
xpc_dictionary_set_string(dict, vmnet_end_address_key, cliopt->vmnet_dhcp_end);
244+
xpc_dictionary_set_string(dict, vmnet_subnet_mask_key, cliopt->vmnet_mask);
245+
}
237246
}
238247

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

0 commit comments

Comments
 (0)