Skip to contentSkip to conversation

station: add ANQP state watch API

PATCH In review James Prestwood 2 participants 5 messages Jun 12, 2020

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/4 station: add ANQP state watch API

This is to allow network to watch for ANQP activity in order to fix the race condition between scanning finishing and ANQP finishing. Without this it is possible for a DBus Connect() to come in before ANQP has completed and causing the network to return NotConfigured, when its actually in the process of obtaining all the network info.

The watch was made globally in station due to network not having a station object until each individual network is created. Adding a watch during network creation would result in many watchers as well as a lot of removal/addition as networks are found and lost.

---
 src/station.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++---
 src/station.h | 13 +++++++++++++
 2 files changed, 63 insertions(+), 3 deletions(-)
src/station.c +50 -3
index 87fadc3f..f0a0e810 100644
@@ -59,6 +59,7 @@ static uint32_t netdev_watch;
 static uint32_t mfp_setting;
 static bool anqp_disabled;
 static bool netconfig_enabled;
+static struct watchlist anqp_watches;
 
 struct station {
 	enum station_state state;
@@ -434,6 +435,32 @@ static void network_add_foreach(struct network *network, void *user_data)
 	station_add_autoconnect_bss(station, network, bss);
 }
 
+static bool match_pending(const void *a, const void *b)
+{
+	const struct anqp_entry *entry = a;
+
+	return entry->pending != 0;
+}
+
+static void remove_anqp(void *data)
+{
+	struct anqp_entry *entry = data;
+
+	l_free(entry);
+}
+
+static bool anqp_entry_foreach(void *data, void *user_data)
+{
+	struct anqp_entry *e = data;
+
+	WATCHLIST_NOTIFY(&anqp_watches, station_anqp_watch_func_t,
+				STATION_ANQP_FINISHED, e->network);
+
+	remove_anqp(e);
+
+	return true;
+}
+
 static void station_anqp_response_cb(enum anqp_result result,
 					const void *anqp, size_t anqp_len,
 					void *user_data)
@@ -487,12 +514,15 @@ static void station_anqp_response_cb(enum anqp_result result,
 	l_strv_free(realms);
 
 request_done:
-	l_queue_remove(station->anqp_pending, entry);
+	entry->pending = 0;
 
-	/* If no more requests, resume scanning */
-	if (!l_queue_isempty(station->anqp_pending))
+	/* Return if there are other pending requests */
+	if (l_queue_find(station->anqp_pending, match_pending, NULL))
 		return;
 
+	/* Notify all watchers now that every ANQP request has finished */
+	l_queue_foreach_remove(station->anqp_pending, anqp_entry_foreach, NULL);
+
 	l_queue_destroy(station->autoconnect_list, l_free);
 	station->autoconnect_list = l_queue_new();
 
@@ -564,6 +594,8 @@ static bool station_start_anqp(struct station *station, struct network *network,
 
 	l_queue_push_head(station->anqp_pending, entry);
 
+	WATCHLIST_NOTIFY(&anqp_watches, station_anqp_watch_func_t,
+				STATION_ANQP_STARTED, network);
 	return true;
 }
 
@@ -1217,6 +1249,18 @@ bool station_remove_state_watch(struct station *station, uint32_t id)
 	return watchlist_remove(&station->state_watches, id);
 }
 
+uint32_t station_add_anqp_watch(station_anqp_watch_func_t func,
+				void *user_data,
+				station_destroy_func_t destroy)
+{
+	return watchlist_add(&anqp_watches, func, user_data, destroy);
+}
+
+void station_remove_anqp_watch(uint32_t id)
+{
+	watchlist_remove(&anqp_watches, id);
+}
+
 bool station_set_autoconnect(struct station *station, bool autoconnect)
 {
 	if (station->autoconnect == autoconnect)
@@ -3270,6 +3314,8 @@ static int station_init(void)
 	if (!netconfig_enabled)
 		l_info("station: Network configuration is disabled.");
 
+	watchlist_init(&anqp_watches, NULL);
+
 	return 0;
 }
 
@@ -3279,6 +3325,7 @@ static void station_exit(void)
 	netdev_watch_remove(netdev_watch);
 	l_queue_destroy(station_list, NULL);
 	station_list = NULL;
+	watchlist_destroy(&anqp_watches);
 }
 
 IWD_MODULE(station, station_init, station_exit)
src/station.h +13 -0
index 4155081b..17a0f8df 100644
@@ -43,8 +43,16 @@ enum station_state {
 	STATION_STATE_ROAMING
 };
 
+enum station_anqp_state {
+	STATION_ANQP_STARTED,
+	STATION_ANQP_FINISHED,
+};
+
 typedef void (*station_foreach_func_t)(struct station *, void *data);
 typedef void (*station_state_watch_func_t)(enum station_state, void *userdata);
+typedef void (*station_anqp_watch_func_t)(enum station_anqp_state,
+						struct network *network,
+						void *user_data);
 typedef void (*station_destroy_func_t)(void *userdata);
 typedef void (*station_network_foreach_func_t)(struct network *, void *data);
 
@@ -66,6 +74,11 @@ uint32_t station_add_state_watch(struct station *station,
 					station_destroy_func_t destroy);
 bool station_remove_state_watch(struct station *station, uint32_t id);
 
+uint32_t station_add_anqp_watch(station_anqp_watch_func_t func,
+				void *user_data,
+				station_destroy_func_t destroy);
+void station_remove_anqp_watch(uint32_t id);
+
 bool station_set_autoconnect(struct station *station, bool autoconnect);
 
 void station_ap_directed_roam(struct station *station,
Signature · 1 line
2.21.1
#1 lore raw

PATCH v3 2/4 network: delay connect if ANQP has not completed

Using the new station ANQP watch network can delay the connection request until after ANQP has finished. Since station may be autoconnecting we must also add a check in network_autoconnect which prevents it from autoconnecting if we have a pending Connect request.

---
 src/network.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 66 insertions(+)

v3:
- Check for agent_request during network_autoconnect rather than connect_after_anqp.

src/network.c +66 -0
index 170db3e5..c0c6f1b0 100644
@@ -52,6 +52,7 @@
 #include "src/util.h"
 
 static uint32_t known_networks_watch;
+static uint32_t anqp_watch;
 
 struct network {
 	char ssid[33];
@@ -72,7 +73,10 @@ struct network {
 	bool update_psk:1;  /* Whether PSK should be written to storage */
 	bool ask_passphrase:1; /* Whether we should force-ask agent */
 	bool is_hs20:1;
+	bool anqp_pending:1;	/* Set if there is a pending ANQP request */
 	int rank;
+	/* Holds DBus Connect() message if it comes in before ANQP finishes */
+	struct l_dbus_message *connect_after_anqp;
 };
 
 static bool network_settings_load(struct network *network)
@@ -521,6 +525,10 @@ int network_autoconnect(struct network *network, struct scan_bss *bss)
 	bool is_rsn;
 	int ret;
 
+	/* already waiting for an agent request, connect in progress */
+	if (network->agent_request)
+		return -EALREADY;
+
 	switch (security) {
 	case SECURITY_NONE:
 		is_rsn = false;
@@ -1102,6 +1110,9 @@ static struct l_dbus_message *network_connect_8021x(struct network *network,
 			goto error;
 		}
 
+		/* Unset this since we are now ready to connect */
+		network->connect_after_anqp = NULL;
+
 		station_connect_network(station, network, bss, message);
 
 		return NULL;
@@ -1156,6 +1167,22 @@ static struct l_dbus_message *network_connect(struct l_dbus *dbus,
 		station_connect_network(station, network, bss, message);
 		return NULL;
 	case SECURITY_8021X:
+		if (network->connect_after_anqp)
+			return dbus_error_busy(message);
+
+		/*
+		 * If there is an ongoing ANQP request we must wait for that to
+		 * finish. Save the message and wait for the ANQP watch to
+		 * fire
+		 */
+		if (network->anqp_pending) {
+			network->connect_after_anqp =
+						l_dbus_message_ref(message);
+			l_debug("Pending ANQP request, delaying connect to %s",
+						network->ssid);
+			return NULL;
+		}
+
 		if (!network_settings_load(network))
 			return dbus_error_not_configured(message);
 
@@ -1484,6 +1511,40 @@ static void known_networks_changed(enum known_networks_event event,
 	}
 }
 
+static void anqp_watch_changed(enum station_anqp_state state,
+				struct network *network, void *user_data)
+{
+	network->anqp_pending = state == STATION_ANQP_STARTED;
+
+	if (state == STATION_ANQP_FINISHED && network->connect_after_anqp) {
+		struct l_dbus_message *reply;
+
+		l_debug("ANQP complete, resuming connect to %s", network->ssid);
+
+		if (!network_settings_load(network)) {
+			reply = dbus_error_not_configured(
+						network->connect_after_anqp);
+			dbus_pending_reply(&network->connect_after_anqp, reply);
+			return;
+		}
+
+		reply = network_connect_8021x(network,
+					network_bss_select(network, true),
+					network->connect_after_anqp);
+
+		if (reply)
+			l_dbus_send(dbus_get_bus(), reply);
+
+		/*
+		 * Unref since network_connect_8021x will hold its own ref, but
+		 * don't NULL this yet because agent requests may be required,
+		 * and we still want this network to be in a 'pending' type of
+		 * state so autoconnect doesn't try and connect again.
+		 */
+		l_dbus_message_unref(network->connect_after_anqp);
+	}
+}
+
 static void setup_network_interface(struct l_dbus_interface *interface)
 {
 	l_dbus_interface_method(interface, "Connect", 0,
@@ -1517,6 +1578,8 @@ static int network_init(void)
 	known_networks_watch =
 		known_networks_watch_add(known_networks_changed, NULL, NULL);
 
+	anqp_watch = station_add_anqp_watch(anqp_watch_changed, NULL, NULL);
+
 	return 0;
 }
 
@@ -1525,6 +1588,9 @@ static void network_exit(void)
 	known_networks_watch_remove(known_networks_watch);
 	known_networks_watch = 0;
 
+	station_remove_anqp_watch(anqp_watch);
+	anqp_watch = 0;
+
 	l_dbus_unregister_interface(dbus_get_bus(), IWD_NETWORK_INTERFACE);
 }
 
Signature · 1 line
2.21.1
#2 lore raw

Re: [PATCH v3 2/4] network: delay connect if ANQP has not completed

Hi James,

Quoted 9 lines · James Prestwood

Using the new station ANQP watch network can delay the connection request until after ANQP has finished. Since station may be autoconnecting we must also add a check in network_autoconnect which prevents it from autoconnecting if we have a pending Connect request.

---
  src/network.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 66 insertions(+)

<snip>

src/network.c static struct l_dbus_message *network_connect_8021x(struct network *network, · line 1115

Quoted 7 lines
src/network.c +3 -0
@@ -1102,6 +1110,9 @@ static struct l_dbus_message *network_connect_8021x(struct network *network,
  			goto error;
  		}
 
+		/* Unset this since we are now ready to connect */
+		network->connect_after_anqp = NULL;
+

So I moved this NULL setting to...

Quoted 4 lines
src/network.c +4 -0
+		reply = network_connect_8021x(network,
+					network_bss_select(network, true),
+					network->connect_after_anqp);
+

src/network.c static void known_networks_changed(enum known_networks_event event, · line 1544

Quoted 10 lines
src/network.c +10 -0
+		if (reply)
+			l_dbus_send(dbus_get_bus(), reply);
+
+		/*
+		 * Unref since network_connect_8021x will hold its own ref, but
+		 * don't NULL this yet because agent requests may be required,
+		 * and we still want this network to be in a 'pending' type of
+		 * state so autoconnect doesn't try and connect again.
+		 */
+		l_dbus_message_unref(network->connect_after_anqp);

here, and also squashed the now unneeded comment.

And applied this + rest of the series.

Regards,
-Denis

#3 lore raw

PATCH v3 3/4 auto-t: add 'Delay' property to hwsim python module

In reply to James Prestwood
---
 autotests/util/hwsim.py | 8 ++++++++
 1 file changed, 8 insertions(+)
autotests/util/hwsim.py +8 -0
index 4d3748b1..6d2b3e8f 100755
@@ -112,6 +112,14 @@ class Rule(HwsimDBusAbstract):
     def drop(self, value):
         self._prop_proxy.Set(self._iface_name, 'Drop', dbus.Boolean(value))
 
+    @property
+    def delay(self):
+        return int(self._properties['Delay'])
+
+    @delay.setter
+    def delay(self, value):
+        self._prop_proxy.Set(self._iface_name, 'Delay', dbus.UInt32(value))
+
     def remove(self):
         self._iface.Remove(reply_handler=self._success,
                 error_handler=self._failure)
Signature · 1 line
2.21.1
#4 lore raw

PATCH v3 4/4 auto-t: add test for delayed ANQP response

In reply to James Prestwood
---
 autotests/testHotspot/anqp_delay_test.py | 85 ++++++++++++++++++++++++
 1 file changed, 85 insertions(+)
 create mode 100644 autotests/testHotspot/anqp_delay_test.py
autotests/testHotspot/anqp_delay_test.py new file +85 -0
new file mode 100644
index 00000000..431f16c1
@@ -0,0 +1,85 @@
+#!/usr/bin/python3
+
+import unittest
+import sys
+import os
+
+sys.path.append('../util')
+import iwd
+from iwd import IWD
+from iwd import PSKAgent
+from iwd import NetworkType
+from hostapd import HostapdCLI
+from hwsim import Hwsim
+import testutil
+from time import sleep
+
+class Test(unittest.TestCase):
+
+    def test_connection_success(self):
+        hwsim = Hwsim()
+
+        bss_radio = hwsim.get_radio('rad0')
+        rule0 = hwsim.rules.create()
+        rule0.source = bss_radio.addresses[0]
+        rule0.bidirectional = True
+        # Force the case where ANQP does not finish before Connect() comes in
+        rule0.delay = 100
+
+        wd = IWD(True, '/tmp')
+
+        hapd = HostapdCLI(config='ssidHotspot.conf')
+
+        psk_agent = PSKAgent('abc', ('domain\\user', 'testpasswd'))
+        wd.register_psk_agent(psk_agent)
+
+        devices = wd.list_devices(1)
+        device = devices[0]
+
+        # We are dependent on a periodic scan here. We want to wait for this
+        # because this is the first opportunity IWD has to do ANQP. Once ANQP
+        # has been done once the network is set up and we cannot simulate the
+        # 'Connect() before ANQP' race condition anymore.
+        condition = 'obj.scanning'
+        wd.wait_for_object_condition(device, condition)
+
+        condition = 'not obj.scanning'
+        wd.wait_for_object_condition(device, condition)
+
+        ordered_network = device.get_ordered_network('Hotspot')
+
+        self.assertEqual(ordered_network.type, NetworkType.eap)
+
+        condition = 'not obj.connected'
+        wd.wait_for_object_condition(ordered_network.network_object, condition)
+
+        ordered_network.network_object.connect()
+
+        rule0.delay = 1
+
+        condition = 'obj.connected'
+        wd.wait_for_object_condition(ordered_network.network_object, condition)
+
+        testutil.test_iface_operstate()
+        testutil.test_ifaces_connected(device.name, hapd.ifname)
+
+        device.disconnect()
+
+        condition = 'not obj.connected'
+        wd.wait_for_object_condition(ordered_network.network_object, condition)
+
+        wd.unregister_psk_agent(psk_agent)
+
+    @classmethod
+    def setUpClass(cls):
+        IWD.copy_to_hotspot('example.conf')
+        conf = '[General]\nDisableANQP=0\n'
+        os.system('echo "%s" > /tmp/main.conf' % conf)
+
+    @classmethod
+    def tearDownClass(cls):
+        IWD.clear_storage()
+        os.remove('/tmp/main.conf')
+
+if __name__ == '__main__':
+    unittest.main(exit=True)
Signature · 1 line
2.21.1