[PATCH] Handle dhcp configuration properly for ipv4 and ipv6
From: Ani Sinha
Date: Thu Mar 21 2024 - 00:30:26 EST
dhcp_enabled is only valid for ipv4. So do not set dhcp methods for ipv6 based
on dhcp_enabled flag. For ipv4, set method to manual only when dhcp_enabled is
false and specific ipv4 addresses are configured. If neither dhcp_enabled is
true and no ipv4 addresses are configured, set method to 'disabled'.
For ipv6, set method to manual when we configure ipv6 addresses. Otherwise set
method to 'auto' so that SLAAC from RA may be used.
Signed-off-by: Ani Sinha <anisinha@xxxxxxxxxx>
---
hv_kvp_daemon.c | 57 +++++++++++++++++++++++++++++++++++--------------
1 file changed, 41 insertions(+), 16 deletions(-)
diff --git a/hv_kvp_daemon.c b/hv_kvp_daemon.c
index b368d3d..a0e6e4a 100644
--- a/hv_kvp_daemon.c
+++ b/hv_kvp_daemon.c
@@ -1286,7 +1286,7 @@ static int process_ip_string_nm(FILE *f, char *ip_string, char *subnet,
{
char addr[INET6_ADDRSTRLEN];
char subnet_addr[INET6_ADDRSTRLEN];
- int error, i = 0;
+ int error = 0, i = 0;
int ip_offset = 0, subnet_offset = 0;
int plen, ip_ver;
@@ -1323,7 +1323,7 @@ static int process_ip_string_nm(FILE *f, char *ip_string, char *subnet,
memset(subnet_addr, 0, sizeof(subnet_addr));
}
- return 0;
+ return error;
}
static int kvp_set_ip_info(char *if_name, struct hv_kvp_ipaddr_value *new_val)
@@ -1511,6 +1511,8 @@ static int kvp_set_ip_info(char *if_name, struct hv_kvp_ipaddr_value *new_val)
goto setval_error;
/*
+ * Now we populate the keyfile format
+ *
* The keyfile format expects the IPv6 and IPv4 configuration in
* different sections. Therefore we iterate through the list twice,
* once to populate the IPv4 section and the next time for IPv6
@@ -1527,20 +1529,6 @@ static int kvp_set_ip_info(char *if_name, struct hv_kvp_ipaddr_value *new_val)
goto setval_error;
}
- /*
- * Now we populate the keyfile format
- */
-
- if (new_val->dhcp_enabled) {
- error = kvp_write_file(nmfile, "method", "", "auto");
- if (error < 0)
- goto setval_error;
- } else {
- error = kvp_write_file(nmfile, "method", "", "manual");
- if (error < 0)
- goto setval_error;
- }
-
/*
* Write the configuration for ipaddress, netmask, gateway and
* name services
@@ -1551,6 +1539,43 @@ static int kvp_set_ip_info(char *if_name, struct hv_kvp_ipaddr_value *new_val)
if (error < 0)
goto setval_error;
+ if (ip_ver == IPV4) {
+ if (new_val->dhcp_enabled) {
+ error = kvp_write_file(nmfile, "method", "", "auto");
+ if (error < 0)
+ goto setval_error;
+ } else if (error) {
+ /* if ipv4 addresses were written, set method to 'manual' */
+ error = kvp_write_file(nmfile, "method", "", "manual");
+ if (error < 0)
+ goto setval_error;
+ } else {
+ /*
+ * if no ipv4 addresses were set and dhcp was not enabled,
+ * disable ipv4 configuration.
+ */
+ error = kvp_write_file(nmfile, "method", "", "disabled");
+ if (error < 0)
+ goto setval_error;
+ }
+
+ } else if (ip_ver == IPV6) {
+ if (error) {
+ /* if ipv6 addresses were written, set method to 'manual' */
+ error = kvp_write_file(nmfile, "method", "", "manual");
+ if (error < 0)
+ goto setval_error;
+ } else {
+ /*
+ * By default for ipv6, set method to 'auto' so that
+ * SLAAC in RA can be used to configure the interface
+ */
+ error = kvp_write_file(nmfile, "method", "", "auto");
+ if (error < 0)
+ goto setval_error;
+ }
+ }
+
error = process_dns_gateway_nm(nmfile,
(char *)new_val->gate_way,
GATEWAY, ip_ver);
--
2.39.2
---1463778935-701159659-1711022773=:180274--