Skip to contentSkip to conversation

treewide: Parse EnableNetworkConfiguration in one place

PATCH Applied Andrew Zaborowski 2 participants 17 messages Oct 21, 2021 – Oct 22, 2021

lore
Share & tools
lore

Andrew Zaborowski

author
#0 lore raw

PATCH 1/12 treewide: Parse EnableNetworkConfiguration in one place

Add netconfig_get_enabled() and use that in all places that want to know whether network configuration is enabled. Drop the enable_network_config deprecated setting, which was only being handled in one of these 5 or so places.

---
 src/ap.c        | 11 +++--------
 src/ip-pool.c   | 11 ++---------
 src/main.c      |  8 ++------
 src/netconfig.c |  9 +++++++++
 src/netconfig.h |  2 ++
 src/resolve.c   | 12 ++----------
 src/station.c   | 10 ++--------
 7 files changed, 22 insertions(+), 41 deletions(-)
src/ap.c +3 -8
index 45e647b8..4b26e9ff 100644
@@ -53,6 +53,7 @@
 #include "src/wscutil.h"
 #include "src/eap-wsc.h"
 #include "src/ip-pool.h"
+#include "src/netconfig.h"
 #include "src/ap.h"
 #include "src/storage.h"
 #include "src/diagnostic.h"
@@ -132,7 +133,6 @@ struct ap_wsc_pbc_probe_record {
 	uint64_t timestamp;
 };
 
-static bool netconfig_enabled;
 static char **global_addr4_strs;
 static uint32_t netdev_watch;
 static struct l_netlink *rtnl;
@@ -2826,7 +2826,7 @@ static int ap_load_ipv4(struct ap_state *ap, const struct l_settings *config)
 	unsigned int lease_time = 0;
 	struct in_addr ia;
 
-	if (!l_settings_has_group(config, "IPv4") || !netconfig_enabled)
+	if (!l_settings_has_group(config, "IPv4") || !netconfig_get_enabled())
 		return 0;
 
 	if (l_settings_has_key(config, "IPv4", "Address")) {
@@ -3829,12 +3829,7 @@ static int ap_init(void)
 	 * Enable network configuration and DHCP only if
 	 * [General].EnableNetworkConfiguration is true.
 	 */
-	if (!l_settings_get_bool(settings, "General",
-					"EnableNetworkConfiguration",
-					&netconfig_enabled))
-		netconfig_enabled = false;
-
-	if (netconfig_enabled) {
+	if (netconfig_get_enabled()) {
 		if (l_settings_get_value(settings, "IPv4", "APAddressPool")) {
 			global_addr4_strs = l_settings_get_string_list(settings,
 								"IPv4",
src/ip-pool.c +2 -9
index 82f5216e..0c84394c 100644
@@ -34,6 +34,7 @@
 #include "src/iwd.h"
 #include "src/module.h"
 #include "src/netdev.h"
+#include "src/netconfig.h"
 #include "src/ip-pool.h"
 
 struct ip_pool_addr4_record {
@@ -357,15 +358,7 @@ static void ip_pool_addr4_dump_cb(int error,
 
 static int ip_pool_init(void)
 {
-	const struct l_settings *settings = iwd_get_config();
-	bool netconfig_enabled;
-
-	if (!l_settings_get_bool(settings, "General",
-				"EnableNetworkConfiguration",
-				&netconfig_enabled))
-		netconfig_enabled = false;
-
-	if (!netconfig_enabled)
+	if (!netconfig_get_enabled())
 		return 0;
 
 	rtnl = iwd_get_rtnl();
src/main.c +2 -6
index b67c74d9..f5095ecc 100644
@@ -42,6 +42,7 @@
 #include "src/rfkill.h"
 #include "src/storage.h"
 #include "src/anqp.h"
+#include "src/netconfig.h"
 
 #include "src/backtrace.h"
 
@@ -217,12 +218,7 @@ static struct l_dbus_message *iwd_dbus_get_info(struct l_dbus *dbus,
 	struct l_dbus_message *reply;
 	struct l_dbus_message_builder *builder;
 	L_AUTO_FREE_VAR(char *, storage_dir) = storage_get_path(NULL);
-	bool netconfig_enabled;
-
-	if (!l_settings_get_bool(iwd_config, "General",
-					"EnableNetworkConfiguration",
-					&netconfig_enabled))
-		netconfig_enabled = false;
+	bool netconfig_enabled = netconfig_get_enabled();
 
 	reply = l_dbus_message_new_method_return(message);
 	builder = l_dbus_message_builder_new(reply);
src/netconfig.c +9 -0
index 2ffc5ab1..833fd3ab 100644
@@ -1581,6 +1581,15 @@ void netconfig_destroy(struct netconfig *netconfig)
 	netconfig_free(netconfig);
 }
 
+bool netconfig_get_enabled(void)
+{
+	bool enabled;
+
+	return l_settings_get_bool(iwd_get_config(), "General",
+					"EnableNetworkConfiguration",
+					&enabled) && enabled;
+}
+
 static int netconfig_init(void)
 {
 	uint32_t r;
src/netconfig.h +2 -0
index 3c4e7641..9d5887e9 100644
@@ -46,3 +46,5 @@ void netconfig_handle_fils_ip_resp(struct netconfig *netconfig,
 
 struct netconfig *netconfig_new(uint32_t ifindex);
 void netconfig_destroy(struct netconfig *netconfig);
+
+bool netconfig_get_enabled(void);
src/resolve.c +2 -10
index 9b724392..59b29ec2 100644
@@ -35,6 +35,7 @@
 #include "src/iwd.h"
 #include "src/module.h"
 #include "src/dbus.h"
+#include "src/netconfig.h"
 #include "src/resolve.h"
 
 struct resolve_ops {
@@ -579,18 +580,9 @@ static const struct {
 static int resolve_init(void)
 {
 	const char *method_name;
-	bool enabled;
 	uint8_t i;
 
-	if (!l_settings_get_bool(iwd_get_config(), "General",
-					"EnableNetworkConfiguration",
-					&enabled)) {
-		if (!l_settings_get_bool(iwd_get_config(), "General",
-					"enable_network_config", &enabled))
-			enabled = false;
-	}
-
-	if (!enabled)
+	if (!netconfig_get_enabled())
 		return 0;
 
 	method_name = l_settings_get_value(iwd_get_config(), "Network",
src/station.c +2 -8
index c73db634..d3767203 100644
@@ -63,7 +63,6 @@ static uint32_t netdev_watch;
 static uint32_t mfp_setting;
 static uint32_t roam_retry_interval;
 static bool anqp_disabled;
-static bool netconfig_enabled;
 static struct watchlist event_watches;
 
 struct station {
@@ -3859,7 +3858,7 @@ static struct station *station_create(struct netdev *netdev)
 	l_dbus_object_add_interface(dbus, netdev_get_path(netdev),
 					IWD_STATION_INTERFACE, station);
 
-	if (netconfig_enabled)
+	if (netconfig_get_enabled())
 		station->netconfig = netconfig_new(netdev_get_ifindex(netdev));
 
 	station->anqp_pending = l_queue_new();
@@ -4442,12 +4441,7 @@ static int station_init(void)
 				&anqp_disabled))
 		anqp_disabled = true;
 
-	if (!l_settings_get_bool(iwd_get_config(), "General",
-					"EnableNetworkConfiguration",
-					&netconfig_enabled))
-			netconfig_enabled = false;
-
-	if (!netconfig_enabled)
+	if (!netconfig_get_enabled())
 		l_info("station: Network configuration is disabled.");
 
 	watchlist_init(&event_watches, NULL);
Signature · 1 line
2.32.0
author
#1 lore raw

PATCH 2/12 netconfig: Cache the IPv6 l_rtnl_address object

For symmetry with netconfig->v4_address add a netconfig->v6_address so that we can track what the current address is at any time.

---
 src/netconfig.c | 44 ++++++++++++++++++++++++++++++++------------
 1 file changed, 32 insertions(+), 12 deletions(-)
src/netconfig.c +32 -12
index 833fd3ab..13c8c9b7 100644
@@ -58,6 +58,7 @@ struct netconfig {
 	uint8_t rtm_protocol;
 	uint8_t rtm_v6_protocol;
 	struct l_rtnl_address *v4_address;
+	struct l_rtnl_address *v6_address;
 	char **dns4_overrides;
 	char **dns6_overrides;
 	char *mdns;
@@ -131,6 +132,8 @@ static void netconfig_free_settings(struct netconfig *netconfig)
 {
 	l_rtnl_address_free(netconfig->v4_address);
 	netconfig->v4_address = NULL;
+	l_rtnl_address_free(netconfig->v6_address);
+	netconfig->v6_address = NULL;
 
 	l_strfreev(netconfig->dns4_overrides);
 	netconfig->dns4_overrides = NULL;
@@ -1031,13 +1034,28 @@ static void netconfig_dhcp6_event_handler(struct l_dhcp6_client *client,
 	case L_DHCP6_CLIENT_EVENT_IP_CHANGED:
 	case L_DHCP6_CLIENT_EVENT_LEASE_OBTAINED:
 	case L_DHCP6_CLIENT_EVENT_LEASE_RENEWED:
+	{
+		const struct l_dhcp6_lease *lease =
+			l_dhcp6_client_get_lease(netconfig->dhcp6_client);
+		_auto_(l_free) char *addr_str =
+			l_dhcp6_lease_get_address(lease);
+		struct l_rtnl_address *address;
+
+		address = l_rtnl_address_new(addr_str,
+					l_dhcp6_lease_get_prefix_length(lease));
+		l_rtnl_address_free(netconfig->v6_address);
+		netconfig->v6_address = address;
+
 		netconfig_set_dns(netconfig);
 		netconfig_set_domains(netconfig);
 		break;
+	}
 	case L_DHCP6_CLIENT_EVENT_LEASE_EXPIRED:
 		l_debug("Lease for interface %u expired", netconfig->ifindex);
 		netconfig_set_dns(netconfig);
 		netconfig_set_domains(netconfig);
+		l_rtnl_address_free(netconfig->v6_address);
+		netconfig->v6_address = NULL;
 
 		/* Fall through */
 	case L_DHCP6_CLIENT_EVENT_NO_LEASE:
@@ -1187,7 +1205,6 @@ static bool netconfig_ipv4_select_and_install(struct netconfig *netconfig)
 static bool netconfig_ipv6_select_and_install(struct netconfig *netconfig)
 {
 	struct netdev *netdev = netdev_find(netconfig->ifindex);
-	struct l_rtnl_address *address = NULL;
 
 	if (netconfig->rtm_v6_protocol == RTPROT_UNSPEC) {
 		l_debug("IPV6 configuration disabled");
@@ -1196,10 +1213,7 @@ static bool netconfig_ipv6_select_and_install(struct netconfig *netconfig)
 
 	sysfs_write_ipv6_setting(netdev_get_name(netdev), "disable_ipv6", "0");
 
-	if (netconfig->rtm_v6_protocol == RTPROT_STATIC)
-		address = netconfig_get_static6_address(
-						netconfig->active_settings);
-	else if (netconfig->rtm_v6_protocol == RTPROT_DHCP &&
+	if (netconfig->rtm_v6_protocol == RTPROT_DHCP &&
 			netconfig->fils_override &&
 			!l_memeqzero(netconfig->fils_override->ipv6_addr, 16)) {
 		uint8_t prefix_len = netconfig->fils_override->ipv6_prefix_len;
@@ -1209,11 +1223,12 @@ static bool netconfig_ipv6_select_and_install(struct netconfig *netconfig)
 		if (unlikely(!addr_str))
 			return false;
 
-		if (L_WARN_ON(unlikely(!(address = l_rtnl_address_new(addr_str,
-								prefix_len)))))
+		netconfig->v6_address = l_rtnl_address_new(addr_str,
+								prefix_len);
+		if (L_WARN_ON(unlikely(!netconfig->v6_address)))
 			return false;
 
-		l_rtnl_address_set_noprefixroute(address, true);
+		l_rtnl_address_set_noprefixroute(netconfig->v6_address, true);
 
 		/*
 		 * TODO: If netconfig->fils_override->ipv6_lifetime is set,
@@ -1223,12 +1238,12 @@ static bool netconfig_ipv6_select_and_install(struct netconfig *netconfig)
 		 */
 	}
 
-	if (address) {
+	if (netconfig->v6_address) {
 		L_WARN_ON(!(netconfig->addr6_add_cmd_id =
-			l_rtnl_ifaddr_add(rtnl, netconfig->ifindex, address,
+			l_rtnl_ifaddr_add(rtnl, netconfig->ifindex,
+					netconfig->v6_address,
 					netconfig_ipv6_ifaddr_add_cmd_cb,
 					netconfig, NULL)));
-		l_rtnl_address_free(address);
 		return true;
 	}
 
@@ -1345,7 +1360,6 @@ bool netconfig_load_settings(struct netconfig *netconfig,
 
 	if (l_settings_has_key(active_settings, "IPv6", "Address")) {
 		v6_address = netconfig_get_static6_address(active_settings);
-		l_rtnl_address_free(v6_address);
 
 		if (unlikely(!v6_address)) {
 			l_error("netconfig: Can't parse IPv6 address");
@@ -1371,6 +1385,9 @@ bool netconfig_load_settings(struct netconfig *netconfig,
 	if (netconfig->rtm_protocol == RTPROT_STATIC)
 		netconfig->v4_address = v4_address;
 
+	if (netconfig->rtm_v6_protocol == RTPROT_STATIC)
+		netconfig->v6_address = v6_address;
+
 	netconfig->active_settings = active_settings;
 	netconfig->dns4_overrides = dns4_overrides;
 	netconfig->dns6_overrides = dns6_overrides;
@@ -1457,6 +1474,9 @@ bool netconfig_reset(struct netconfig *netconfig)
 	netconfig_reset_v4(netconfig);
 
 	if (netconfig->rtm_v6_protocol) {
+		l_rtnl_address_free(netconfig->v6_address);
+		netconfig->v6_address = NULL;
+
 		l_strfreev(netconfig->dns6_overrides);
 		netconfig->dns6_overrides = NULL;
 
Signature · 1 line
2.32.0
Applied
#2 lore raw

Re: [PATCH 02/12] netconfig: Cache the IPv6 l_rtnl_address object

Hi Andrew,

Quoted 6 lines · Andrew Zaborowski

For symmetry with netconfig->v4_address add a netconfig->v6_address so that we can track what the current address is at any time.

---
  src/netconfig.c | 44 ++++++++++++++++++++++++++++++++------------
  1 file changed, 32 insertions(+), 12 deletions(-)

Applied, thanks.

Regards,
-Denis

author
#3 lore raw

PATCH 3/12 netconfig: Track gateway address strings

In reply to Andrew Zaborowski

Cache the latest v4 and v6 gateway IP string in struct netconfig state to be able to more easily detect changes in those values in future commits and perhaps to simplify the ..._routes_install functions. netconfig_ipv4_get_gateway's out_mac parameter can now be NULL. While editing that function fix a small formatting annoyance.

---
 src/netconfig.c | 62 +++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 50 insertions(+), 12 deletions(-)
src/netconfig.c +50 -12
index 13c8c9b7..29a1dbde 100644
@@ -63,6 +63,8 @@ struct netconfig {
 	char **dns6_overrides;
 	char *mdns;
 	struct ie_fils_ip_addr_response_info *fils_override;
+	char *v4_gateway_str;
+	char *v6_gateway_str;
 
 	const struct l_settings *active_settings;
 
@@ -436,6 +438,8 @@ static char *netconfig_ipv4_get_gateway(struct netconfig *netconfig,
 {
 	const struct l_dhcp_lease *lease;
 	char *gateway;
+	const struct ie_fils_ip_addr_response_info *fils =
+		netconfig->fils_override;
 
 	switch (netconfig->rtm_protocol) {
 	case RTPROT_STATIC:
@@ -449,15 +453,12 @@ static char *netconfig_ipv4_get_gateway(struct netconfig *netconfig,
 		return gateway;
 
 	case RTPROT_DHCP:
-		if (netconfig->fils_override &&
-				netconfig->fils_override->ipv4_gateway) {
-			gateway = netconfig_ipv4_to_string(
-					netconfig->fils_override->ipv4_gateway);
+		if (fils && fils->ipv4_gateway) {
+			gateway = netconfig_ipv4_to_string(fils->ipv4_gateway);
 
-			if (gateway && !l_memeqzero(netconfig->fils_override->
-							ipv4_gateway_mac, 6))
-				*out_mac = netconfig->fils_override->
-					ipv4_gateway_mac;
+			if (gateway && out_mac &&
+					!l_memeqzero(fils->ipv4_gateway_mac, 6))
+				*out_mac = fils->ipv4_gateway_mac;
 
 			return gateway;
 		}
@@ -495,7 +496,7 @@ static struct l_rtnl_address *netconfig_get_static6_address(
 	prefix_len = strtoul(p, NULL, 10);
 	if (!unlikely(errno == EINVAL || errno == ERANGE ||
 			!prefix_len || prefix_len > 128)) {
-		l_error("netconfig: Invalid prefix '%s'  provided in network"
+		l_error("netconfig: Invalid prefix '%s' provided in network"
 				" configuration file", p);
 		return NULL;
 	}
@@ -511,6 +512,7 @@ no_prefix_len:
 
 static struct l_rtnl_route *netconfig_get_static6_gateway(
 						struct netconfig *netconfig,
+						char **out_str,
 						const uint8_t **out_mac)
 {
 	L_AUTO_FREE_VAR(char *, gateway);
@@ -541,6 +543,7 @@ static struct l_rtnl_route *netconfig_get_static6_gateway(
 
 	l_rtnl_route_set_priority(ret, ROUTE_PRIORITY_OFFSET);
 	l_rtnl_route_set_protocol(ret, RTPROT_STATIC);
+	*out_str = l_steal_ptr(gateway);
 	*out_mac = mac;
 
 	return ret;
@@ -924,7 +927,9 @@ static void netconfig_ipv6_ifaddr_add_cmd_cb(int error, uint16_t type,
 		return;
 	}
 
-	gateway = netconfig_get_static6_gateway(netconfig, &gateway_mac);
+	gateway = netconfig_get_static6_gateway(netconfig,
+						&netconfig->v6_gateway_str,
+						&gateway_mac);
 	if (gateway) {
 		netconfig->route6_add_cmd_id = l_rtnl_route_add(rtnl,
 							netconfig->ifindex,
@@ -981,10 +986,24 @@ static void netconfig_ipv4_dhcp_event_handler(struct l_dhcp_client *client,
 					netconfig->v4_address,
 					netconfig_ifaddr_del_cmd_cb,
 					netconfig, NULL));
-		l_rtnl_address_free(netconfig->v4_address);
 		/* Fall through. */
 	case L_DHCP_CLIENT_EVENT_LEASE_OBTAINED:
-		netconfig->v4_address = netconfig_get_dhcp4_address(netconfig);
+	{
+		char *gateway_str;
+		struct l_rtnl_address *address;
+
+		gateway_str = netconfig_ipv4_get_gateway(netconfig, NULL);
+		if (l_streq0(netconfig->v4_gateway_str, gateway_str))
+			l_free(gateway_str);
+		else {
+			l_free(netconfig->v4_gateway_str);
+			netconfig->v4_gateway_str = gateway_str;
+		}
+
+		address = netconfig_get_dhcp4_address(netconfig);
+		l_rtnl_address_free(netconfig->v4_address);
+		netconfig->v4_address = address;
+
 		if (!netconfig->v4_address) {
 			l_error("netconfig: Failed to obtain IP addresses from "
 							"DHCPv4 lease.");
@@ -997,6 +1016,7 @@ static void netconfig_ipv4_dhcp_event_handler(struct l_dhcp_client *client,
 					netconfig_ipv4_ifaddr_add_cmd_cb,
 					netconfig, NULL)));
 		break;
+	}
 	case L_DHCP_CLIENT_EVENT_LEASE_RENEWED:
 		break;
 	case L_DHCP_CLIENT_EVENT_LEASE_EXPIRED:
@@ -1006,6 +1026,7 @@ static void netconfig_ipv4_dhcp_event_handler(struct l_dhcp_client *client,
 					netconfig, NULL));
 		l_rtnl_address_free(netconfig->v4_address);
 		netconfig->v4_address = NULL;
+		l_free(l_steal_ptr(netconfig->v4_gateway_str));
 
 		/* Fall through. */
 	case L_DHCP_CLIENT_EVENT_NO_LEASE:
@@ -1040,6 +1061,18 @@ static void netconfig_dhcp6_event_handler(struct l_dhcp6_client *client,
 		_auto_(l_free) char *addr_str =
 			l_dhcp6_lease_get_address(lease);
 		struct l_rtnl_address *address;
+		struct l_icmp6_client *icmp6 =
+			l_dhcp6_client_get_icmp6(netconfig->dhcp6_client);
+		const struct l_icmp6_router *router =
+			l_icmp6_client_get_router(icmp6);
+		char *gateway_str = l_icmp6_router_get_address(router);
+
+		if (l_streq0(netconfig->v6_gateway_str, gateway_str))
+			l_free(gateway_str);
+		else {
+			l_free(netconfig->v6_gateway_str);
+			netconfig->v6_gateway_str = gateway_str;
+		}
 
 		address = l_rtnl_address_new(addr_str,
 					l_dhcp6_lease_get_prefix_length(lease));
@@ -1056,6 +1089,7 @@ static void netconfig_dhcp6_event_handler(struct l_dhcp6_client *client,
 		netconfig_set_domains(netconfig);
 		l_rtnl_address_free(netconfig->v6_address);
 		netconfig->v6_address = NULL;
+		l_free(l_steal_ptr(netconfig->v6_gateway_str));
 
 		/* Fall through */
 	case L_DHCP6_CLIENT_EVENT_NO_LEASE:
@@ -1092,6 +1126,8 @@ static void netconfig_reset_v4(struct netconfig *netconfig)
 
 		l_acd_destroy(netconfig->acd);
 		netconfig->acd = NULL;
+
+		l_free(l_steal_ptr(netconfig->v4_gateway_str));
 	}
 }
 
@@ -1485,6 +1521,8 @@ bool netconfig_reset(struct netconfig *netconfig)
 
 		sysfs_write_ipv6_setting(netdev_get_name(netdev),
 						"disable_ipv6", "1");
+
+		l_free(l_steal_ptr(netconfig->v6_gateway_str));
 	}
 
 	l_free(l_steal_ptr(netconfig->fils_override));
Signature · 1 line
2.32.0
#4 lore raw

Re: [PATCH 03/12] netconfig: Track gateway address strings

Hi Andrew,

Quoted 9 lines · Andrew Zaborowski

Cache the latest v4 and v6 gateway IP string in struct netconfig state to be able to more easily detect changes in those values in future commits and perhaps to simplify the ..._routes_install functions. netconfig_ipv4_get_gateway's out_mac parameter can now be NULL. While editing that function fix a small formatting annoyance.

---
  src/netconfig.c | 62 +++++++++++++++++++++++++++++++++++++++----------
  1 file changed, 50 insertions(+), 12 deletions(-)

<snip>

Quoted 31 lines
src/netconfig.c +7 -8
@@ -436,6 +438,8 @@ static char *netconfig_ipv4_get_gateway(struct netconfig *netconfig,
  {
  	const struct l_dhcp_lease *lease;
  	char *gateway;
+	const struct ie_fils_ip_addr_response_info *fils =
+		netconfig->fils_override;
 
  	switch (netconfig->rtm_protocol) {
  	case RTPROT_STATIC:
@@ -449,15 +453,12 @@ static char *netconfig_ipv4_get_gateway(struct netconfig *netconfig,
  		return gateway;
 
  	case RTPROT_DHCP:
-		if (netconfig->fils_override &&
-				netconfig->fils_override->ipv4_gateway) {
-			gateway = netconfig_ipv4_to_string(
-					netconfig->fils_override->ipv4_gateway);
+		if (fils && fils->ipv4_gateway) {
+			gateway = netconfig_ipv4_to_string(fils->ipv4_gateway);
 
-			if (gateway && !l_memeqzero(netconfig->fils_override->
-							ipv4_gateway_mac, 6))
-				*out_mac = netconfig->fils_override->
-					ipv4_gateway_mac;
+			if (gateway && out_mac &&
+					!l_memeqzero(fils->ipv4_gateway_mac, 6))
+				*out_mac = fils->ipv4_gateway_mac;
 
  			return gateway;
  		}

src/netconfig.c static struct l_rtnl_address *netconfig_get_static6_address( · line 502

Quoted 8 lines
src/netconfig.c +1 -1
@@ -495,7 +496,7 @@ static struct l_rtnl_address *netconfig_get_static6_address(
  	prefix_len = strtoul(p, NULL, 10);
  	if (!unlikely(errno == EINVAL || errno == ERANGE ||
  			!prefix_len || prefix_len > 128)) {
-		l_error("netconfig: Invalid prefix '%s'  provided in network"
+		l_error("netconfig: Invalid prefix '%s' provided in network"
  				" configuration file", p);
  		return NULL;
  	}

I separated out these 3 chunks into 2 separate commits since they did not seem to be related to the commit description.

So this commit became 3. Please review. Applied, thanks.

Regards,
-Denis

author
#5 lore raw

PATCH 4/12 netconfig: Track DNS address string lists

In reply to Andrew Zaborowski

Cache the latest v4 and v6 DNS IP string lists in struct netconfig state to be able to more easily detect changes in those values in future commits. For that split netconfig_set_dns's code into this function, which now only commit the values in netconfig->dns{4,6}_list to the resolver, and netconfig_dns_list_update() which figures out the active DNS IP address list and saves it in netconfig->dns{4,6} list. This probably saves some cycles as the callers can now decide to only recalculate the dns_list which may have changed.

While there simplify netconfig_set_dns return type to void as the result was always 0 anyway and was never checked by callers.

---
 src/netconfig.c | 101 ++++++++++++++++++++++++++++--------------------
 1 file changed, 60 insertions(+), 41 deletions(-)
src/netconfig.c +60 -41
index 29a1dbde..3856b264 100644
@@ -61,6 +61,8 @@ struct netconfig {
 	struct l_rtnl_address *v6_address;
 	char **dns4_overrides;
 	char **dns6_overrides;
+	char **dns4_list;
+	char **dns6_list;
 	char *mdns;
 	struct ie_fils_ip_addr_response_info *fils_override;
 	char *v4_gateway_str;
@@ -278,52 +280,61 @@ static void netconfig_set_neighbor_entry_cb(int error,
 			strerror(-error), error);
 }
 
-static int netconfig_set_dns(struct netconfig *netconfig)
+static void netconfig_set_dns(struct netconfig *netconfig)
 {
-	const uint8_t *fils_dns4_mac = NULL;
-	const uint8_t *fils_dns6_mac = NULL;
-	char **dns4_list = netconfig_get_dns_list(netconfig, AF_INET,
-							&fils_dns4_mac);
-	char **dns6_list = netconfig_get_dns_list(netconfig, AF_INET6,
-							&fils_dns6_mac);
-	unsigned int n_entries4 = l_strv_length(dns4_list);
-	unsigned int n_entries6 = l_strv_length(dns6_list);
-	char **dns_list;
-	const struct ie_fils_ip_addr_response_info *fils =
-		netconfig->fils_override;
-
-	if (!dns4_list && !dns6_list)
-		return 0;
+	if (!netconfig->dns4_list && !netconfig->dns6_list)
+		return;
 
-	dns_list = dns4_list;
+	if (netconfig->dns4_list && netconfig->dns6_list) {
+		unsigned int n_entries4 = l_strv_length(netconfig->dns4_list);
+		unsigned int n_entries6 = l_strv_length(netconfig->dns6_list);
+		char **dns_list = l_malloc(sizeof(char *) *
+					(n_entries4 + n_entries6 + 1));
 
-	if (dns6_list) {
-		dns_list = l_realloc(dns_list,
-				sizeof(char *) * (n_entries4 + n_entries6 + 1));
-		memcpy(dns_list + n_entries4, dns6_list,
+		memcpy(dns_list, netconfig->dns4_list,
+			sizeof(char *) * n_entries4);
+		memcpy(dns_list + n_entries4, netconfig->dns6_list,
 			sizeof(char *) * (n_entries6 + 1));
-		/* Contents now belong to dns_list, so no l_strfreev */
-		l_free(dns6_list);
+		resolve_set_dns(netconfig->resolve, dns_list);
+		l_free(dns_list);
+		return;
 	}
 
-	resolve_set_dns(netconfig->resolve, dns_list);
-	l_strv_free(dns_list);
+	resolve_set_dns(netconfig->resolve,
+			netconfig->dns4_list ?: netconfig->dns6_list);
+}
 
-	if (fils_dns4_mac && !l_rtnl_neighbor_set_hwaddr(rtnl,
-					netconfig->ifindex, AF_INET,
-					&fils->ipv4_dns, fils_dns4_mac, 6,
-					netconfig_set_neighbor_entry_cb, NULL,
-					NULL))
-		l_debug("l_rtnl_neighbor_set_hwaddr failed");
+static bool netconfig_dns_list_update(struct netconfig *netconfig, uint8_t af)
+{
+	const uint8_t *fils_dns_mac = NULL;
+	char ***dns_list_ptr = af == AF_INET ?
+		&netconfig->dns4_list : &netconfig->dns6_list;
+	char **new_dns_list = netconfig_get_dns_list(netconfig, af,
+							&fils_dns_mac);
+
+	if (l_strv_eq(*dns_list_ptr, new_dns_list)) {
+		l_strv_free(new_dns_list);
+		return false;
+	}
 
-	if (fils_dns6_mac && !l_rtnl_neighbor_set_hwaddr(rtnl,
-					netconfig->ifindex, AF_INET6,
-					fils->ipv6_dns, fils_dns6_mac, 6,
-					netconfig_set_neighbor_entry_cb, NULL,
-					NULL))
-		l_debug("l_rtnl_neighbor_set_hwaddr failed");
+	l_strv_free(*dns_list_ptr);
+	*dns_list_ptr = new_dns_list;
 
-	return 0;
+	if (fils_dns_mac) {
+		const struct ie_fils_ip_addr_response_info *fils =
+			netconfig->fils_override;
+		const void *dns_ip = af == AF_INET ?
+			(const void *) &fils->ipv4_dns :
+			(const void *) &fils->ipv6_dns;
+
+		if (!l_rtnl_neighbor_set_hwaddr(rtnl, netconfig->ifindex, af,
+						dns_ip, fils_dns_mac, 6,
+						netconfig_set_neighbor_entry_cb,
+						NULL, NULL))
+			l_debug("l_rtnl_neighbor_set_hwaddr failed");
+	}
+
+	return true;
 }
 
 static void append_domain(char **domains, unsigned int *n_domains,
@@ -1010,6 +1021,8 @@ static void netconfig_ipv4_dhcp_event_handler(struct l_dhcp_client *client,
 			return;
 		}
 
+		netconfig_dns_list_update(netconfig, AF_INET);
+
 		L_WARN_ON(!(netconfig->addr4_add_cmd_id =
 				l_rtnl_ifaddr_add(rtnl, netconfig->ifindex,
 					netconfig->v4_address,
@@ -1079,12 +1092,14 @@ static void netconfig_dhcp6_event_handler(struct l_dhcp6_client *client,
 		l_rtnl_address_free(netconfig->v6_address);
 		netconfig->v6_address = address;
 
+		netconfig_dns_list_update(netconfig, AF_INET6);
 		netconfig_set_dns(netconfig);
 		netconfig_set_domains(netconfig);
 		break;
 	}
 	case L_DHCP6_CLIENT_EVENT_LEASE_EXPIRED:
 		l_debug("Lease for interface %u expired", netconfig->ifindex);
+		netconfig_dns_list_update(netconfig, AF_INET6);
 		netconfig_set_dns(netconfig);
 		netconfig_set_domains(netconfig);
 		l_rtnl_address_free(netconfig->v6_address);
@@ -1118,8 +1133,8 @@ static void netconfig_reset_v4(struct netconfig *netconfig)
 	if (netconfig->rtm_protocol) {
 		netconfig_remove_v4_address(netconfig);
 
-		l_strfreev(netconfig->dns4_overrides);
-		netconfig->dns4_overrides = NULL;
+		l_strv_free(l_steal_ptr(netconfig->dns4_overrides));
+		l_strv_free(l_steal_ptr(netconfig->dns4_list));
 
 		l_dhcp_client_stop(netconfig->dhcp_client);
 		netconfig->rtm_protocol = 0;
@@ -1204,6 +1219,8 @@ static bool netconfig_ipv4_select_and_install(struct netconfig *netconfig)
 							ip)))
 			return false;
 
+		netconfig_dns_list_update(netconfig, AF_INET);
+
 		netconfig->acd = l_acd_new(netconfig->ifindex);
 		l_acd_set_event_handler(netconfig->acd,
 					netconfig_ipv4_acd_event, netconfig,
@@ -1275,6 +1292,8 @@ static bool netconfig_ipv6_select_and_install(struct netconfig *netconfig)
 	}
 
 	if (netconfig->v6_address) {
+		netconfig_dns_list_update(netconfig, AF_INET6);
+
 		L_WARN_ON(!(netconfig->addr6_add_cmd_id =
 			l_rtnl_ifaddr_add(rtnl, netconfig->ifindex,
 					netconfig->v6_address,
@@ -1513,8 +1532,8 @@ bool netconfig_reset(struct netconfig *netconfig)
 		l_rtnl_address_free(netconfig->v6_address);
 		netconfig->v6_address = NULL;
 
-		l_strfreev(netconfig->dns6_overrides);
-		netconfig->dns6_overrides = NULL;
+		l_strv_free(l_steal_ptr(netconfig->dns6_overrides));
+		l_strv_free(l_steal_ptr(netconfig->dns6_list));
 
 		l_dhcp6_client_stop(netconfig->dhcp6_client);
 		netconfig->rtm_v6_protocol = 0;
Signature · 1 line
2.32.0
#6 lore raw

Re: [PATCH 04/12] netconfig: Track DNS address string lists

Hi Andrew,

Quoted 9 lines · Andrew Zaborowski

Cache the latest v4 and v6 DNS IP string lists in struct netconfig state to be able to more easily detect changes in those values in future commits. For that split netconfig_set_dns's code into this function, which now only commit the values in netconfig->dns{4,6}_list to the resolver, and netconfig_dns_list_update() which figures out the active DNS IP address list and saves it in netconfig->dns{4,6} list. This probably saves some cycles as the callers can now decide to only recalculate the dns_list which may have changed.

Quoted 6 lines

While there simplify netconfig_set_dns return type to void as the result was always 0 anyway and was never checked by callers.

---
  src/netconfig.c | 101 ++++++++++++++++++++++++++++--------------------
  1 file changed, 60 insertions(+), 41 deletions(-)

Patches 4-6 applied, thanks.

Regards,
-Denis

author
#7 lore raw

PATCH 5/12 netconfig: Track local domains lists

In reply to Andrew Zaborowski

Cache the latest v4 and v6 domain string lists in struct netconfig state to be able to more easily detect changes in those values in future commits. For that split netconfig_set_domains's code into this function, which now only commits the values in netconfig->v{4,6}_domain{,s} to the resolver, and netconfig_domains_update() which figures out the active domains string list and saves it into netconfig->v{4,6}_domain{,s}. This probably saves some cycles as the callers can now decide to only recalculate the domains list which may have changed.

While there simplify netconfig_set_domains return type to void as the result was always 0 anyway and was never checked by callers.

---
 src/netconfig.c | 88 ++++++++++++++++++++++++++++++++++---------------
 1 file changed, 61 insertions(+), 27 deletions(-)
src/netconfig.c +61 -27
index 3856b264..f4bf944e 100644
@@ -67,6 +67,8 @@ struct netconfig {
 	struct ie_fils_ip_addr_response_info *fils_override;
 	char *v4_gateway_str;
 	char *v6_gateway_str;
+	char *v4_domain;
+	char **v6_domains;
 
 	const struct l_settings *active_settings;
 
@@ -353,47 +355,71 @@ static void append_domain(char **domains, unsigned int *n_domains,
 	*n_domains += 1;
 }
 
-static int netconfig_set_domains(struct netconfig *netconfig)
+static void netconfig_set_domains(struct netconfig *netconfig)
 {
 	char *domains[31];
 	unsigned int n_domains = 0;
-	char *v4_domain = NULL;
-	char **v6_domains = NULL;
 	char **p;
 
 	memset(domains, 0, sizeof(domains));
 
-	/* Allow to override the DHCP domain name with setting entry. */
-	v4_domain = l_settings_get_string(netconfig->active_settings,
-							"IPv4", "DomainName");
-	if (!v4_domain && netconfig->rtm_protocol == RTPROT_DHCP) {
-		const struct l_dhcp_lease *lease =
-			l_dhcp_client_get_lease(netconfig->dhcp_client);
+	append_domain(domains, &n_domains,
+			L_ARRAY_SIZE(domains) - 1, netconfig->v4_domain);
 
-		if (lease)
-			v4_domain = l_dhcp_lease_get_domain_name(lease);
-	}
+	for (p = netconfig->v6_domains; p && *p; p++)
+		append_domain(domains, &n_domains,
+				L_ARRAY_SIZE(domains) - 1, *p);
 
-	append_domain(domains, &n_domains,
-					L_ARRAY_SIZE(domains) - 1, v4_domain);
+	resolve_set_domains(netconfig->resolve, domains);
+}
 
-	if (netconfig->rtm_v6_protocol == RTPROT_DHCP) {
-		const struct l_dhcp6_lease *lease =
-			l_dhcp6_client_get_lease(netconfig->dhcp6_client);
+static bool netconfig_domains_update(struct netconfig *netconfig, uint8_t af)
+{
+	bool changed = false;
 
-		if (lease)
-			v6_domains = l_dhcp6_lease_get_domains(lease);
-	}
+	if (af == AF_INET) {
+		/* Allow to override the DHCP domain name with setting entry. */
+		char *v4_domain = l_settings_get_string(
+						netconfig->active_settings,
+						"IPv4", "DomainName");
 
-	for (p = v6_domains; p && *p; p++)
-		append_domain(domains, &n_domains,
-					L_ARRAY_SIZE(domains) - 1, *p);
+		if (!v4_domain && netconfig->rtm_protocol == RTPROT_DHCP) {
+			const struct l_dhcp_lease *lease =
+				l_dhcp_client_get_lease(netconfig->dhcp_client);
 
-	resolve_set_domains(netconfig->resolve, domains);
-	l_strv_free(v6_domains);
-	l_free(v4_domain);
+			if (lease)
+				v4_domain = l_dhcp_lease_get_domain_name(lease);
+		}
 
-	return 0;
+		if (l_streq0(v4_domain, netconfig->v4_domain))
+			l_free(v4_domain);
+		else {
+			l_free(netconfig->v4_domain);
+			netconfig->v4_domain = v4_domain;
+			changed = true;
+		}
+	} else {
+		char **v6_domains = NULL;
+
+		if (netconfig->rtm_v6_protocol == RTPROT_DHCP) {
+			const struct l_dhcp6_lease *lease =
+				l_dhcp6_client_get_lease(
+						netconfig->dhcp6_client);
+
+			if (lease)
+				v6_domains = l_dhcp6_lease_get_domains(lease);
+		}
+
+		if (l_strv_eq(netconfig->v6_domains, v6_domains))
+			l_strv_free(v6_domains);
+		else {
+			l_strv_free(netconfig->v6_domains);
+			netconfig->v6_domains = v6_domains;
+			changed = true;
+		}
+	}
+
+	return changed;
 }
 
 static struct l_rtnl_address *netconfig_get_static4_address(
@@ -1022,6 +1048,7 @@ static void netconfig_ipv4_dhcp_event_handler(struct l_dhcp_client *client,
 		}
 
 		netconfig_dns_list_update(netconfig, AF_INET);
+		netconfig_domains_update(netconfig, AF_INET);
 
 		L_WARN_ON(!(netconfig->addr4_add_cmd_id =
 				l_rtnl_ifaddr_add(rtnl, netconfig->ifindex,
@@ -1093,6 +1120,7 @@ static void netconfig_dhcp6_event_handler(struct l_dhcp6_client *client,
 		netconfig->v6_address = address;
 
 		netconfig_dns_list_update(netconfig, AF_INET6);
+		netconfig_domains_update(netconfig, AF_INET6);
 		netconfig_set_dns(netconfig);
 		netconfig_set_domains(netconfig);
 		break;
@@ -1100,6 +1128,7 @@ static void netconfig_dhcp6_event_handler(struct l_dhcp6_client *client,
 	case L_DHCP6_CLIENT_EVENT_LEASE_EXPIRED:
 		l_debug("Lease for interface %u expired", netconfig->ifindex);
 		netconfig_dns_list_update(netconfig, AF_INET6);
+		netconfig_domains_update(netconfig, AF_INET6);
 		netconfig_set_dns(netconfig);
 		netconfig_set_domains(netconfig);
 		l_rtnl_address_free(netconfig->v6_address);
@@ -1143,6 +1172,8 @@ static void netconfig_reset_v4(struct netconfig *netconfig)
 		netconfig->acd = NULL;
 
 		l_free(l_steal_ptr(netconfig->v4_gateway_str));
+
+		l_free(l_steal_ptr(netconfig->v4_domain));
 	}
 }
 
@@ -1220,6 +1251,7 @@ static bool netconfig_ipv4_select_and_install(struct netconfig *netconfig)
 			return false;
 
 		netconfig_dns_list_update(netconfig, AF_INET);
+		netconfig_domains_update(netconfig, AF_INET);
 
 		netconfig->acd = l_acd_new(netconfig->ifindex);
 		l_acd_set_event_handler(netconfig->acd,
@@ -1542,6 +1574,8 @@ bool netconfig_reset(struct netconfig *netconfig)
 						"disable_ipv6", "1");
 
 		l_free(l_steal_ptr(netconfig->v6_gateway_str));
+
+		l_strv_free(l_steal_ptr(netconfig->v6_domains));
 	}
 
 	l_free(l_steal_ptr(netconfig->fils_override));
Signature · 1 line
2.32.0
author
#8 lore raw

PATCH 6/12 netconfig: Convert netconfig_load_settings to use _auto_

In reply to Andrew Zaborowski

As requested do the cleanup in netconfig_load_settings using ell's private _auto_() macro.

---
 src/netconfig.c | 38 ++++++++++++++------------------------
 1 file changed, 14 insertions(+), 24 deletions(-)
src/netconfig.c +14 -24
index f4bf944e..7b4b7f8a 100644
@@ -1373,14 +1373,14 @@ static int validate_dns_list(int family, char **dns_list)
 bool netconfig_load_settings(struct netconfig *netconfig,
 				const struct l_settings *active_settings)
 {
-	char *mdns;
+	_auto_(l_free) char *mdns = NULL;
 	bool send_hostname;
 	bool v6_enabled;
 	char hostname[HOST_NAME_MAX + 1];
-	char **dns4_overrides = NULL;
-	char **dns6_overrides = NULL;
-	struct l_rtnl_address *v4_address = NULL;
-	struct l_rtnl_address *v6_address = NULL;
+	_auto_(l_strv_free) char **dns4_overrides = NULL;
+	_auto_(l_strv_free) char **dns6_overrides = NULL;
+	_auto_(l_rtnl_address_free) struct l_rtnl_address *v4_address = NULL;
+	_auto_(l_rtnl_address_free) struct l_rtnl_address *v6_address = NULL;
 
 	dns4_overrides = l_settings_get_string_list(active_settings,
 							"IPv4", "DNS", ' ');
@@ -1392,7 +1392,7 @@ bool netconfig_load_settings(struct netconfig *netconfig,
 			dns4_overrides = NULL;
 
 			if (r < 0)
-				goto err_dns4;
+				return false;
 		}
 
 		if (r == 0)
@@ -1410,7 +1410,7 @@ bool netconfig_load_settings(struct netconfig *netconfig,
 			dns6_overrides = NULL;
 
 			if (r < 0)
-				goto err_dns6;
+				return false;
 		}
 
 		if (r == 0)
@@ -1437,7 +1437,7 @@ bool netconfig_load_settings(struct netconfig *netconfig,
 
 		if (unlikely(!v4_address)) {
 			l_error("netconfig: Can't parse IPv4 address");
-			goto err_v4_addr;
+			return false;
 		}
 	}
 
@@ -1450,7 +1450,7 @@ bool netconfig_load_settings(struct netconfig *netconfig,
 
 		if (unlikely(!v6_address)) {
 			l_error("netconfig: Can't parse IPv6 address");
-			goto err_v6_addr;
+			return false;
 		}
 	}
 
@@ -1470,26 +1470,16 @@ bool netconfig_load_settings(struct netconfig *netconfig,
 	netconfig_free_settings(netconfig);
 
 	if (netconfig->rtm_protocol == RTPROT_STATIC)
-		netconfig->v4_address = v4_address;
+		netconfig->v4_address = l_steal_ptr(v4_address);
 
 	if (netconfig->rtm_v6_protocol == RTPROT_STATIC)
-		netconfig->v6_address = v6_address;
+		netconfig->v6_address = l_steal_ptr(v6_address);
 
 	netconfig->active_settings = active_settings;
-	netconfig->dns4_overrides = dns4_overrides;
-	netconfig->dns6_overrides = dns6_overrides;
-	netconfig->mdns = mdns;
+	netconfig->dns4_overrides = l_steal_ptr(dns4_overrides);
+	netconfig->dns6_overrides = l_steal_ptr(dns6_overrides);
+	netconfig->mdns = l_steal_ptr(mdns);
 	return true;
-
-err_v6_addr:
-	l_rtnl_address_free(v4_address);
-err_v4_addr:
-	l_free(mdns);
-	l_strfreev(dns6_overrides);
-err_dns6:
-	l_strfreev(dns4_overrides);
-err_dns4:
-	return false;
 }
 
 bool netconfig_configure(struct netconfig *netconfig,
Signature · 1 line
2.32.0
author
#9 lore raw

PATCH 7/12 netconfig: Refactor setting new values to system

In reply to Andrew Zaborowski

Refactor and unify how the netconfig values, that we obtain from DHCP or the settings or otherwise, are written/committed to the system netdevs. We basically concentrate all of those actions in a new function netconfig_set() that is called whenever any value changes. The only exceptions are the sysfs writes and some of the ARP table writes.

This will allow us to more easily delegate committing the values to an (optional) D-Bus agent, or expose them as D-Bus properties if we ever choose to do that in the future. For that I'm also passing a bitmask telling netconfig_set() which values may have actually changed, so we don't bother looking at those that definitely haven't changed.
---
This is a relatively big change to review but the v4 and v6 portions are similar.

---
 src/netconfig.c | 541 +++++++++++++++++++++++++++++++++++-------------
 1 file changed, 398 insertions(+), 143 deletions(-)
src/netconfig.c +398 -143
index 7b4b7f8a..a73afe24 100644
@@ -51,6 +51,15 @@
 #include "src/ie.h"
 #include "src/netconfig.h"
 
+enum netconfig_changed_flags {
+	NETCONFIG_CHANGED_ADDRESS	= 1 << 0,
+	NETCONFIG_CHANGED_NETMASK	= 1 << 1,
+	NETCONFIG_CHANGED_GATEWAY	= 1 << 2,
+	NETCONFIG_CHANGED_DNS		= 1 << 3,
+	NETCONFIG_CHANGED_DOMAINS	= 1 << 4,
+	NETCONFIG_CHANGED_ALL		= 0x1f,
+};
+
 struct netconfig {
 	uint32_t ifindex;
 	struct l_dhcp_client *dhcp_client;
@@ -58,7 +67,9 @@ struct netconfig {
 	uint8_t rtm_protocol;
 	uint8_t rtm_v6_protocol;
 	struct l_rtnl_address *v4_address;
+	struct l_rtnl_address *stale_v4_address;
 	struct l_rtnl_address *v6_address;
+	struct l_rtnl_address *stale_v6_address;
 	char **dns4_overrides;
 	char **dns6_overrides;
 	char **dns4_list;
@@ -69,6 +80,8 @@ struct netconfig {
 	char *v6_gateway_str;
 	char *v4_domain;
 	char **v6_domains;
+	unsigned int pending_v4_set;
+	unsigned int pending_v6_set;
 
 	const struct l_settings *active_settings;
 
@@ -844,10 +857,8 @@ static void netconfig_route6_add_cb(int error, uint16_t type,
 	}
 }
 
-static bool netconfig_ipv4_routes_install(struct netconfig *netconfig)
+static bool netconfig_ipv4_subnet_route_install(struct netconfig *netconfig)
 {
-	L_AUTO_FREE_VAR(char *, gateway) = NULL;
-	const uint8_t *gateway_mac = NULL;
 	struct in_addr in_addr;
 	char ip[INET_ADDRSTRLEN];
 	char network[INET_ADDRSTRLEN];
@@ -870,10 +881,19 @@ static bool netconfig_ipv4_routes_install(struct netconfig *netconfig)
 						netconfig_route_generic_cb,
 						netconfig, NULL)) {
 		l_error("netconfig: Failed to add subnet route.");
-
 		return false;
 	}
 
+	return true;
+}
+
+static bool netconfig_ipv4_gateway_route_install(struct netconfig *netconfig)
+{
+	L_AUTO_FREE_VAR(char *, gateway) = NULL;
+	const uint8_t *gateway_mac = NULL;
+	struct in_addr in_addr;
+	char ip[INET_ADDRSTRLEN];
+
 	gateway = netconfig_ipv4_get_gateway(netconfig, &gateway_mac);
 	if (!gateway) {
 		l_debug("No gateway obtained from %s.",
@@ -889,6 +909,10 @@ static bool netconfig_ipv4_routes_install(struct netconfig *netconfig)
 		return true;
 	}
 
+	if (!l_rtnl_address_get_address(netconfig->v4_address, ip) ||
+			inet_pton(AF_INET, ip, &in_addr) < 1)
+		return false;
+
 	netconfig->route4_add_gateway_cmd_id =
 		l_rtnl_route4_add_gateway(rtnl, netconfig->ifindex, gateway, ip,
 						ROUTE_PRIORITY_OFFSET,
@@ -902,50 +926,77 @@ static bool netconfig_ipv4_routes_install(struct netconfig *netconfig)
 		return false;
 	}
 
-	if (gateway_mac) {
-		/*
-		 * Attempt to use the gateway MAC address received from the AP
-		 * by writing the mapping directly into the netdev's ARP table
-		 * so as to save one data frame roundtrip before first IP
-		 * connections are established.  This is very low-priority but
-		 * print error messages just because they may indicate bigger
-		 * problems.
-		 */
-		if (!l_rtnl_neighbor_set_hwaddr(rtnl, netconfig->ifindex,
+	/*
+	 * Attempt to use the gateway MAC address received from the AP by
+	 * writing the mapping directly into the netdev's ARP table so as
+	 * to save one data frame roundtrip before first IP connections
+	 * are established.  This is very low-priority but print error
+	 * messages just because they may indicate bigger problems.
+	 */
+	if (gateway_mac && !l_rtnl_neighbor_set_hwaddr(rtnl, netconfig->ifindex,
 					AF_INET,
 					&netconfig->fils_override->ipv4_gateway,
 					gateway_mac, 6,
 					netconfig_set_neighbor_entry_cb, NULL,
 					NULL))
-			l_debug("l_rtnl_neighbor_set_hwaddr failed");
+		l_debug("l_rtnl_neighbor_set_hwaddr failed");
+
+	return true;
+}
+
+static bool netconfig_ipv6_static_gateway_route_install(
+						struct netconfig *netconfig)
+{
+	_auto_(l_rtnl_route_free) struct l_rtnl_route *gateway = NULL;
+	const uint8_t *gateway_mac;
+
+	gateway = netconfig_get_static6_gateway(netconfig,
+						&netconfig->v6_gateway_str,
+						&gateway_mac);
+	if (!gateway) {
+		l_debug("No IPv6 gateway set");
+		return true;
 	}
 
+	netconfig->route6_add_cmd_id = l_rtnl_route_add(rtnl,
+							netconfig->ifindex,
+							gateway,
+							netconfig_route6_add_cb,
+							netconfig, NULL);
+	if (!L_WARN_ON(unlikely(!netconfig->route6_add_cmd_id)))
+		return false;
+
+	if (gateway_mac && !l_rtnl_neighbor_set_hwaddr(rtnl, netconfig->ifindex,
+					AF_INET6,
+					netconfig->fils_override->ipv6_gateway,
+					gateway_mac, 6,
+					netconfig_set_neighbor_entry_cb, NULL,
+					NULL))
+		l_debug("l_rtnl_neighbor_set_hwaddr failed");
+
 	return true;
 }
 
+static void netconfig_set(struct netconfig *netconfig, uint8_t af,
+				unsigned int changed);
+
 static void netconfig_ipv4_ifaddr_add_cmd_cb(int error, uint16_t type,
 						const void *data, uint32_t len,
 						void *user_data)
 {
 	struct netconfig *netconfig = user_data;
+	unsigned int changed = netconfig->pending_v4_set;
 
 	netconfig->addr4_add_cmd_id = 0;
 
-	if (error && error != -EEXIST) {
+	if (error && error != -EEXIST)
 		l_error("netconfig: Failed to add IP address. "
 				"Error %d: %s", error, strerror(-error));
-		return;
-	}
-
-	netconfig_gateway_to_arp(netconfig);
-
-	if (!netconfig_ipv4_routes_install(netconfig)) {
-		l_error("netconfig: Failed to install IPv4 routes.");
-		return;
-	}
+	else
+		netconfig_ipv4_subnet_route_install(netconfig);
 
-	netconfig_set_dns(netconfig);
-	netconfig_set_domains(netconfig);
+	netconfig->pending_v4_set = 0;
+	netconfig_set(netconfig, AF_INET, changed);
 }
 
 static void netconfig_ipv6_ifaddr_add_cmd_cb(int error, uint16_t type,
@@ -953,40 +1004,16 @@ static void netconfig_ipv6_ifaddr_add_cmd_cb(int error, uint16_t type,
 						void *user_data)
 {
 	struct netconfig *netconfig = user_data;
-	struct l_rtnl_route *gateway;
-	const uint8_t *gateway_mac;
+	unsigned int changed = netconfig->pending_v4_set;
 
 	netconfig->addr6_add_cmd_id = 0;
 
-	if (error && error != -EEXIST) {
+	if (error && error != -EEXIST)
 		l_error("netconfig: Failed to add IPv6 address. "
 				"Error %d: %s", error, strerror(-error));
-		return;
-	}
-
-	gateway = netconfig_get_static6_gateway(netconfig,
-						&netconfig->v6_gateway_str,
-						&gateway_mac);
-	if (gateway) {
-		netconfig->route6_add_cmd_id = l_rtnl_route_add(rtnl,
-							netconfig->ifindex,
-							gateway,
-							netconfig_route6_add_cb,
-							netconfig, NULL);
-		L_WARN_ON(unlikely(!netconfig->route6_add_cmd_id));
-		l_rtnl_route_free(gateway);
-
-		if (gateway_mac && !l_rtnl_neighbor_set_hwaddr(rtnl,
-					netconfig->ifindex, AF_INET6,
-					netconfig->fils_override->ipv6_gateway,
-					gateway_mac, 6,
-					netconfig_set_neighbor_entry_cb, NULL,
-					NULL))
-			l_debug("l_rtnl_neighbor_set_hwaddr failed");
-	}
 
-	netconfig_set_dns(netconfig);
-	netconfig_set_domains(netconfig);
+	netconfig->pending_v6_set = 0;
+	netconfig_set(netconfig, AF_INET6, changed);
 }
 
 static void netconfig_ifaddr_del_cmd_cb(int error, uint16_t type,
@@ -1009,21 +1036,169 @@ static void netconfig_ifaddr_del_cmd_cb(int error, uint16_t type,
 				"Error %d: %s", error, strerror(-error));
 }
 
+static bool netconfig_address_cmp_address(const struct l_rtnl_address *a,
+						const struct l_rtnl_address *b)
+{
+	char str_a[INET6_ADDRSTRLEN];
+	char str_b[INET6_ADDRSTRLEN];
+
+	if (a == b)
+		return true;
+
+	if (!a || !b)
+		return false;
+
+	if (!l_rtnl_address_get_address(a, str_a) ||
+			!l_rtnl_address_get_address(b, str_b))
+		return false;
+
+	return !strcmp(str_a, str_b);
+}
+
+static bool netconfig_address_cmp_prefix_len(const struct l_rtnl_address *a,
+						const struct l_rtnl_address *b)
+{
+	if (a == b)
+		return true;
+
+	if (!a || !b)
+		return false;
+
+	return l_rtnl_address_get_prefix_length(a) ==
+		l_rtnl_address_get_prefix_length(b);
+}
+
+static void netconfig_set(struct netconfig *netconfig, uint8_t af,
+				unsigned int changed)
+{
+	struct l_rtnl_address *stale_addr;
+
+	if (af == AF_INET)
+		stale_addr = l_steal_ptr(netconfig->stale_v4_address);
+	else
+		stale_addr = l_steal_ptr(netconfig->stale_v6_address);
+
+	if (stale_addr) {
+		L_WARN_ON(!l_rtnl_ifaddr_delete(rtnl, netconfig->ifindex,
+						stale_addr,
+						netconfig_ifaddr_del_cmd_cb,
+						netconfig, NULL));
+		l_rtnl_address_free(stale_addr);
+	}
+
+	if (af == AF_INET) {
+		if (!netconfig->v4_address)
+			return;
+
+		if ((changed & (NETCONFIG_CHANGED_ADDRESS |
+					NETCONFIG_CHANGED_NETMASK)) &&
+				!netconfig->addr4_add_cmd_id) {
+			netconfig->addr4_add_cmd_id = l_rtnl_ifaddr_add(rtnl,
+					netconfig->ifindex,
+					netconfig->v4_address,
+					netconfig_ipv4_ifaddr_add_cmd_cb,
+					netconfig, NULL);
+			if (!L_WARN_ON(!netconfig->addr4_add_cmd_id))
+				changed &= ~(NETCONFIG_CHANGED_ADDRESS |
+						NETCONFIG_CHANGED_NETMASK);
+		}
+
+		/*
+		 * If we either started the address addition now or one was
+		 * already in progress, wait for that operation's callback
+		 * and we'll be called again to set the rest of the values
+		 * in the changed bitmask.
+		 */
+		if (netconfig->addr4_add_cmd_id) {
+			netconfig->pending_v4_set |= changed;
+			return;
+		}
+
+		/*
+		 * For IPv6 the address, the subnet route and the gateway
+		 * route are handled automatically for DHCP, only cover the
+		 * static and FILS-provided cases here.
+		 */
+	} else if (netconfig->rtm_v6_protocol == RTPROT_STATIC ||
+			(netconfig->fils_override &&
+			 !l_memeqzero(netconfig->fils_override->ipv6_addr,
+					16))) {
+		if (!netconfig->v6_address)
+			return;
+
+		if ((changed & (NETCONFIG_CHANGED_ADDRESS |
+					NETCONFIG_CHANGED_NETMASK)) &&
+				!netconfig->addr6_add_cmd_id) {
+			netconfig->addr6_add_cmd_id = l_rtnl_ifaddr_add(rtnl,
+					netconfig->ifindex,
+					netconfig->v6_address,
+					netconfig_ipv6_ifaddr_add_cmd_cb,
+					netconfig, NULL);
+			if (!L_WARN_ON(!netconfig->addr6_add_cmd_id))
+				changed &= ~(NETCONFIG_CHANGED_ADDRESS |
+						NETCONFIG_CHANGED_NETMASK);
+		}
+
+		/*
+		 * If we either started the address addition now or one was
+		 * already in progress, wait for that operation's callback
+		 * and we'll be called again to set the rest of the values
+		 * in the changed bitmask.
+		 */
+		if (netconfig->addr6_add_cmd_id) {
+			netconfig->pending_v4_set |= changed;
+			return;
+		}
+	}
+
+	if (af == AF_INET) {
+		if (changed & NETCONFIG_CHANGED_GATEWAY) {
+			netconfig_gateway_to_arp(netconfig);
+
+			if (netconfig_ipv4_gateway_route_install(netconfig))
+				changed &= ~NETCONFIG_CHANGED_GATEWAY;
+		} else if (netconfig->notify) {
+			/*
+			 * The gateway route addition callback notifies the user
+			 * of netconfig success if a gateway is provided, otherwise
+			 * notify here.
+			 */
+			netconfig->notify(NETCONFIG_EVENT_CONNECTED,
+						netconfig->user_data);
+			netconfig->notify = NULL;
+		}
+	} else if (netconfig->rtm_v6_protocol == RTPROT_STATIC ||
+			(netconfig->fils_override &&
+			 !l_memeqzero(netconfig->fils_override->ipv6_gateway,
+					16))) {
+		if (changed & (NETCONFIG_CHANGED_GATEWAY))
+			if (netconfig_ipv6_static_gateway_route_install(
+								netconfig))
+				changed &= ~NETCONFIG_CHANGED_GATEWAY;
+	}
+
+	if (changed & NETCONFIG_CHANGED_DNS) {
+		netconfig_set_dns(netconfig);
+		changed &= ~NETCONFIG_CHANGED_DNS;
+	}
+
+	if (changed & NETCONFIG_CHANGED_DOMAINS) {
+		netconfig_set_domains(netconfig);
+		changed &= ~NETCONFIG_CHANGED_DOMAINS;
+	}
+}
+
 static void netconfig_ipv4_dhcp_event_handler(struct l_dhcp_client *client,
 						enum l_dhcp_client_event event,
 						void *userdata)
 {
 	struct netconfig *netconfig = userdata;
+	unsigned int changed = 0;
 
 	l_debug("DHCPv4 event %d", event);
 
 	switch (event) {
 	case L_DHCP_CLIENT_EVENT_IP_CHANGED:
-		L_WARN_ON(!l_rtnl_ifaddr_delete(rtnl, netconfig->ifindex,
-					netconfig->v4_address,
-					netconfig_ifaddr_del_cmd_cb,
-					netconfig, NULL));
-		/* Fall through. */
 	case L_DHCP_CLIENT_EVENT_LEASE_OBTAINED:
 	{
 		char *gateway_str;
@@ -1035,9 +1210,19 @@ static void netconfig_ipv4_dhcp_event_handler(struct l_dhcp_client *client,
 		else {
 			l_free(netconfig->v4_gateway_str);
 			netconfig->v4_gateway_str = gateway_str;
+			changed |= NETCONFIG_CHANGED_GATEWAY;
 		}
 
 		address = netconfig_get_dhcp4_address(netconfig);
+
+		if (!netconfig_address_cmp_prefix_len(netconfig->v4_address,
+							address))
+			changed |= NETCONFIG_CHANGED_ADDRESS;
+
+		if (netconfig_address_cmp_address(netconfig->v4_address,
+							address))
+			changed |= NETCONFIG_CHANGED_NETMASK;
+
 		l_rtnl_address_free(netconfig->v4_address);
 		netconfig->v4_address = address;
 
@@ -1047,26 +1232,36 @@ static void netconfig_ipv4_dhcp_event_handler(struct l_dhcp_client *client,
 			return;
 		}
 
-		netconfig_dns_list_update(netconfig, AF_INET);
-		netconfig_domains_update(netconfig, AF_INET);
+		if (netconfig_dns_list_update(netconfig, AF_INET))
+			changed |= NETCONFIG_CHANGED_DNS;
 
-		L_WARN_ON(!(netconfig->addr4_add_cmd_id =
-				l_rtnl_ifaddr_add(rtnl, netconfig->ifindex,
-					netconfig->v4_address,
-					netconfig_ipv4_ifaddr_add_cmd_cb,
-					netconfig, NULL)));
+		if (netconfig_domains_update(netconfig, AF_INET))
+			changed |= NETCONFIG_CHANGED_DOMAINS;
+
+		netconfig_set(netconfig, AF_INET, changed);
 		break;
 	}
 	case L_DHCP_CLIENT_EVENT_LEASE_RENEWED:
 		break;
 	case L_DHCP_CLIENT_EVENT_LEASE_EXPIRED:
-		L_WARN_ON(!l_rtnl_ifaddr_delete(rtnl, netconfig->ifindex,
-					netconfig->v4_address,
-					netconfig_ifaddr_del_cmd_cb,
-					netconfig, NULL));
-		l_rtnl_address_free(netconfig->v4_address);
-		netconfig->v4_address = NULL;
-		l_free(l_steal_ptr(netconfig->v4_gateway_str));
+		l_debug("Lease for interface %u expired", netconfig->ifindex);
+		changed = NETCONFIG_CHANGED_ADDRESS | NETCONFIG_CHANGED_NETMASK;
+		l_rtnl_address_free(netconfig->stale_v4_address);
+		netconfig->stale_v4_address =
+			l_steal_ptr(netconfig->v4_address);
+
+		if (netconfig->v4_gateway_str) {
+			changed |= NETCONFIG_CHANGED_GATEWAY;
+			l_free(l_steal_ptr(netconfig->v4_gateway_str));
+		}
+
+		if (netconfig_dns_list_update(netconfig, AF_INET))
+			changed |= NETCONFIG_CHANGED_DNS;
+
+		if (netconfig_domains_update(netconfig, AF_INET))
+			changed |= NETCONFIG_CHANGED_DOMAINS;
+
+		netconfig_set(netconfig, AF_INET, changed);
 
 		/* Fall through. */
 	case L_DHCP_CLIENT_EVENT_NO_LEASE:
@@ -1090,6 +1285,7 @@ static void netconfig_dhcp6_event_handler(struct l_dhcp6_client *client,
 						void *userdata)
 {
 	struct netconfig *netconfig = userdata;
+	unsigned int changed = 0;
 
 	switch (event) {
 	case L_DHCP6_CLIENT_EVENT_IP_CHANGED:
@@ -1112,28 +1308,52 @@ static void netconfig_dhcp6_event_handler(struct l_dhcp6_client *client,
 		else {
 			l_free(netconfig->v6_gateway_str);
 			netconfig->v6_gateway_str = gateway_str;
+			changed |= NETCONFIG_CHANGED_GATEWAY;
 		}
 
 		address = l_rtnl_address_new(addr_str,
 					l_dhcp6_lease_get_prefix_length(lease));
+
+		if (!netconfig_address_cmp_prefix_len(netconfig->v6_address,
+							address))
+			changed |= NETCONFIG_CHANGED_ADDRESS;
+
+		if (netconfig_address_cmp_address(netconfig->v6_address,
+							address))
+			changed |= NETCONFIG_CHANGED_NETMASK;
+
 		l_rtnl_address_free(netconfig->v6_address);
 		netconfig->v6_address = address;
 
-		netconfig_dns_list_update(netconfig, AF_INET6);
-		netconfig_domains_update(netconfig, AF_INET6);
-		netconfig_set_dns(netconfig);
-		netconfig_set_domains(netconfig);
+		if (netconfig_dns_list_update(netconfig, AF_INET6))
+			changed |= NETCONFIG_CHANGED_DNS;
+
+		if (netconfig_domains_update(netconfig, AF_INET6))
+			changed |= NETCONFIG_CHANGED_DOMAINS;
+
+		netconfig_set(netconfig, AF_INET6, changed);
 		break;
 	}
 	case L_DHCP6_CLIENT_EVENT_LEASE_EXPIRED:
-		l_debug("Lease for interface %u expired", netconfig->ifindex);
-		netconfig_dns_list_update(netconfig, AF_INET6);
-		netconfig_domains_update(netconfig, AF_INET6);
-		netconfig_set_dns(netconfig);
-		netconfig_set_domains(netconfig);
-		l_rtnl_address_free(netconfig->v6_address);
-		netconfig->v6_address = NULL;
-		l_free(l_steal_ptr(netconfig->v6_gateway_str));
+		l_debug("IPv6 lease for interface %u expired",
+			netconfig->ifindex);
+		changed = NETCONFIG_CHANGED_ADDRESS | NETCONFIG_CHANGED_NETMASK;
+		l_rtnl_address_free(netconfig->stale_v6_address);
+		netconfig->stale_v6_address =
+			l_steal_ptr(netconfig->v6_address);
+
+		if (netconfig->v6_gateway_str) {
+			changed |= NETCONFIG_CHANGED_GATEWAY;
+			l_free(l_steal_ptr(netconfig->v6_gateway_str));
+		}
+
+		if (netconfig_dns_list_update(netconfig, AF_INET6))
+			changed |= NETCONFIG_CHANGED_DNS;
+
+		if (netconfig_domains_update(netconfig, AF_INET6))
+			changed |= NETCONFIG_CHANGED_DOMAINS;
+
+		netconfig_set(netconfig, AF_INET6, changed);
 
 		/* Fall through */
 	case L_DHCP6_CLIENT_EVENT_NO_LEASE:
@@ -1144,50 +1364,93 @@ static void netconfig_dhcp6_event_handler(struct l_dhcp6_client *client,
 	}
 }
 
-static void netconfig_remove_v4_address(struct netconfig *netconfig)
-{
-	if (!netconfig->v4_address)
-		return;
-
-	L_WARN_ON(!l_rtnl_ifaddr_delete(rtnl, netconfig->ifindex,
-					netconfig->v4_address,
-					netconfig_ifaddr_del_cmd_cb,
-					netconfig, NULL));
-	l_rtnl_address_free(netconfig->v4_address);
-	netconfig->v4_address = NULL;
-}
-
 static void netconfig_reset_v4(struct netconfig *netconfig)
 {
 	if (netconfig->rtm_protocol) {
-		netconfig_remove_v4_address(netconfig);
+		unsigned int changed = 0;
+
+		if (netconfig->v4_address)
+			changed |= NETCONFIG_CHANGED_ADDRESS |
+				NETCONFIG_CHANGED_NETMASK;
+
+		l_rtnl_address_free(netconfig->stale_v4_address);
+		netconfig->stale_v4_address = l_steal_ptr(netconfig->v4_address);
+
+		if (netconfig->v4_gateway_str)
+			changed |= NETCONFIG_CHANGED_GATEWAY;
+
+		l_free(l_steal_ptr(netconfig->v4_gateway_str));
+
+		if (netconfig->dns4_list)
+			changed |= NETCONFIG_CHANGED_DNS;
 
 		l_strv_free(l_steal_ptr(netconfig->dns4_overrides));
 		l_strv_free(l_steal_ptr(netconfig->dns4_list));
 
+		if (netconfig->v4_domain)
+			changed |= NETCONFIG_CHANGED_DOMAINS;
+
+		l_free(l_steal_ptr(netconfig->v4_domain));
+
+		netconfig_set(netconfig, AF_INET, changed);
+
 		l_dhcp_client_stop(netconfig->dhcp_client);
 		netconfig->rtm_protocol = 0;
 
 		l_acd_destroy(netconfig->acd);
 		netconfig->acd = NULL;
+	}
+}
 
-		l_free(l_steal_ptr(netconfig->v4_gateway_str));
+static void netconfig_reset_v6(struct netconfig *netconfig)
+{
+	if (netconfig->rtm_v6_protocol) {
+		unsigned int changed = 0;
+		struct netdev *netdev = netdev_find(netconfig->ifindex);
 
-		l_free(l_steal_ptr(netconfig->v4_domain));
+		if (netconfig->v6_address)
+			changed |= NETCONFIG_CHANGED_ADDRESS |
+				NETCONFIG_CHANGED_NETMASK;
+
+		l_rtnl_address_free(netconfig->stale_v6_address);
+		netconfig->stale_v6_address = l_steal_ptr(netconfig->v6_address);
+
+		if (netconfig->v6_gateway_str)
+			changed |= NETCONFIG_CHANGED_GATEWAY;
+
+		l_free(l_steal_ptr(netconfig->v6_gateway_str));
+
+		if (netconfig->dns6_list)
+			changed |= NETCONFIG_CHANGED_DNS;
+
+		l_strv_free(l_steal_ptr(netconfig->dns6_overrides));
+		l_strv_free(l_steal_ptr(netconfig->dns6_list));
+
+		if (netconfig->v6_domains)
+			changed |= NETCONFIG_CHANGED_DOMAINS;
+
+		l_free(l_steal_ptr(netconfig->v6_domains));
+
+		netconfig_set(netconfig, AF_INET6, changed);
+
+		l_dhcp6_client_stop(netconfig->dhcp6_client);
+		netconfig->rtm_v6_protocol = 0;
+
+		sysfs_write_ipv6_setting(netdev_get_name(netdev),
+						"disable_ipv6", "1");
 	}
 }
 
 static void netconfig_ipv4_acd_event(enum l_acd_event event, void *user_data)
 {
 	struct netconfig *netconfig = user_data;
+	unsigned int changed;
 
 	switch (event) {
 	case L_ACD_EVENT_AVAILABLE:
-		L_WARN_ON(!(netconfig->addr4_add_cmd_id =
-				l_rtnl_ifaddr_add(rtnl, netconfig->ifindex,
-					netconfig->v4_address,
-					netconfig_ipv4_ifaddr_add_cmd_cb,
-					netconfig, NULL)));
+		changed = netconfig->pending_v4_set;
+		netconfig->pending_v4_set = 0;
+		netconfig_set(netconfig, AF_INET, changed);
 		return;
 	case L_ACD_EVENT_CONFLICT:
 		/*
@@ -1205,7 +1468,10 @@ static void netconfig_ipv4_acd_event(enum l_acd_event event, void *user_data)
 		 * case.
 		 */
 		l_error("netconfig: statically configured address was lost");
-		netconfig_remove_v4_address(netconfig);
+
+		l_rtnl_address_free(netconfig->stale_v4_address);
+		netconfig->stale_v4_address = l_steal_ptr(netconfig->v4_address);
+		netconfig_set(netconfig, AF_INET, NETCONFIG_CHANGED_ALL);
 		break;
 	}
 }
@@ -1243,6 +1509,17 @@ static bool netconfig_ipv4_select_and_install(struct netconfig *netconfig)
 
 	if (set_address) {
 		char ip[INET6_ADDRSTRLEN];
+		unsigned int changed = NETCONFIG_CHANGED_ADDRESS |
+			NETCONFIG_CHANGED_NETMASK;
+
+		if (netconfig->v4_gateway_str)
+			changed |= NETCONFIG_CHANGED_GATEWAY;
+
+		if (netconfig_dns_list_update(netconfig, AF_INET))
+			changed |= NETCONFIG_CHANGED_DNS;
+
+		if (netconfig_domains_update(netconfig, AF_INET))
+			changed |= NETCONFIG_CHANGED_DOMAINS;
 
 		if (L_WARN_ON(!netconfig->v4_address ||
 					!l_rtnl_address_get_address(
@@ -1250,9 +1527,6 @@ static bool netconfig_ipv4_select_and_install(struct netconfig *netconfig)
 							ip)))
 			return false;
 
-		netconfig_dns_list_update(netconfig, AF_INET);
-		netconfig_domains_update(netconfig, AF_INET);
-
 		netconfig->acd = l_acd_new(netconfig->ifindex);
 		l_acd_set_event_handler(netconfig->acd,
 					netconfig_ipv4_acd_event, netconfig,
@@ -1266,12 +1540,9 @@ static bool netconfig_ipv4_select_and_install(struct netconfig *netconfig)
 			l_acd_destroy(netconfig->acd);
 			netconfig->acd = NULL;
 
-			L_WARN_ON(!(netconfig->addr4_add_cmd_id =
-				l_rtnl_ifaddr_add(rtnl, netconfig->ifindex,
-					netconfig->v4_address,
-					netconfig_ipv4_ifaddr_add_cmd_cb,
-					netconfig, NULL)));
-		}
+			netconfig_set(netconfig, AF_INET, changed);
+		} else
+			netconfig->pending_v4_set |= changed;
 
 		return true;
 	}
@@ -1324,13 +1595,16 @@ static bool netconfig_ipv6_select_and_install(struct netconfig *netconfig)
 	}
 
 	if (netconfig->v6_address) {
-		netconfig_dns_list_update(netconfig, AF_INET6);
+		unsigned int changed = NETCONFIG_CHANGED_ADDRESS |
+			NETCONFIG_CHANGED_NETMASK;
 
-		L_WARN_ON(!(netconfig->addr6_add_cmd_id =
-			l_rtnl_ifaddr_add(rtnl, netconfig->ifindex,
-					netconfig->v6_address,
-					netconfig_ipv6_ifaddr_add_cmd_cb,
-					netconfig, NULL)));
+		if (netconfig->v6_gateway_str)
+			changed |= NETCONFIG_CHANGED_GATEWAY;
+
+		if (netconfig_dns_list_update(netconfig, AF_INET6))
+			changed |= NETCONFIG_CHANGED_DNS;
+
+		netconfig_set(netconfig, AF_INET6, changed);
 		return true;
 	}
 
@@ -1523,8 +1797,6 @@ bool netconfig_reconfigure(struct netconfig *netconfig)
 
 bool netconfig_reset(struct netconfig *netconfig)
 {
-	struct netdev *netdev = netdev_find(netconfig->ifindex);
-
 	if (netconfig->route4_add_gateway_cmd_id) {
 		l_netlink_cancel(rtnl, netconfig->route4_add_gateway_cmd_id);
 		netconfig->route4_add_gateway_cmd_id = 0;
@@ -1549,24 +1821,7 @@ bool netconfig_reset(struct netconfig *netconfig)
 		resolve_revert(netconfig->resolve);
 
 	netconfig_reset_v4(netconfig);
-
-	if (netconfig->rtm_v6_protocol) {
-		l_rtnl_address_free(netconfig->v6_address);
-		netconfig->v6_address = NULL;
-
-		l_strv_free(l_steal_ptr(netconfig->dns6_overrides));
-		l_strv_free(l_steal_ptr(netconfig->dns6_list));
-
-		l_dhcp6_client_stop(netconfig->dhcp6_client);
-		netconfig->rtm_v6_protocol = 0;
-
-		sysfs_write_ipv6_setting(netdev_get_name(netdev),
-						"disable_ipv6", "1");
-
-		l_free(l_steal_ptr(netconfig->v6_gateway_str));
-
-		l_strv_free(l_steal_ptr(netconfig->v6_domains));
-	}
+	netconfig_reset_v6(netconfig);
 
 	l_free(l_steal_ptr(netconfig->fils_override));
 
Signature · 1 line
2.32.0
author
#10 lore raw

PATCH 8/12 netconfig: Move FILS override checks to common functions

In reply to Andrew Zaborowski
---
 src/netconfig.c | 56 ++++++++++++++++++++++++++++++++-----------------
 1 file changed, 37 insertions(+), 19 deletions(-)
src/netconfig.c +37 -19
index a73afe24..88e10e3f 100644
@@ -220,6 +220,36 @@ static inline char *netconfig_ipv6_to_string(const uint8_t *addr)
 	return addr_str;
 }
 
+static bool netconfig_use_fils_addr(struct netconfig *netconfig, int af)
+{
+	if ((af == AF_INET ? netconfig->rtm_protocol :
+				netconfig->rtm_v6_protocol) != RTPROT_DHCP)
+		return false;
+
+	if (!netconfig->fils_override)
+		return false;
+
+	if (af == AF_INET)
+		return !!netconfig->fils_override->ipv4_addr;
+
+	return !l_memeqzero(netconfig->fils_override->ipv6_addr, 16);
+}
+
+static bool netconfig_use_fils_gateway(struct netconfig *netconfig, int af)
+{
+	if ((af == AF_INET ? netconfig->rtm_protocol :
+				netconfig->rtm_v6_protocol) != RTPROT_DHCP)
+		return false;
+
+	if (!netconfig->fils_override)
+		return false;
+
+	if (af == AF_INET)
+		return !!netconfig->fils_override->ipv4_gateway;
+
+	return !l_memeqzero(netconfig->fils_override->ipv6_gateway, 16);
+}
+
 static char **netconfig_get_dns_list(struct netconfig *netconfig, int af,
 					const uint8_t **out_dns_mac)
 {
@@ -503,7 +533,7 @@ static char *netconfig_ipv4_get_gateway(struct netconfig *netconfig,
 		return gateway;
 
 	case RTPROT_DHCP:
-		if (fils && fils->ipv4_gateway) {
+		if (netconfig_use_fils_gateway(netconfig, AF_INET)) {
 			gateway = netconfig_ipv4_to_string(fils->ipv4_gateway);
 
 			if (gateway && out_mac &&
@@ -571,10 +601,7 @@ static struct l_rtnl_route *netconfig_get_static6_gateway(
 
 	gateway = l_settings_get_string(netconfig->active_settings,
 						"IPv6", "Gateway");
-	if (!gateway && netconfig->rtm_v6_protocol == RTPROT_DHCP &&
-			netconfig->fils_override &&
-			!l_memeqzero(netconfig->fils_override->ipv6_gateway,
-					16)) {
+	if (!gateway && netconfig_use_fils_gateway(netconfig, AF_INET6)) {
 		gateway = netconfig_ipv6_to_string(
 					netconfig->fils_override->ipv6_gateway);
 
@@ -742,8 +769,7 @@ static void netconfig_ifaddr_ipv6_added(struct netconfig *netconfig,
 			ip, ifa->ifa_prefixlen);
 
 	if (netconfig->rtm_v6_protocol != RTPROT_DHCP ||
-			(netconfig->fils_override &&
-			 !l_memeqzero(netconfig->fils_override->ipv6_addr, 16)))
+			netconfig_use_fils_addr(netconfig, AF_INET6))
 		return;
 
 	inet_pton(AF_INET6, ip, &in6);
@@ -1120,9 +1146,7 @@ static void netconfig_set(struct netconfig *netconfig, uint8_t af,
 		 * static and FILS-provided cases here.
 		 */
 	} else if (netconfig->rtm_v6_protocol == RTPROT_STATIC ||
-			(netconfig->fils_override &&
-			 !l_memeqzero(netconfig->fils_override->ipv6_addr,
-					16))) {
+			netconfig_use_fils_addr(netconfig, AF_INET6)) {
 		if (!netconfig->v6_address)
 			return;
 
@@ -1168,9 +1192,7 @@ static void netconfig_set(struct netconfig *netconfig, uint8_t af,
 			netconfig->notify = NULL;
 		}
 	} else if (netconfig->rtm_v6_protocol == RTPROT_STATIC ||
-			(netconfig->fils_override &&
-			 !l_memeqzero(netconfig->fils_override->ipv6_gateway,
-					16))) {
+			netconfig_use_fils_gateway(netconfig, AF_INET6)) {
 		if (changed & (NETCONFIG_CHANGED_GATEWAY))
 			if (netconfig_ipv6_static_gateway_route_install(
 								netconfig))
@@ -1481,9 +1503,7 @@ static bool netconfig_ipv4_select_and_install(struct netconfig *netconfig)
 	struct netdev *netdev = netdev_find(netconfig->ifindex);
 	bool set_address = (netconfig->rtm_protocol == RTPROT_STATIC);
 
-	if (netconfig->rtm_protocol == RTPROT_DHCP &&
-			netconfig->fils_override &&
-			netconfig->fils_override->ipv4_addr) {
+	if (netconfig_use_fils_addr(netconfig, AF_INET)) {
 		L_AUTO_FREE_VAR(char *, addr_str) = netconfig_ipv4_to_string(
 					netconfig->fils_override->ipv4_addr);
 		uint8_t prefix_len = netconfig->fils_override->ipv4_prefix_len;
@@ -1569,9 +1589,7 @@ static bool netconfig_ipv6_select_and_install(struct netconfig *netconfig)
 
 	sysfs_write_ipv6_setting(netdev_get_name(netdev), "disable_ipv6", "0");
 
-	if (netconfig->rtm_v6_protocol == RTPROT_DHCP &&
-			netconfig->fils_override &&
-			!l_memeqzero(netconfig->fils_override->ipv6_addr, 16)) {
+	if (netconfig_use_fils_addr(netconfig, AF_INET6)) {
 		uint8_t prefix_len = netconfig->fils_override->ipv6_prefix_len;
 		L_AUTO_FREE_VAR(char *, addr_str) = netconfig_ipv6_to_string(
 					netconfig->fils_override->ipv6_addr);
Signature · 1 line
2.32.0
author
#11 lore raw

PATCH 9/12 doc: Update agent-api.txt to match code and clarify

In reply to Andrew Zaborowski
---
 doc/agent-api.txt | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)
doc/agent-api.txt +12 -8
index f3d8514e..9da913e0 100644
@@ -7,19 +7,23 @@ Object path	/net/connman/iwd
 
 Methods		void RegisterAgent(object path)
 
-			Register new agent for handling user requests.
+			Register new agent for handling user secrets requests.
+			The agent object's methods on the net.connman.iwd.Agent
+			interface will be called when IWD needs to request
+			credentials for a connection to a network in station
+			mode.
 
-			Possible Errors: [service].Error.InvalidArguments
-					 [service].Error.Failed
-					 [service].Error.AlreadyExists
+			Possible Errors: [service].InvalidArguments
+					 [service].Failed
+					 [service].AlreadyExists
 
 		void UnregisterAgent(object path)
 
 			Unregister an existing agent.
 
-			Possible Errors: [service].Error.InvalidArguments
-					 [service].Error.Failed
-					 [service].Error.NotFound
+			Possible Errors: [service].InvalidArguments
+					 [service].Failed
+					 [service].NotFound
 
 
 Agent hierarchy
@@ -80,5 +84,5 @@ Methods		void Release() [noreply]
 
 Examples	Requesting a passphrase for WPA2 network
 
-			RequestPassphrase("/network1")
+			RequestPassphrase("/net/connman/iwd/0/3/54657374_psk")
 			==> "secret123"
Signature · 1 line
2.32.0
Applied
#12 lore raw

Re: [PATCH 09/12] doc: Update agent-api.txt to match code and clarify

Hi Andrew,

Quoted 4 lines · Andrew Zaborowski
---
  doc/agent-api.txt | 20 ++++++++++++--------
  1 file changed, 12 insertions(+), 8 deletions(-)

Applied, thanks.

Regards,
-Denis

author
#13 lore raw

PATCH 10/12 doc: Add NetworkConfigurationAgent API description

In reply to Andrew Zaborowski

Document the new API that clients can use to get notified of new network configuration and be responsible for committing it to the netdev, the resolver, etc.

---
 doc/agent-api.txt | 106 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 106 insertions(+)
doc/agent-api.txt +106 -0
index 9da913e0..92317e57 100644
@@ -25,6 +25,48 @@ Methods		void RegisterAgent(object path)
 					 [service].Failed
 					 [service].NotFound
 
+		void RegisterNetworkConfigurationAgent(object path)
+
+			Register the agent for handling IPv4 and IPv6
+			configuration setting.  This method and its
+			corresponding unregister method are only supported
+			if network configuration is enabled: see
+			EnableNetworkConfiguration in iwd.config(5) and
+			NetworkConfigurationEnabled in
+			net.connman.iwd.Daemon.GetInfo in daemon-api.txt.
+
+			During a station-mode, AP-mode or P2P connection, IWD
+			will obtain the IP configuration values for a network
+			interface, either automatically from a remote server
+			(DHCP or otherwise), generate it or read it from a
+			settings file.  If no network configuration agent is
+			present, IWD immediately writes (commits) those
+			values to the corresponding interface, the routing
+			table and communicates them to the resolver.  If such
+			agent is registered, IWD requests the agent to do
+			this last step.
+
+			Connection success is not signalled until the agent
+			method has returned.  The agent may also be invoked
+			during the connection lifetime whenever the IP
+			configuration changes and after a roam.  The agent
+			object must have the NetworkConfigurationAgent
+			interface.
+
+			Possible Errors: [service].InvalidArguments
+					 [service].Failed
+					 [service].AlreadyExists
+					 [service].NotSupported
+
+		void UnregisterNetworkConfigurationAgent(object path)
+
+			Unregister an existing network configuration agent.
+
+			Possible Errors: [service].InvalidArguments
+					 [service].Failed
+					 [service].NotFound
+					 [service].NotAvailable
+
 
 Agent hierarchy
 ===============
@@ -86,3 +128,67 @@ Examples	Requesting a passphrase for WPA2 network
 
 			RequestPassphrase("/net/connman/iwd/0/3/54657374_psk")
 			==> "secret123"
+
+
+NetworkConfigurationAgent hierarchy
+===================================
+
+Service		unique name
+Interface	net.connman.iwd.NetworkConfigurationAgent
+Object path	freely definable
+
+Methods		void Release() [noreply]
+
+			This method gets called when the service daemon
+			unregisters the agent. An agent can use it to do
+			cleanup tasks. There is no need to unregister the
+			agent, because when this method gets called it has
+			already been unregistered.
+
+		void ConfigureIPv4(object device, string interface, dict config)
+
+			This method gets called during a connection setup
+			when a new set of IPv4 configuration values is to
+			be written to a network interface.  The connection
+			is aborted if the method call returns an error or
+			times out.  The 'config' dictionary (a{sv}) maps
+			string keys to values of types defined per key.
+
+			The following key/value pairs are used, but more
+			may be added in future versions.
+
+			string Method - Indicates whether the local address
+			was set statically (value "static") or obtained
+			automatically such as through DHCP (value "auto").
+			Even when the address was obtained from the remote
+			end some configuration bits, such as DNS addresses,
+			may have been overridden locally.
+
+			string Address - Local IP address string.
+
+			byte PrefixLength - Holds the prefix-length of the
+			local subnet.  For IPv4 this maps to the netmask.
+
+			string Gateway [optional] - Local subnet's gateway
+			address if one exists.
+
+			array(string) DomainNameServers [optional] - Holds
+			the list of DNS addresses configured if any exist.
+
+			array(string) DomainNames [optional] - Holds the
+			network's local domain names if any exist.
+
+			Possible Errors: net.connman.iwd.Agent.Error.Canceled
+
+		void ConfigureIPv6(object device, string interface, dict config)
+
+			Same as ConfigureIPv4 above but for IPv6.
+
+		void Cancel(object device, string interface,
+						string reason) [noreply]
+
+			This method gets called to indicate that the connection
+			request failed before a reply was returned.  The
+			argument will indicate why the request is being
+			cancelled and may be "out-of-range", "user-canceled",
+			"timed-out" or "shutdown".
Signature · 1 line
2.32.0
author
#14 lore raw

PATCH 11/12 agent: Implement the NetworkConfigurationAgent dbus API

In reply to Andrew Zaborowski
---
 src/agent.c     | 337 ++++++++++++++++++++++++++++++++++++++++++++----
 src/agent.h     |  11 ++
 src/dbus.h      |   2 +
 src/netconfig.h |  15 +++
 4 files changed, 343 insertions(+), 22 deletions(-)
src/agent.c +315 -22
index 6fe4507d..eacecfd5 100644
@@ -25,15 +25,22 @@
 #endif
 
 #include <errno.h>
+#include <arpa/inet.h>
 
 #include <ell/ell.h>
 #include "src/dbus.h"
 #include "src/agent.h"
 #include "src/iwd.h"
 #include "src/module.h"
+#include "src/netconfig.h"
 
 static unsigned int next_request_id = 0;
 
+enum agent_type {
+	AGENT_TYPE_SECRET,
+	AGENT_TYPE_NETCONFIG,
+};
+
 enum agent_request_type {
 	AGENT_REQUEST_TYPE_PASSPHRASE,
 	AGENT_REQUEST_TYPE_USER_NAME_PASSWD,
@@ -50,6 +57,18 @@ struct agent_request {
 	agent_request_destroy_func_t destroy;
 };
 
+/* Netconfig requests are not queued and can run in parallel */
+struct netconfig_request {
+	unsigned int dbus_id;
+	unsigned int id;
+	char *path;
+	char *ifname;
+	struct l_timeout *timeout;
+	void *user_data;
+	void *user_callback;
+	agent_request_destroy_func_t destroy;
+};
+
 struct agent {
 	char *owner;
 	char *path;
@@ -58,9 +77,11 @@ struct agent {
 	struct l_timeout *timeout;
 	int timeout_secs;
 	struct l_queue *requests;
+	enum agent_type type;
 };
 
-static struct l_queue *agents;
+static struct l_queue *secret_agents;
+static struct agent *netconfig_agent;
 
 /*
  * How long we wait for user to input things.
@@ -74,7 +95,8 @@ static unsigned int agent_timeout_input_request(void)
 	return 120;
 }
 
-static void send_cancel_request(void *user_data, int reason)
+static void send_cancel_request(void *user_data, int reason,
+				const char *path, const char *ifname)
 {
 	struct agent *agent = user_data;
 	struct l_dbus_message *message;
@@ -106,7 +128,12 @@ static void send_cancel_request(void *user_data, int reason)
 							IWD_AGENT_INTERFACE,
 							"Cancel");
 
-	l_dbus_message_set_arguments(message, "s", reasonstr);
+	if (path)
+		l_dbus_message_set_arguments(message, "oss", path, ifname,
+						reasonstr);
+	else
+		l_dbus_message_set_arguments(message, "s", reasonstr);
+
 	l_dbus_message_set_no_reply(message, true);
 
 	l_dbus_send(dbus_get_bus(), message);
@@ -128,6 +155,20 @@ static void agent_request_free(void *user_data)
 	l_free(request);
 }
 
+static void netconfig_request_free(void *user_data)
+{
+	struct netconfig_request *request = user_data;
+
+	if (request->destroy)
+		request->destroy(request->user_data);
+
+	l_timeout_remove(request->timeout);
+	l_dbus_cancel(dbus_get_bus(), request->dbus_id);
+	l_free(request->path);
+	l_free(request->ifname);
+	l_free(request);
+}
+
 static void passphrase_reply(struct l_dbus_message *reply,
 					struct agent_request *request)
 {
@@ -212,7 +253,10 @@ static void agent_free(void *data)
 	if (agent->pending_id)
 		l_dbus_cancel(dbus_get_bus(), agent->pending_id);
 
-	l_queue_destroy(agent->requests, agent_request_free);
+	if (agent->type == AGENT_TYPE_SECRET)
+		l_queue_destroy(agent->requests, agent_request_free);
+	else
+		l_queue_destroy(agent->requests, netconfig_request_free);
 
 	if (agent->disconnect_watch)
 		l_dbus_remove_watch(dbus_get_bus(), agent->disconnect_watch);
@@ -230,7 +274,7 @@ static void request_timeout(struct l_timeout *timeout, void *user_data)
 
 	l_dbus_cancel(dbus_get_bus(), agent->pending_id);
 
-	send_cancel_request(agent, -ETIMEDOUT);
+	send_cancel_request(agent, -ETIMEDOUT, NULL, NULL);
 
 	agent_finalize_pending(agent, NULL);
 
@@ -313,7 +357,8 @@ static struct agent *agent_lookup(const char *owner)
 	if (!owner)
 		return NULL;
 
-	for (entry = l_queue_get_entries(agents); entry; entry = entry->next) {
+	for (entry = l_queue_get_entries(secret_agents); entry;
+			entry = entry->next) {
 		struct agent *agent = entry->data;
 
 		if (strcmp(agent->owner, owner))
@@ -332,7 +377,7 @@ static struct agent *get_agent(const char *owner)
 	if (agent)
 		return agent;
 
-	return l_queue_peek_head(agents);
+	return l_queue_peek_head(secret_agents);
 }
 
 /**
@@ -347,7 +392,7 @@ static struct agent *get_agent(const char *owner)
  * id that can be used to cancel the request.
  *
  * If @trigger is not NULL, then a reference is taken automatically.  If
- * agent_cancel_request is called subsequently, a dbus_aborted error is
+ * agent_request_cancel is called subsequently, a dbus_aborted error is
  * automatically generated for @trigger.  Otherwise, after @callback is
  * called, the reference to @trigger is dropped.  It is assumed that the
  * caller will take ownership of @trigger in the callback if needed.
@@ -456,6 +501,156 @@ unsigned int agent_request_user_password(const char *path, const char *user,
 					callback, trigger, user_data, destroy);
 }
 
+static void netconfig_data_to_dict(const struct netconfig_data *data,
+					struct l_dbus_message_builder *builder)
+{
+	const char *ip_method_str = data->method == NETCONFIG_METHOD_STATIC ?
+		"static" : "auto";
+
+	l_dbus_message_builder_enter_array(builder, "{sv}");
+	dbus_append_dict_basic(builder, "Method", 's', ip_method_str);
+	dbus_append_dict_basic(builder, "Address", 's', data->addr);
+	dbus_append_dict_basic(builder, "PrefixLength", 'y', &data->prefix_len);
+
+	if (data->gateway_addr)
+		dbus_append_dict_basic(builder, "Gateway", 's',
+					data->gateway_addr);
+
+	if (data->dns) {
+		char **str;
+
+		l_dbus_message_builder_enter_dict(builder, "sv");
+		l_dbus_message_builder_append_basic(builder, 's',
+							"DomainNameServers");
+		l_dbus_message_builder_enter_variant(builder, "as");
+		l_dbus_message_builder_enter_array(builder, "s");
+
+		for (str = data->dns; *str; str++)
+			l_dbus_message_builder_append_basic(builder, 's', str);
+
+		l_dbus_message_builder_leave_array(builder);
+		l_dbus_message_builder_leave_variant(builder);
+		l_dbus_message_builder_leave_dict(builder);
+	}
+
+	if (data->domain_names) {
+		char **str;
+
+		l_dbus_message_builder_enter_dict(builder, "sv");
+		l_dbus_message_builder_append_basic(builder, 's',
+							"DomainNames");
+		l_dbus_message_builder_enter_variant(builder, "as");
+		l_dbus_message_builder_enter_array(builder, "s");
+
+		for (str = data->domain_names; *str; str++)
+			l_dbus_message_builder_append_basic(builder, 's', str);
+
+		l_dbus_message_builder_leave_array(builder);
+		l_dbus_message_builder_leave_variant(builder);
+		l_dbus_message_builder_leave_dict(builder);
+	}
+
+	l_dbus_message_builder_leave_array(builder);
+}
+
+static void netconfig_timeout_cb(struct l_timeout *timeout, void *user_data)
+{
+	struct netconfig_request *request = user_data;
+	agent_request_netconfig_func_t user_callback = request->user_callback;
+
+	l_debug("path %s ifname %s", request->path, request->ifname);
+
+	l_queue_remove(netconfig_agent->requests, request);
+
+	send_cancel_request(netconfig_agent, -ETIMEDOUT,
+				request->path, request->ifname);
+
+	user_callback(request->id, AGENT_RESULT_FAILED, request->user_data);
+	netconfig_request_free(request);
+}
+
+static void netconfig_reply_cb(struct l_dbus_message *message,
+				void *user_data)
+{
+	struct netconfig_request *request = user_data;
+	agent_request_netconfig_func_t user_callback = request->user_callback;
+	const char *error, *text;
+	enum agent_result result = AGENT_RESULT_OK;
+
+	l_debug("path %s ifname %s", request->path, request->ifname);
+
+	request->dbus_id = 0;
+	l_queue_remove(netconfig_agent->requests, request);
+
+	if (l_dbus_message_get_error(message, &error, &text)) {
+		if (!strcmp(error, "net.connman.iwd.Agent.Error.Canceled"))
+			result = AGENT_RESULT_CANCELLED;
+		else
+			result = AGENT_RESULT_FAILED;
+	} else if (!l_dbus_message_get_arguments(message, ""))
+		result = AGENT_RESULT_FAILED;
+
+	user_callback(request->id, result, request->user_data);
+	netconfig_request_free(request);
+}
+
+unsigned int agent_request_netconfig(const char *path, const char *ifname,
+				const struct netconfig_data *data,
+				agent_request_netconfig_func_t callback,
+				void *user_data,
+				agent_request_destroy_func_t destroy)
+{
+	struct l_dbus_message *message;
+	struct l_dbus_message_builder *builder;
+	struct netconfig_request *request;
+	unsigned int dbus_id;
+	const char *method_name;
+
+	l_debug("path %s ifname %s", path, ifname);
+
+	if (!netconfig_agent || !callback)
+		return 0;
+
+	method_name = data->family == AF_INET ? "ConfigureIPv4" : "ConfigureIPv6";
+	message = l_dbus_message_new_method_call(dbus_get_bus(),
+						netconfig_agent->owner,
+						netconfig_agent->path,
+						IWD_NETCONFIG_AGENT_INTERFACE,
+						method_name);
+
+	builder = l_dbus_message_builder_new(message);
+	l_dbus_message_builder_append_basic(builder, 'o', path);
+	l_dbus_message_builder_append_basic(builder, 's', ifname);
+	netconfig_data_to_dict(data, builder);
+	l_dbus_message_builder_finalize(builder);
+	l_dbus_message_builder_destroy(builder);
+
+	request = l_new(struct netconfig_request, 1);
+
+	dbus_id = l_dbus_send_with_reply(dbus_get_bus(), message,
+						netconfig_reply_cb,
+						request, NULL);
+	if (!dbus_id) {
+		l_error("l_dbus_send_with_reply failed");
+		l_dbus_message_unref(message);
+		l_free(request);
+		return 0;
+	}
+
+	request->dbus_id = dbus_id;
+	request->id = ++next_request_id;
+	request->path = l_strdup(path);
+	request->ifname = l_strdup(ifname);
+	request->timeout = l_timeout_create(5, netconfig_timeout_cb,
+						request, NULL);
+	request->user_data = user_data;
+	request->user_callback = callback;
+	request->destroy = destroy;
+
+	l_queue_push_tail(netconfig_agent->requests, request);
+	return request->id;
+}
+
 static bool find_request(const void *a, const void *b)
 {
 	const struct agent_request *request = a;
@@ -464,13 +659,22 @@ static bool find_request(const void *a, const void *b)
 	return request->id == id;
 }
 
+static bool find_netconfig_request(const void *a, const void *b)
+{
+	const struct netconfig_request *request = a;
+	unsigned int id = L_PTR_TO_UINT(b);
+
+	return request->id == id;
+}
+
 bool agent_request_cancel(unsigned int req_id, int reason)
 {
 	struct agent_request *request = NULL;
 	struct agent *agent;
 	const struct l_queue_entry *entry;
 
-	for (entry = l_queue_get_entries(agents); entry; entry = entry->next) {
+	for (entry = l_queue_get_entries(secret_agents); entry;
+			entry = entry->next) {
 		agent = entry->data;
 
 		request = l_queue_remove_if(agent->requests, find_request,
@@ -479,11 +683,27 @@ bool agent_request_cancel(unsigned int req_id, int reason)
 			break;
 	}
 
-	if (!request)
-		return false;
+	if (!request) {
+		struct netconfig_request *nc_request;
+
+		if (!netconfig_agent)
+			return false;
+
+		nc_request = l_queue_remove_if(netconfig_agent->requests,
+						find_netconfig_request,
+						L_UINT_TO_PTR(req_id));
+		if (!nc_request)
+			return false;
+
+		send_cancel_request(netconfig_agent, reason,
+					nc_request->path, nc_request->ifname);
+
+		netconfig_request_free(nc_request);
+		return true;
+	}
 
 	if (!request->message) {
-		send_cancel_request(agent, reason);
+		send_cancel_request(agent, reason, NULL, NULL);
 
 		l_dbus_cancel(dbus_get_bus(), agent->pending_id);
 
@@ -511,18 +731,19 @@ static void agent_disconnect(struct l_dbus *dbus, void *user_data)
 	if (agent->pending_id)
 		agent_finalize_pending(agent, NULL);
 
-	l_queue_remove(agents, agent);
+	l_queue_remove(secret_agents, agent);
 
 	l_idle_oneshot(agent_free, agent, NULL);
 }
 
-static struct agent *agent_create(struct l_dbus *dbus, const char *name,
-							const char *path)
+static struct agent *agent_create(struct l_dbus *dbus, enum agent_type type,
+					const char *name, const char *path)
 {
 	struct agent *agent;
 
 	agent = l_new(struct agent, 1);
 
+	agent->type = type;
 	agent->owner = l_strdup(name);
 	agent->path = l_strdup(path);
 	agent->requests = l_queue_new();
@@ -548,11 +769,12 @@ static struct l_dbus_message *agent_register(struct l_dbus *dbus,
 	if (!l_dbus_message_get_arguments(message, "o", &path))
 		return dbus_error_invalid_args(message);
 
-	agent = agent_create(dbus, l_dbus_message_get_sender(message), path);
+	agent = agent_create(dbus, AGENT_TYPE_SECRET,
+				l_dbus_message_get_sender(message), path);
 	if (!agent)
 		return dbus_error_failed(message);
 
-	l_queue_push_tail(agents, agent);
+	l_queue_push_tail(secret_agents, agent);
 
 	l_debug("agent %s path %s", agent->owner, agent->path);
 
@@ -575,7 +797,7 @@ static struct l_dbus_message *agent_unregister(struct l_dbus *dbus,
 	if (!agent)
 		return dbus_error_not_found(message);
 
-	l_queue_remove(agents, agent);
+	l_queue_remove(secret_agents, agent);
 
 	agent_free(agent);
 
@@ -586,6 +808,64 @@ static struct l_dbus_message *agent_unregister(struct l_dbus *dbus,
 	return reply;
 }
 
+static struct l_dbus_message *netconfig_agent_register(struct l_dbus *dbus,
+						struct l_dbus_message *message,
+						void *user_data)
+{
+	struct l_dbus_message *reply;
+	const char *path;
+
+	l_debug("");
+
+	if (!l_dbus_message_get_arguments(message, "o", &path))
+		return dbus_error_invalid_args(message);
+
+	if (!netconfig_get_enabled())
+		return dbus_error_not_supported(message);
+
+	if (netconfig_agent)
+		return dbus_error_already_exists(message);
+
+	netconfig_agent = agent_create(dbus, AGENT_TYPE_NETCONFIG,
+					l_dbus_message_get_sender(message),
+					path);
+	if (!netconfig_agent)
+		return dbus_error_failed(message);
+
+	l_debug("agent %s path %s",
+		netconfig_agent->owner, netconfig_agent->path);
+
+	reply = l_dbus_message_new_method_return(message);
+	l_dbus_message_set_arguments(reply, "");
+	return reply;
+}
+
+static struct l_dbus_message *netconfig_agent_unregister(struct l_dbus *dbus,
+						struct l_dbus_message *message,
+						void *user_data)
+{
+	struct l_dbus_message *reply;
+	const char *path;
+
+	l_debug("");
+
+	if (!l_dbus_message_get_arguments(message, "o", &path))
+		return dbus_error_invalid_args(message);
+
+	if (!netconfig_agent || strcmp(netconfig_agent->path, path))
+		return dbus_error_not_found(message);
+
+	if (strcmp(netconfig_agent->owner, l_dbus_message_get_sender(message)))
+		return dbus_error_not_available(message);
+
+	agent_free(netconfig_agent);
+	netconfig_agent = NULL;
+
+	reply = l_dbus_message_new_method_return(message);
+	l_dbus_message_set_arguments(reply, "");
+	return reply;
+}
+
 static void setup_agent_interface(struct l_dbus_interface *interface)
 {
 	l_dbus_interface_method(interface, "RegisterAgent", 0,
@@ -594,6 +874,13 @@ static void setup_agent_interface(struct l_dbus_interface *interface)
 	l_dbus_interface_method(interface, "UnregisterAgent", 0,
 				agent_unregister,
 				"", "o", "path");
+
+	l_dbus_interface_method(interface,
+				"RegisterNetworkConfigurationAgent", 0,
+				netconfig_agent_register, "", "o", "path");
+	l_dbus_interface_method(interface,
+				"UnregisterNetworkConfigurationAgent", 0,
+				netconfig_agent_unregister, "", "o", "path");
 }
 
 static bool release_agent(void *data, void *user_data)
@@ -622,7 +909,7 @@ static int agent_init(void)
 {
 	struct l_dbus *dbus = dbus_get_bus();
 
-	agents = l_queue_new();
+	secret_agents = l_queue_new();
 
 	if (!l_dbus_register_interface(dbus, IWD_AGENT_MANAGER_INTERFACE,
 						setup_agent_interface,
@@ -650,13 +937,19 @@ static void agent_exit(void)
 
 	l_dbus_unregister_interface(dbus, IWD_AGENT_MANAGER_INTERFACE);
 
-	l_queue_destroy(agents, agent_free);
-	agents = NULL;
+	l_queue_destroy(secret_agents, agent_free);
+	secret_agents = NULL;
+
+	if (netconfig_agent)
+		agent_free(l_steal_ptr(netconfig_agent));
 }
 
 void agent_shutdown(void)
 {
-	l_queue_foreach_remove(agents, release_agent, NULL);
+	l_queue_foreach_remove(secret_agents, release_agent, NULL);
+
+	if (netconfig_agent)
+		release_agent(l_steal_ptr(netconfig_agent), NULL);
 }
 
 IWD_MODULE(agent, agent_init, agent_exit);
src/agent.h +11 -0
index eb08c6b6..83625618 100644
@@ -25,8 +25,11 @@
 enum agent_result {
 	AGENT_RESULT_OK,
 	AGENT_RESULT_FAILED,
+	AGENT_RESULT_CANCELLED,
 };
 
+struct netconfig_data;
+
 typedef void (*agent_request_passphrase_func_t) (enum agent_result result,
 					const char *passphrase,
 					struct l_dbus_message *message,
@@ -35,6 +38,9 @@ typedef void (*agent_request_user_name_passwd_func_t) (enum agent_result result,
 					const char *user, const char *password,
 					struct l_dbus_message *message,
 					void *user_data);
+typedef void (*agent_request_netconfig_func_t) (unsigned int id,
+						enum agent_result result,
+						void *user_data);
 typedef void (*agent_request_destroy_func_t)(void *user_data);
 
 void agent_shutdown(void);
@@ -58,4 +64,9 @@ unsigned int agent_request_user_password(const char *path, const char *user,
 				agent_request_passphrase_func_t callback,
 				struct l_dbus_message *trigger, void *user_data,
 				agent_request_destroy_func_t destroy);
+unsigned int agent_request_netconfig(const char *path, const char *ifname,
+				const struct netconfig_data *data,
+				agent_request_netconfig_func_t callback,
+				void *user_data,
+				agent_request_destroy_func_t destroy);
 bool agent_request_cancel(unsigned int req_id, int reason);
src/dbus.h +2 -0
index 7703b37f..814b006b 100644
@@ -43,6 +43,8 @@
 #define IWD_STATION_DIAGNOSTIC_INTERFACE "net.connman.iwd.StationDiagnostic"
 #define IWD_AP_DIAGNOSTIC_INTERFACE "net.connman.iwd.AccessPointDiagnostic"
 #define IWD_STATION_DEBUG_INTERFACE "net.connman.iwd.StationDebug"
+#define IWD_NETCONFIG_AGENT_INTERFACE \
+	"net.connman.iwd.NetworkConfigurationAgent"
 
 #define IWD_BASE_PATH "/net/connman/iwd"
 #define IWD_AGENT_MANAGER_PATH IWD_BASE_PATH
src/netconfig.h +15 -0
index 9d5887e9..2f152b45 100644
@@ -28,6 +28,21 @@ enum netconfig_event {
 	NETCONFIG_EVENT_CONNECTED,
 };
 
+enum netconfig_method {
+	NETCONFIG_METHOD_STATIC,
+	NETCONFIG_METHOD_AUTO,
+};
+
+struct netconfig_data {
+	uint8_t family;
+	enum netconfig_method method;
+	char *addr;
+	uint8_t prefix_len;
+	char *gateway_addr;
+	char **dns;
+	char **domain_names;
+};
+
 typedef void (*netconfig_notify_func_t)(enum netconfig_event event,
 							void *user_data);
 
Signature · 1 line
2.32.0
author
#15 lore raw

PATCH 12/12 netconfig: Try calling the agent before setting new values

In reply to Andrew Zaborowski

Implement the logic where, after netconfig decides on what address, gateway, DNS, etc. data to use, it calls the NetworkConfigurationAgent that may be registered and relies on it to commit the new values. If no agent is registered it will proceed to set the new values as usual. If it does make an agent call, it wlil wait for the call to return before signalling NETCONFIG_EVENT_CONNECTED.

---
 src/netconfig.c | 114 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/netconfig.h |   1 +
 src/station.c   |   4 ++
 3 files changed, 119 insertions(+)
src/netconfig.c +114 -0
index 88e10e3f..a17e06bb 100644
@@ -49,6 +49,7 @@
 #include "src/resolve.h"
 #include "src/util.h"
 #include "src/ie.h"
+#include "src/agent.h"
 #include "src/netconfig.h"
 
 enum netconfig_changed_flags {
@@ -96,6 +97,10 @@ struct netconfig {
 	uint32_t addr6_add_cmd_id;
 	uint32_t route4_add_gateway_cmd_id;
 	uint32_t route6_add_cmd_id;
+
+	const char *dbus_obj_path;
+	unsigned int agent_v4_req_id;
+	unsigned int agent_v6_req_id;
 };
 
 static struct l_netlink *rtnl;
@@ -1062,6 +1067,92 @@ static void netconfig_ifaddr_del_cmd_cb(int error, uint16_t type,
 				"Error %d: %s", error, strerror(-error));
 }
 
+static void netconfig_agent_req_cb(unsigned int req_id, enum agent_result result,
+					void *user_data)
+{
+	struct netconfig *netconfig = user_data;
+	bool v4 = req_id == netconfig->agent_v4_req_id;
+
+	if (v4)
+		netconfig->agent_v4_req_id = 0;
+	else
+		netconfig->agent_v6_req_id = 0;
+
+	if (result != AGENT_RESULT_OK) {
+		l_error("Netconfig agent request for %s failed",
+			v4 ? "IPv4" : "IPv6");
+		/* TODO: report to user if v4 */
+		return;
+	}
+
+	l_debug("Netconfig agent request success for %s", v4 ? "IPv4" : "IPv6");
+
+	if (!v4)
+		return;
+
+	if (netconfig->notify) {
+		netconfig->notify(NETCONFIG_EVENT_CONNECTED,
+					netconfig->user_data);
+		netconfig->notify = NULL;
+	}
+}
+
+static bool netconfig_try_agent(struct netconfig *netconfig, uint8_t af)
+{
+	struct netconfig_data data = { .family = af };
+	const struct l_rtnl_address *addr;
+	char addr_str[INET6_ADDRSTRLEN];
+	char *v4_domains[] = { netconfig->v4_domain, NULL };
+	struct netdev *netdev = netdev_find(netconfig->ifindex);
+	unsigned int *id = (af == AF_INET) ?
+		&netconfig->agent_v4_req_id : &netconfig->agent_v6_req_id;
+
+	if (!netconfig->dbus_obj_path)
+		return false;
+
+	addr = (af == AF_INET) ? netconfig->v4_address : netconfig->v6_address;
+
+	if ((af == AF_INET && netconfig->rtm_protocol == RTPROT_STATIC) ||
+			(af == AF_INET6 &&
+			 netconfig->rtm_v6_protocol == RTPROT_STATIC))
+		data.method = NETCONFIG_METHOD_STATIC;
+	else if ((af == AF_INET && netconfig->rtm_protocol == RTPROT_DHCP) ||
+			(af == AF_INET6 &&
+			 netconfig->rtm_v6_protocol == RTPROT_DHCP))
+		data.method = NETCONFIG_METHOD_AUTO;
+	else
+		return true;
+
+	if (addr) {
+		if (l_rtnl_address_get_address(addr, addr_str))
+			data.addr = addr_str;
+
+		data.prefix_len = l_rtnl_address_get_prefix_length(addr);
+	}
+
+	data.gateway_addr = (af == AF_INET) ?
+		netconfig->v4_gateway_str : netconfig->v6_gateway_str;
+	data.dns = (af == AF_INET) ?
+		netconfig->dns4_list : netconfig->dns6_list;
+	data.domain_names = (af == AF_INET) ?
+		(v4_domains[0] ? v4_domains : NULL) : netconfig->v6_domains;
+
+	if (*id)
+		agent_request_cancel(*id, -ECANCELED);
+
+	*id = agent_request_netconfig(netconfig->dbus_obj_path,
+						netdev_get_name(netdev),
+						&data,
+						netconfig_agent_req_cb,
+						netconfig, NULL);
+	if (!*id)
+		return false;
+
+	l_debug("Netconfig request for %s on %s sent to agent",
+		(af == AF_INET) ? "IPv4" : "IPv6", netdev_get_name(netdev));
+	return true;
+}
+
 static bool netconfig_address_cmp_address(const struct l_rtnl_address *a,
 						const struct l_rtnl_address *b)
 {
@@ -1099,6 +1190,9 @@ static void netconfig_set(struct netconfig *netconfig, uint8_t af,
 {
 	struct l_rtnl_address *stale_addr;
 
+	if (netconfig_try_agent(netconfig, af))
+		return;
+
 	if (af == AF_INET)
 		stale_addr = l_steal_ptr(netconfig->stale_v4_address);
 	else
@@ -1421,6 +1515,12 @@ static void netconfig_reset_v4(struct netconfig *netconfig)
 
 		l_acd_destroy(netconfig->acd);
 		netconfig->acd = NULL;
+
+		if (netconfig->agent_v4_req_id) {
+			agent_request_cancel(netconfig->agent_v4_req_id,
+						-ECANCELED);
+			netconfig->agent_v4_req_id = 0;
+		}
 	}
 }
 
@@ -1460,6 +1560,12 @@ static void netconfig_reset_v6(struct netconfig *netconfig)
 
 		sysfs_write_ipv6_setting(netdev_get_name(netdev),
 						"disable_ipv6", "1");
+
+		if (netconfig->agent_v6_req_id) {
+			agent_request_cancel(netconfig->agent_v6_req_id,
+						-ECANCELED);
+			netconfig->agent_v6_req_id = 0;
+		}
 	}
 }
 
@@ -1815,6 +1921,8 @@ bool netconfig_reconfigure(struct netconfig *netconfig)
 
 bool netconfig_reset(struct netconfig *netconfig)
 {
+	netconfig->dbus_obj_path = NULL;
+
 	if (netconfig->route4_add_gateway_cmd_id) {
 		l_netlink_cancel(rtnl, netconfig->route4_add_gateway_cmd_id);
 		netconfig->route4_add_gateway_cmd_id = 0;
@@ -1955,6 +2063,11 @@ void netconfig_destroy(struct netconfig *netconfig)
 	netconfig_free(netconfig);
 }
 
+void netconfig_set_dbus_path(struct netconfig *netconfig, const char *path)
+{
+	netconfig->dbus_obj_path = path;
+}
+
 bool netconfig_get_enabled(void)
 {
 	bool enabled;
@@ -2034,3 +2147,4 @@ static void netconfig_exit(void)
 }
 
 IWD_MODULE(netconfig, netconfig_init, netconfig_exit)
+IWD_MODULE_DEPENDS(netconfig, agent)
src/netconfig.h +1 -0
index 2f152b45..709792fc 100644
@@ -61,5 +61,6 @@ void netconfig_handle_fils_ip_resp(struct netconfig *netconfig,
 
 struct netconfig *netconfig_new(uint32_t ifindex);
 void netconfig_destroy(struct netconfig *netconfig);
+void netconfig_set_dbus_path(struct netconfig *netconfig, const char *path);
 
 bool netconfig_get_enabled(void);
src/station.c +4 -0
index d3767203..6f311d76 100644
@@ -2734,6 +2734,10 @@ static void station_connect_ok(struct station *station)
 			}
 		}
 
+		/* The path remains valid until connected_network changes */
+		netconfig_set_dbus_path(station->netconfig,
+				network_get_path(station->connected_network));
+
 		if (L_WARN_ON(!netconfig_configure(station->netconfig,
 						station_netconfig_event_handler,
 						station)))
Signature · 1 line
2.32.0
#16 lore raw
In reply to Andrew Zaborowski

Hi Andrew,

Quoted 1 line · Andrew Zaborowski

Add netconfig_get_enabled() and use that in all places that want to know

So I renamed this to netconfig_enabled() since that better reflects the intent of this API.

Quoted 13 lines

whether network configuration is enabled. Drop the enable_network_config deprecated setting, which was only being handled in one of these 5 or so places.

---
  src/ap.c        | 11 +++--------
  src/ip-pool.c   | 11 ++---------
  src/main.c      |  8 ++------
  src/netconfig.c |  9 +++++++++
  src/netconfig.h |  2 ++
  src/resolve.c   | 12 ++----------
  src/station.c   | 10 ++--------
  7 files changed, 22 insertions(+), 41 deletions(-)

I also refactored main.c D-Bus API implementation slightly in a follow-on commit. Please review.

Regards,
-Denis