Wireless Daemon for Linux
 help / color / mirror / Atom feed
From: James Prestwood <prestwoj@gmail.com>
To: iwd@lists.linux.dev
Cc: James Prestwood <prestwoj@gmail.com>
Subject: [PATCH 2/5] knownnetworks: network: support updating known network settings
Date: Thu, 14 Dec 2023 10:01:07 -0800	[thread overview]
Message-ID: <20231214180110.130991-2-prestwoj@gmail.com> (raw)
In-Reply-To: <20231214180110.130991-1-prestwoj@gmail.com>

Currently if a known network is modified on disk the settings are not
reloaded by network. Only disconnecting/reconnecting to the network
would update the settings. This poses an issue to DPP since its
creating or updating a known network after configuration then trying
to connect. The connection itself works fine since the PSK/passphrase
is set to the network object directly, but any additional settings
are not updated.

To fix this add a new UPDATED known network event. This is then
handled from within network and all settings read from disk are
applied to the network object.
---
 src/knownnetworks.c |   4 ++
 src/knownnetworks.h |   1 +
 src/network.c       | 104 ++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 106 insertions(+), 3 deletions(-)

diff --git a/src/knownnetworks.c b/src/knownnetworks.c
index d4d50a6f..04ce74ec 100644
--- a/src/knownnetworks.c
+++ b/src/knownnetworks.c
@@ -468,6 +468,10 @@ void known_network_update(struct network_info *network,
 	known_network_set_autoconnect(network, new->is_autoconnectable);
 
 	memcpy(&network->config, new, sizeof(struct network_config));
+
+	WATCHLIST_NOTIFY(&known_network_watches,
+				known_networks_watch_func_t,
+				KNOWN_NETWORKS_EVENT_UPDATED, network);
 }
 
 bool known_networks_foreach(known_networks_foreach_func_t function,
diff --git a/src/knownnetworks.h b/src/knownnetworks.h
index 0a5c9e25..e8ffac0b 100644
--- a/src/knownnetworks.h
+++ b/src/knownnetworks.h
@@ -35,6 +35,7 @@ struct network_info;
 enum known_networks_event {
 	KNOWN_NETWORKS_EVENT_ADDED,
 	KNOWN_NETWORKS_EVENT_REMOVED,
+	KNOWN_NETWORKS_EVENT_UPDATED,
 };
 
 struct network_info_ops {
diff --git a/src/network.c b/src/network.c
index 41c5460b..bd3671ca 100644
--- a/src/network.c
+++ b/src/network.c
@@ -730,6 +730,73 @@ static void network_settings_save(struct network *network,
 		network_settings_save_sae_pt_ecc(settings, network->sae_pt_20);
 }
 
+static bool network_settings_update(struct network *network,
+					struct l_settings *new)
+{
+	bool have_transition_disable;
+	uint8_t transition_disable = 0;
+	unsigned int i;
+	size_t psk_len;
+	_auto_(l_strv_free) char **list = NULL;
+	_auto_(l_free) uint8_t *psk = NULL;
+	_auto_(l_free) char *passphrase = NULL;
+
+	if (l_settings_get_bool(new, NET_TRANSITION_DISABLE,
+				&have_transition_disable) &&
+				have_transition_disable) {
+		list = l_settings_get_string_list(new,
+					NET_TRANSITION_DISABLE_MODES, ' ');
+
+		for (i = 0; list[i]; i++) {
+			if (!strcmp(list[i], "personal"))
+				set_bit(&transition_disable, 0);
+			else if (!strcmp(list[i], "enterprise"))
+				set_bit(&transition_disable, 1);
+			else if (!strcmp(list[i], "open"))
+				set_bit(&transition_disable, 2);
+		}
+
+		have_transition_disable = true;
+	} else
+		have_transition_disable = false;
+
+	if (network->security != SECURITY_PSK)
+		goto apply;
+
+	psk = l_settings_get_bytes(network->settings, "Security",
+					"PreSharedKey", &psk_len);
+	if (psk && psk_len != 32) {
+		l_warn("updated [Security].PreSharedKey value is invalid!");
+		return false;
+	}
+
+	passphrase = l_settings_get_string(network->settings,
+						"Security", "Passphrase");
+	if (passphrase && !crypto_passphrase_is_valid(passphrase)) {
+		l_warn("updated [Security].Passphrase value is invalid!");
+		return false;
+	}
+
+apply:
+	network_settings_close(network);
+	network->settings = new;
+
+	network->have_transition_disable = have_transition_disable;
+	network->transition_disable = transition_disable;
+
+	if (psk)
+		network->psk = l_steal_ptr(psk);
+
+	if (passphrase) {
+		network->passphrase = l_strdup(passphrase);
+
+		network_settings_load_pt_ecc(network, 19, &network->sae_pt_19);
+		network_settings_load_pt_ecc(network, 20, &network->sae_pt_20);
+	}
+
+	return true;
+}
+
 void network_sync_settings(struct network *network)
 {
 	struct network_info *info = network->info;
@@ -1966,17 +2033,32 @@ static void network_update_hotspot(struct network *network, void *user_data)
 	match_hotspot_network(info, network);
 }
 
-static void match_known_network(struct station *station, void *user_data)
+static void match_known_network(struct station *station, void *user_data,
+				bool new)
 {
 	struct network_info *info = user_data;
 	struct network *network;
 
 	if (!info->is_hotspot) {
+		struct l_settings *settings;
 		network = station_network_find(station, info->ssid, info->type);
 		if (!network)
 			return;
 
-		network_set_info(network, info);
+		/* New networks should load settings upon connecting */
+		if (new) {
+			network_set_info(network, info);
+			return;
+		}
+
+		settings = network_info_open_settings(info);
+
+		if (!settings || !network_settings_update(network, settings)) {
+			l_warn("Failed to apply new/updated settings (%s)",
+					info->ssid);
+			l_settings_free(settings);
+		}
+
 		return;
 	}
 
@@ -1984,17 +2066,33 @@ static void match_known_network(struct station *station, void *user_data)
 	station_network_foreach(station, network_update_hotspot, info);
 }
 
+static void add_known_network(struct station *station, void *user_data)
+{
+	match_known_network(station, (struct network_info *)user_data, true);
+}
+
+static void update_known_network(struct station *station, void *user_data)
+{
+	match_known_network(station, (struct network_info *)user_data, false);
+}
+
 static void known_networks_changed(enum known_networks_event event,
 					const struct network_info *info,
 					void *user_data)
 {
 	switch (event) {
 	case KNOWN_NETWORKS_EVENT_ADDED:
-		station_foreach(match_known_network, (void *) info);
+		station_foreach(add_known_network, (void *) info);
 
 		/* Syncs frequencies of newly known network */
 		known_network_frequency_sync((struct network_info *)info);
 		break;
+	case KNOWN_NETWORKS_EVENT_UPDATED:
+		station_foreach(update_known_network, (void *) info);
+
+		/* Syncs frequencies of updated known network */
+		known_network_frequency_sync((struct network_info *)info);
+		break;
 	case KNOWN_NETWORKS_EVENT_REMOVED:
 		station_foreach(emit_known_network_removed, (void *) info);
 		break;
-- 
2.34.1


  reply	other threads:[~2023-12-14 18:01 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-14 18:01 [PATCH 1/5] network: remove 'path' from settings_load_pt_ecc James Prestwood
2023-12-14 18:01 ` James Prestwood [this message]
2023-12-15 16:37   ` [PATCH 2/5] knownnetworks: network: support updating known network settings Denis Kenzior
2023-12-14 18:01 ` [PATCH 3/5] dpp: fix extra settings not being used when connecting James Prestwood
2023-12-14 18:01 ` [PATCH 4/5] auto-t: add DPP tests to check extra settings are applied James Prestwood
2023-12-14 18:01 ` [PATCH 5/5] auto-t: increase RAM when running with valgrind (UML) James Prestwood
2023-12-15 16:28 ` [PATCH 1/5] network: remove 'path' from settings_load_pt_ecc Denis Kenzior

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231214180110.130991-2-prestwoj@gmail.com \
    --to=prestwoj@gmail.com \
    --cc=iwd@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox