Delay connect after ANQP (for release?)
cover Delay connect after ANQP (for release?)
These were part of a larger set, but refactored to be stand alone. Since these are fixing a recently discovered race condition we may want them in the next release.
James Prestwood (4):
station: add ANQP state watch API
network: delay connect if ANQP has not completed
auto-t: add 'Delay' property to hwsim python module
auto-t: add test for delayed ANQP response
autotests/testHotspot/anqp_delay_test.py | 85 ++++++++++++++++++++++++ autotests/util/hwsim.py | 8 +++ src/network.c | 58 ++++++++++++++++ src/station.c | 21 ++++++ src/station.h | 13 ++++ 5 files changed, 185 insertions(+) create mode 100644 autotests/testHotspot/anqp_delay_test.py
Signature · 1 line
2.21.1
PATCH 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 | 21 +++++++++++++++++++++ src/station.h | 13 +++++++++++++ 2 files changed, 34 insertions(+)
src/station.c +21 -0
@@ -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;
@@ -489,6 +490,9 @@ static void station_anqp_response_cb(enum anqp_result result, request_done:l_queue_remove (station->anqp_pending, entry); +WATCHLIST_NOTIFY (&anqp_watches, station_anqp_watch_func_t, +STATION_ANQP_FINISHED , network); +/* If no more requests, resume scanning */ if (!l_queue_isempty (station->anqp_pending))return ;
@@ -564,6 +568,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 +1223,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 +3288,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 +3299,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
Re: [PATCH 1/4] station: add ANQP state watch API
Hi James,
Quoted 15 lines · James Prestwood
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 | 21 +++++++++++++++++++++ src/station.h | 13 +++++++++++++ 2 files changed, 34 insertions(+)
<snip>
src/station.c static void station_anqp_response_cb(enum anqp_result result, · line 495
Quoted 9 lines
struct station { enum station_state state;src/station.c +3
-0@@ -489,6 +490,9 @@ static void station_anqp_response_cb(enum anqp_result result, request_done:l_queue_remove (station->anqp_pending, entry); +WATCHLIST_NOTIFY (&anqp_watches, station_anqp_watch_func_t, +STATION_ANQP_FINISHED , network); +
So you don't want to delay until all anqp requests finish? Will the kernel cancel all the anqp work items nicely in this case?
src/station.c static void station_anqp_response_cb(enum anqp_result result, · line 498
Quoted 3 lines
src/station.c +0
-0/* If no more requests, resume scanning */ if (!l_queue_isempty (station->anqp_pending))return ;
Regards,
-Denis
Re: [PATCH 1/4] station: add ANQP state watch API
Quoted 29 lines · Denis Kenzior
Hi James,
Quoted 16 lines · James Prestwood
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 DBusConnect()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 | 21 +++++++++++++++++++++ src/station.h | 13 +++++++++++++ 2 files changed, 34 insertions(+)<snip>
Quoted 7 lines
struct station { enum station_state state;src/station.c +0
-0@@ -489,6 +490,9 @@ static void station_anqp_response_cb(enumanqp_result result, request_done: l_queue_remove(station->anqp_pending, entry);
Quoted 7 lines
src/station.c static void station_anqp_response_cb(enum anqp_result result, · line 495
Quoted 3 lines
src/station.c +3
-0+WATCHLIST_NOTIFY (&anqp_watches, station_anqp_watch_func_t, +STATION_ANQP_FINISHED , network); +So you don't want to delay until all anqp requests finish? Will the kernel
cancel all the anqp work items nicely in this case?
Yeah good point. We could do one of two things:
- Wait until all ANQP requets come back, then notify each network of
changes, or
- Notify as they come in and cancel any pending requests if a connect
comes in.
And no the kernel doesn't do any of this for us, I see ANQP responses coming in after we start doing a Connect() in my auto tests.
Quoted 7 lines
Quoted 4 lines
src/station.c +0
-0/* If no more requests, resume scanning */ if (!l_queue_isempty (station->anqp_pending))return;
Regards,
-Denis
PATCH 2/4 network: delay connect if ANQP has not completed
In reply to James PrestwoodUsing 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 | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+)
src/network.c +58 -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,11 @@ static struct l_dbus_message *network_connect_8021x(struct network *network, goto error; } +if (network->connect_after_anqp) { +l_dbus_message_unref (network->connect_after_anqp); + network->connect_after_anqp =NULL ; + } +station_connect_network (station, network, bss, message);return NULL ;
@@ -1156,6 +1169,19 @@ static struct l_dbus_message *network_connect(struct l_dbus *dbus,station_connect_network (station, network, bss, message);return NULL ;case SECURITY_8021X : +/* +* 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 +1510,33 @@ 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); +else + reply =network_connect_8021x (network, +network_bss_select (network, true), + network->connect_after_anqp); + +if (!reply) +return ; + +dbus_pending_reply (&network->connect_after_anqp, reply); + +return ; + } +} +static void setup_network_interface (struct l_dbus_interface *interface) { l_dbus_interface_method(interface, "Connect", 0,
@@ -1517,6 +1570,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 +1580,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 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 | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+)
<snip>
src/network.c static struct l_dbus_message *network_connect_8021x(struct network *network, · line 1117
Quoted 9 lines
src/network.c +5
-0@@ -1102,6 +1110,11 @@ static struct l_dbus_message *network_connect_8021x(struct network *network, goto error; } +if (network->connect_after_anqp) { +l_dbus_message_unref (network->connect_after_anqp); + network->connect_after_anqp =NULL ; + } +
Hmm, what does this do?
Quoted 10 lines
src/network.c +3
-0station_connect_network (station, network, bss, message);return NULL ;@@ -1156,6 +1169,19 @@ static struct l_dbus_message *network_connect(struct l_dbus *dbus,station_connect_network (station, network, bss, message);return NULL ;case SECURITY_8021X : +/* +* If there is an ongoing ANQP request we must wait for that to +* finish. Save the message and wait for the ANQP watch to
src/network.c static struct l_dbus_message *network_connect(struct l_dbus *dbus, · line 1184
Quoted 10 lines
src/network.c +10
-0+* 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 ; + } +
What if another request comes in during this time? Shouldn't you block the new request with an -EBUSY or something? Otherwise you leak connect_after_anqp.
Quoted 15 lines
src/network.c +8
-0if (!network_settings_load (network))return dbus_error_not_configured (message);@@ -1484,6 +1510,33 @@ 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; +
src/network.c static void known_networks_changed(enum known_networks_event event, · line 1530
Quoted 10 lines
src/network.c +10
-0+l_debug ("ANQP complete, resuming connect to %s" , network->ssid); + +if (!network_settings_load (network)) + reply =dbus_error_not_configured ( + network->connect_after_anqp); +else + reply =network_connect_8021x (network, +network_bss_select (network, true), + network->connect_after_anqp); +
Hmm, perhaps isolate network->connect_after_anqp in here instead of bleeding the details into network_connect_8021x?
Maybe something like:
if (!network_load_settings()) {
dbus_pending_reply(&network->connect_after_anqp,
dbus_error_not_configured());
return;
}reply = network_connect_8021x();
if (reply) {
struct l_dbus *dbus = dbus_get_bus();
l_dbus_send(dbus, reply);
}l_dbus_message_unref(network->connect_after_anqp); network->connect_after_anqp = NULL;
Quoted 23 lines
src/network.c +11
-0+if (!reply) +return ; + +dbus_pending_reply (&network->connect_after_anqp, reply); + +return ; + } +} +static void setup_network_interface (struct l_dbus_interface *interface) { l_dbus_interface_method(interface, "Connect", 0,@@ -1517,6 +1570,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 ; }
src/network.c static void network_exit(void) · line 1588
Quoted 9 lines
src/network.c +3
-0@@ -1525,6 +1580,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 ); }
Regards,
-Denis
Re: [PATCH 2/4] network: delay connect if ANQP has not completed
Quoted 21 lines · Denis Kenzior
Hi James,
Quoted 10 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 | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+)<snip>
Quoted 5 lines
src/network.c +0
-0@@ -1102,6 +1110,11 @@ static struct l_dbus_message *network_connect_8021x(struct network *network, goto error; }
Quoted 8 lines
src/network.c static struct l_dbus_message *network_connect_8021x(struct network *network, · line 1117
Quoted 6 lines
src/network.c +5
-0+if (network->connect_after_anqp) { +l_dbus_message_unref (network- >connect_after_anqp); + network->connect_after_anqp =NULL ; + } +Hmm, what does this do?
Just cleaning up, but I refactored it like you suggested below which makes this not needed.
Quoted 25 lines
Quoted 25 lines
(quoted diff) +0
-0station_connect_network(station, network, bss,message);
return NULL;
(quoted diff) +2
-0@@ -1156,6 +1169,19 @@ static struct l_dbus_message *network_connect(struct l_dbus *dbus, station_connect_network(station, network, bss, message); return NULL; case SECURITY_8021X: + /* + * If there is an ongoing ANQP request we must wait forthat to
(quoted diff) +1
-0+ * finish. Save the message and wait for the ANQP watchto
src/network.c +5
-0+* fire +*/ +if (network->anqp_pending) { + network->connect_after_anqp = + l_dbus_message_ref(message);
(quoted diff) +1
-0+ l_debug("Pending ANQP request, delaying connectto %s",
src/network.c +1
-0+ network->ssid);
Quoted 8 lines
src/network.c static struct l_dbus_message *network_connect(struct l_dbus *dbus, · line 1184
Quoted 3 lines
src/network.c +3
-0+return NULL ; + } +What if another request comes in during this time? Shouldn't you block the new
request with an-EBUSYor something? Otherwise you leakconnect_after_anqp.
Yep, fixed.
Quoted 45 lines
Quoted 30 lines
if (!network_settings_load(network)) return dbus_error_not_configured(message);src/network.c +9
-0@@ -1484,6 +1510,33 @@ 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);src/network.c +4
-0+ +if (!network_settings_load (network)) + reply = dbus_error_not_configured( + network-Quoted 1 line
connect_after_anqp);src/network.c +3
-0+else + reply = network_connect_8021x(network, + network_bss_select(network,true),
Review
Quoted 2 lines
(quoted diff) +2
-0+ network->connect_after_anqp); +Hmm, perhaps isolate
network->connect_after_anqpin here instead of bleeding the
details intonetwork_connect_8021x?Maybe something like:
if (!network_load_settings()) { dbus_pending_reply(&network->connect_after_anqp, dbus_error_not_configured()); return; }
Quoted 5 lines
reply =
network_connect_8021x();if (reply) { struct l_dbus *dbus = dbus_get_bus(); l_dbus_send(dbus, reply); }
Yeah I like this better.
Quoted 44 lines
l_dbus_message_unref(network->connect_after_anqp);network->connect_after_anqp= NULL;Quoted 28 lines
src/network.c +4
-0+if (!reply) +return ; + + dbus_pending_reply(&network->connect_after_anqp,reply);
src/network.c +5
-0+ +return ; + } +} + static void setup_network_interface(struct l_dbus_interface*interface)
{
l_dbus_interface_method(interface, "Connect", 0,src/network.c +2
-0@@ -1517,6 +1570,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 ;}src/network.c static void network_exit(void) · line 1588
Quoted 9 lines
src/network.c +3
-0@@ -1525,6 +1580,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 ); }Regards,
-Denis
PATCH 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 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