station: add ANQP state watch API
PATCH v2 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 | 57 ++++++++++++++++++++++++++++++++++++++++++++++++--- src/station.h | 13 ++++++++++++ 2 files changed, 67 insertions(+), 3 deletions(-)
v2:
- Changed to notify ANQP watches after all the requests come in, not as each comes in. This prevents Connect() and ANQP requests happening at the same time.
src/station.c +54 -3
@@ -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,36 @@ 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; +uint64_t wdev_id =netdev_get_wdev_id (entry->station->netdev); + +if (entry->pending) +anqp_cancel (wdev_id, entry->pending); + +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 +518,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 +598,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 +1253,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 +3318,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 +3329,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
@@ -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
PATCH v2 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(+)
v2:
- Moved most of the logic out of _connect_8021x and into the ANQP watch callback. Setting connect_after_anqp to NULL was still required though because if an agent request is needed this could let autoconnect try and connect to a network. This is checked for in network_autoconnect but only if connect_after_anqp is non-NULL. So we wait until the last possible moment to NULL that.
src/network.c +66 -0
@@ -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 ANQP response to connect */ +if (network->connect_after_anqp) +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
Re: [PATCH v2 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_autoconnectwhich prevents it from autoconnecting if we have a pending Connect request.--- src/network.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+)
Quoted 8 lines
v2:
- Moved most of the logic out of_connect_8021xand into the ANQP watch callback. Settingconnect_after_anqpto NULL was still required though because if an agent request is needed this could let autoconnect try and connect to a network. This is checked for innetwork_autoconnectbut only ifconnect_after_anqpis non-NULL. So we wait until the last possible moment to NULL that.
Sorry I couldn't make up my mind yesterday as to which way to go in canceling remaining ANQP requests on connect or not.
This series looks good, but I'm not sure why you'd treat 'connect_after_anqp' differently than a regular connect request over d-bus. Those can come in during autoconnect phase and can still get stuck in the 'agent limbo'...
By the way, it might also warrant a WARN_ON if we ever hit the
+ if (network->connect_after_anqp)
+ return -EALREADY;
+
condition inside network_autoconnect(). Since it really shouldn't happen and is likely a bug in station if it did.
Regards,
-Denis
Re: [PATCH v2 2/4] network: delay connect if ANQP has not completed
Hi Denis,
Quoted 16 lines
Sorry I couldn't make up my mind yesterday as to which way to go in canceling
remaining ANQP requests on connect or not.This series looks good, but I'm not sure why you'd treat '
connect_after_anqp'
differently than a regular connect request over d-bus. Those can come in during
autoconnect phase and can still get stuck in the 'agent limbo'...By the way, it might also warrant a
WARN_ONif we ever hit thesrc/network.c +3
-0+if (network->connect_after_anqp) +return -EALREADY ; +
Quoted 3 lines
condition inside
network_autoconnect(). Since it really shouldn't happen and is
likely a bug in station if it did.
So this behavior is actually happening with my new ANQP delay autotest:
- During scan callback ANQP is started
- Connect() comes in before ANQP is done.
- ANQP finishes and notifies network, which resumes the Connect(). But
we need to now do an agent request.
- Then autoconnect comes in and attempts to connect, but we are
missing secrets so it then closes the network settings
- Agent request comes back and settings have been destroyed out from
under it so we fail with NotConfigured.
Maybe instead we could have network disable autoconnect when we are in this limbo state of an agent request? And/or while waiting for ANQP to finish.
Quoted 3 lines
Regards,
-Denis
Re: [PATCH v2 2/4] network: delay connect if ANQP has not completed
Quoted 32 lines · James Prestwood
Hi Denis,
Quoted 16 lines
Sorry I couldn't make up my mind yesterday as to which way to go in canceling
remaining ANQP requests on connect or not.This series looks good, but I'm not sure why you'd treat '
connect_after_anqp'
differently than a regular connect request over d-bus. Those can come in during
autoconnect phase and can still get stuck in the 'agent limbo'...By the way, it might also warrant a
WARN_ONif we ever hit thesrc/network.c +3
-0+if (network->connect_after_anqp) +return -EALREADY ; +Quoted 3 lines
condition inside
network_autoconnect(). Since it really shouldn't happen and is
likely a bug in station if it did.So this behavior is actually happening with my new ANQP delay autotest:
- During scan callback ANQP is started
-Connect()comes in before ANQP is done.
- ANQP finishes and notifies network, which resumes theConnect(). But
we need to now do an agent request.
- Then autoconnect comes in and attempts to connect, but we are
missing secrets so it then closes the network settings
- Agent request comes back and settings have been destroyed out from
under it so we fail with NotConfigured.
Quoted 3 lines
Maybe instead we could have network disable autoconnect when we are in this limbo state of an agent request? And/or while waiting for ANQP to finish.
But this behavior is not related to anqp itself. Hence why I'm asking why you're trying to treat the 'connect while in anqp and then needing secrets' different from 'connect while in autoconnect and needing secrets'
Maybe we should simply not close settings if agent requests are pending in network_autoconnect.
Regards,
-Denis
Re: [PATCH v2 2/4] network: delay connect if ANQP has not completed
Quoted 50 lines · Denis Kenzior
Quoted 37 lines · James Prestwood
Hi Denis,
Quoted 17 lines
Sorry I couldn't make up my mind yesterday as to which way to go in
canceling
remaining ANQP requests on connect or not.This series looks good, but I'm not sure why you'd treat '
connect_after_anqp'
differently than a regular connect request over d-bus. Those can come in during
autoconnect phase and can still get stuck in the 'agent limbo'...By the way, it might also warrant a
WARN_ONif we ever hit thesrc/network.c +3
-0+if (network->connect_after_anqp) +return -EALREADY ; +Quoted 4 lines
condition inside
network_autoconnect(). Since it really shouldn't
happen and is
likely a bug in station if it did.So this behavior is actually happening with my new ANQP delay autotest:
- During scan callback ANQP is started
-Connect()comes in before ANQP is done.
- ANQP finishes and notifies network, which resumes theConnect(). But
we need to now do an agent request.
- Then autoconnect comes in and attempts to connect, but we are
missing secrets so it then closes the network settings
- Agent request comes back and settings have been destroyed out
from
under it so we fail with NotConfigured.Quoted 5 lines
Maybe instead we could have network disable autoconnect when we are in
this limbo state of an agent request? And/or while waiting for ANQP to
finish.But this behavior is not related to anqp itself. Hence why I'm asking why
you're trying to treat the 'connect while in anqp and then needing secrets'
different from 'connect while in autoconnect and needing secrets'
Quoted 3 lines
Maybe we should simply not close settings if agent requests are pending in
network_autoconnect.
Really not do *anything* in network_autoconnect if agent requests are pending. Ill replace my network->connect_after_anqp check with network-
Quoted 5 lines
agent_requestinstead.Regards,
-Denis
Re: [PATCH v2 2/4] network: delay connect if ANQP has not completed
Hi James,
Quoted 7 lines
Quoted 3 lines
Maybe we should simply not close settings if agent requests are pending in
network_autoconnect.Really not do *anything* in
network_autoconnectif agent requests are pending. Ill replace mynetwork->connect_after_anqpcheck with network-Quoted 1 line
agent_requestinstead.
That's fine. I suspect that if an agent request is in progress, then we're missing secrets or need the psk. So, really the result should be equivalent if you simply let the normal processing go on.
Regards,
-Denis
PATCH v2 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
@@ -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
PATCH v2 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
@@ -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