auto-t: support hostapd event processing
PATCH 1/5 auto-t: support hostapd event processing
Hostapd has a feature where you can connect to its control socket and receive events it generates. Currently we only send commands via this socket.
First we open the socket (/var/run/hostapd/<iface>) and send the ATTACH command. This tells hostapd we are ready and after this any events will be sent over this socket.
A new API, wait_for_event, was added which takes an event string and waits for some timeout. The glib event loop has been integrated into this, though its not technically async since we are selecting over a socket which blocks. To mitigate this a small timeout was chosen for each select call and then wrapped in a while loop which waits for the full timeout.
--- autotests/util/hostapd.py | 68 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+)
autotests/util/hostapd.py +68 -0
@@ -3,6 +3,9 @@ import os, os.path from wiphy import wiphy_map import re import socket +import select +import time +from gi.repository import GLib chan_freq_map = [ None,
@@ -22,12 +25,17 @@ chan_freq_map = [ 2484 ] +ctrl_count = 0 +mainloop = GLib.MainLoop() + hostapd_map = {ifname: intf for wname, wiphy in wiphy_map.items() for ifname, intf in wiphy.interface_map.items() if wiphy.use == 'hostapd'} class HostapdCLI: def __init__(self, interface=None, config=None): + global ctrl_count + if not interface and not config: raise Exception('interface or config must be provided')
@@ -48,10 +56,64 @@ class HostapdCLI: self._hostapd_restarted = False + self.local_ctrl = '/tmp/hostapd_' + str(os.getpid()) + '_' + \ + str(ctrl_count) + self.ctrl_sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) + self.ctrl_sock.bind(self.local_ctrl) + self.ctrl_sock.connect(self.socket_path + '/' + self.ifname) + + if 'OK' not in self._ctrl_request('ATTACH'): + raise Exception('ATTACH failed') + + ctrl_count = ctrl_count + 1 + + def wait_for_event(self, event, timeout=10): + global mainloop + self._wait_timed_out = False + + def wait_timeout_cb(): + self._wait_timed_out = True + return False + + timeout = GLib.timeout_add_seconds(timeout, wait_timeout_cb) + context = mainloop.get_context() + + while True: + context.iteration(may_block=False) + + while self._data_available(0.25): + data = self.ctrl_sock.recv(4096).decode('utf-8') + if event in data: + return data + + if self._wait_timed_out: + raise TimeoutError('waiting for hostapd event timed out') + + return None + + def _data_available(self, timeout=2): + [r, w, e] = select.select([self.ctrl_sock], [], [], timeout) + if r: + return True + return False + + def _ctrl_request(self, command, timeout=10): + if type(command) is str: + command = str.encode(command) + + self.ctrl_sock.send(bytes(command)) + + if self._data_available(timeout): + return self.ctrl_sock.recv(4096).decode('utf-8') + + raise Exception('timeout waiting for control response') + def __del__(self): if self._hostapd_restarted: os.system('killall hostapd') + self.ctrl_sock.close() + def wps_push_button(self): os.system(self.cmdline + ' wps_pbc')
@@ -140,3 +202,9 @@ class HostapdCLI: # set flag so hostapd can be killed after the test self._hostapd_restarted = True + + def req_beacon(self, addr, request): + ''' + Send a RRM Beacon request + ''' + os.system(self.cmdline + ' req_beacon ' + addr + ' ' + request)
Signature · 1 line
2.17.1
PATCH 2/5 wiphy: add beacon bits to RM Enabled Capabilities
This tells AP's that we support Passive, Active, and Table beacon measurements.
--- src/wiphy.c | 2 ++ 1 file changed, 2 insertions(+)
src/wiphy.c +2 -0
@@ -961,6 +961,8 @@ static void wiphy_setup_rm_enabled_capabilities(struct wiphy *wiphy) wiphy->rm_enabled_capabilities[0 ] =IE_TYPE_RM_ENABLED_CAPABILITIES ; wiphy->rm_enabled_capabilities[1 ] =5 ; +/* Bits: Passive (4), Active (5), and Beacon Table (6) capabilities */ + wiphy->rm_enabled_capabilities[2 ] =0x70 ;/* * TODO: Support at least Link Measurement if TX_POWER_INSERTION is
Signature · 1 line
2.17.1
Re: [PATCH 2/5] wiphy: add beacon bits to RM Enabled Capabilities
Hi James,
Quoted 11 lines · James Prestwood
This tells AP's that we support Passive, Active, and Table beacon measurements.
--- src/wiphy.c | 2 ++ 1 file changed, 2 insertions(+)src/wiphy.c +0
-0
src/wiphy.c static void wiphy_setup_rm_enabled_capabilities(struct wiphy *wiphy) · line 965
Quoted 5 lines
src/wiphy.c +2
-0@@ -961,6 +961,8 @@ static void wiphy_setup_rm_enabled_capabilities(struct wiphy *wiphy) wiphy->rm_enabled_capabilities[0 ] =IE_TYPE_RM_ENABLED_CAPABILITIES ; wiphy->rm_enabled_capabilities[1 ] =5 ; +/* Bits: Passive (4), Active (5), and Beacon Table (6) capabilities */ + wiphy->rm_enabled_capabilities[2 ] =0x70 ;
Do you really mean to add all of these or just the Beacon table one?
src/wiphy.c static void wiphy_setup_rm_enabled_capabilities(struct wiphy *wiphy)
Quoted 4 lines
src/wiphy.c +0
-0/* * TODO: Support at least Link Measurement if TX_POWER_INSERTION is
Regards,
-Denis
Re: [PATCH 2/5] wiphy: add beacon bits to RM Enabled Capabilities
Quoted 16 lines · Denis Kenzior
Hi James,
Quoted 13 lines · James Prestwood
This tells AP's that we support Passive, Active, and Table beacon measurements.
--- src/wiphy.c | 2 ++ 1 file changed, 2 insertions(+)src/wiphy.c +0
-0@@ -961,6 +961,8 @@ static void wiphy_setup_rm_enabled_capabilities(struct wiphy *wiphy)
Quoted 8 lines
src/wiphy.c static void wiphy_setup_rm_enabled_capabilities(struct wiphy *wiphy) · line 965
Quoted 6 lines
src/wiphy.c +1
-0wiphy->rm_enabled_capabilities[0 ] =IE_TYPE_RM_ENABLED_CAPABILITIES ; wiphy->rm_enabled_capabilities[1 ] =5 ; +/* Bits: Passive (4), Active (5), and Beacon Table (6) capabilities */
src/wiphy.c +1
-0+ wiphy->rm_enabled_capabilities[2 ] =0x70 ;Do you really mean to add all of these or just the Beacon table one?
Yeah this was intended. This lets us support passive and active scan requests. Did you only want to allow table measurements?
Thanks,
James
Quoted 9 lines
Quoted 6 lines
/*
* TODO: Support at least Link Measurement ifTX_POWER_INSERTIONisRegards,
-Denis
Re: [PATCH 2/5] wiphy: add beacon bits to RM Enabled Capabilities
Hi James,
Quoted 9 lines
Quoted 5 lines
src/wiphy.c static void wiphy_setup_rm_enabled_capabilities(struct wiphy *wiphy) · line 965
Quoted 3 lines
(quoted diff) +1
-0+ /* Bits: Passive (4), Active (5), and Beacon Table (6)capabilities */
src/wiphy.c +1
-0+ wiphy->rm_enabled_capabilities[2 ] =0x70 ;Do you really mean to add all of these or just the Beacon table one?
Yeah this was intended. This lets us support passive and active scan requests. Did you only want to allow table measurements?
Ah no, this is fine. I now realize I mixed up Beacon Table and Beacon Request. So ignore this, the commit is fine. I'll take it in the next version.
Regards,
-Denis
PATCH 3/5 station: add APIs to get connected BSS and BSS list
In reply to James PrestwoodFor Radio Resource Management (RRM) we will need access to the currently connected BSS as well as the last scan results in order to do certain kinds of requested measurements.
--- src/station.c | 10 ++++++++++ src/station.h | 2 ++ 2 files changed, 12 insertions(+)
src/station.c +10 -0
@@ -3015,6 +3015,16 @@ void station_network_foreach(struct station *station,l_hashmap_foreach (station->networks, network_foreach, &data); } +struct l_queue *station_get_bss_list (struct station *station) +{ +return station->bss_list; +} + +struct scan_bss *station_get_connected_bss (struct station *station) +{ +return station->connected_bss; +} +static struct station *station_create (struct netdev *netdev) {struct station *station;
src/station.h +2 -0
@@ -85,3 +85,5 @@ void station_foreach(station_foreach_func_t func, void *user_data);void station_network_foreach (struct station *station,station_network_foreach_func_t func,void *user_data); +struct l_queue *station_get_bss_list (struct station *station); +struct scan_bss *station_get_connected_bss (struct station *station);
Signature · 1 line
2.17.1
Denis Kenzior
AppliedRe: [PATCH 3/5] station: add APIs to get connected BSS and BSS list
Hi James,
Quoted 8 lines · James Prestwood
For Radio Resource Management (RRM) we will need access to the currently connected BSS as well as the last scan results in order to do certain kinds of requested measurements.
--- src/station.c | 10 ++++++++++ src/station.h | 2 ++ 2 files changed, 12 insertions(+)
Applied, thanks.
Regards,
-Denis
PATCH 4/5 rrm: add radio resource management module
In reply to James PrestwoodThis module takes care of radio measurements which an AP can request. There are many types of requests, and for now only beacon requests are supported.
IWD will filter certain types of beacon requests that are NOT supported:
- AP channel reports. Only single channel requests will be supported
- Autonomous measurements. Only direct requests will be supported. IWD will not accept requets to trigger reports under certain conditions (SNR/RSSI thresholds, etc.)
- Timed measurements. Only immediate measurements will be performed. The accuracy for timed measurements cannot be reliably guaranteed due to kernel scheduling/queues.
- Full reporting detail. The AP can request the STA return the full set of IEs in a beacon. IWD does not currently save all IEs, plus there is quite a bit of complexity involved as certain IEs get truncated, and there are other length limitations.
There are other limitations not specific to beacon requests:
- IWD will support single measurement requests per report. Multiple measurement request IEs can be included, but the reports will be sent out separately.
- IWD will limit the number of requests it responds to in a given amount of time. As it stands now this is hard coded to 2 requests per second maximum. This will prevent DoS attacks.
- IWD will not accept any measurement requests from APs it is not connected to, and will not accept any requests until connected.
--- Makefile.am | 1 + src/rrm.c | 764 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 765 insertions(+) create mode 100644 src/rrm.c
Makefile.am +1 -0
@@ -226,6 +226,7 @@ src_iwd_SOURCES = src/main.c linux/nl80211.h src/iwd.h src/missing.h \ src/hotspot.c \ src/p2putil.h src/p2putil.c \ src/module.c \ + src/rrm.c \ $(eap_sources) \ $(builtin_sources)
src/rrm.c new file +764 -0
764 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [PATCH 4/5] rrm: add radio resource management module
Hi James,
Quoted 37 lines · James Prestwood
This module takes care of radio measurements which an AP can request. There are many types of requests, and for now only beacon requests are supported.
IWD will filter certain types of beacon requests that are NOT supported:
- AP channel reports. Only single channel requests will be supported
- Autonomous measurements. Only direct requests will be supported. IWD will not accept requets to trigger reports under certain conditions (SNR/RSSI thresholds, etc.)
- Timed measurements. Only immediate measurements will be performed. The accuracy for timed measurements cannot be reliably guaranteed due to kernel scheduling/queues.
- Full reporting detail. The AP can request the STA return the full set of IEs in a beacon. IWD does not currently save all IEs, plus there is quite a bit of complexity involved as certain IEs get truncated, and there are other length limitations.There are other limitations not specific to beacon requests:
- IWD will support single measurement requests per report. Multiple measurement request IEs can be included, but the reports will be sent out separately.
- IWD will limit the number of requests it responds to in a given amount of time. As it stands now this is hard coded to 2 requests per second maximum. This will prevent DoS attacks.
- IWD will not accept any measurement requests from APs it is not connected to, and will not accept any requests until connected.
--- Makefile.am | 1 + src/rrm.c | 764 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 765 insertions(+)create mode 100644
src/rrm.c
<snip>
src/rrm.c line 104
Quoted 10 lines
src/rrm.c new file +10
-0+/* +* Basically the same as 802.11-2016 9.4.2.21.7 +* +* Note: Not packed as this is only for saving values for response +*/ +struct rrm_beacon_req_info { +uint8_t oper_class; +uint8_t channel;/* The single channel provided in request */ +uint8_t bssid[6 ]; +char *ssid;
ssid is a fixed length field, so you can store it that way, e.g. char [33];
src/rrm.c line 117
Quoted 13 lines
src/rrm.c new file +13
-0+}; + +struct rrm_request_info { +uint32_t ifindex; +uint8_t from[6 ]; +uint8_t dialog_token;/* dialog token in Radio Measurement Request */ +uint8_t mtoken;/* token in measurement request element */ +uint8_t mode; +uint8_t type; + +/* TODO: once more measurements are supported this can be a union */ +struct rrm_beacon_req_info *beacon; +};
Why not use container_of and store rrm_request_info right in rrm_beacon_req_info.
src/rrm.c line 120
Quoted 3 lines
src/rrm.c new file +3
-0+ +static uint64_t last_request_us; +static const uint8_t wildcard_bss[6 ] = {0xff ,0xff ,0xff ,0xff ,0xff ,0xff };
This seems like a utility method candidate...
<snip>
Quoted 35 lines
src/rrm.c new file +35
-0+ +static void rrm_build_measurement_report (struct rrm_request_info *info, +const void *report,size_t report_len, +uint8_t *to) +{ + *to++ =IE_TYPE_MEASUREMENT_REPORT ; + *to++ =3 + report_len; + *to++ = info->mtoken; + *to++ =0 ; + *to++ = info->type; + +if (report) +memcpy (to, report, report_len); +} + +static void rrm_register_frame_cb (struct l_genl_msg *msg,void *user_data) +{ +if (l_genl_msg_get_error (msg) <0 ) +l_error ("Could not register frame watch type %04x: %i" , +L_PTR_TO_UINT (user_data),l_genl_msg_get_error (msg)); +} + +static void rrm_register_frame (uint32_t ifindex) +{ +struct l_genl_msg *msg; +uint16_t frame_type =0x00d0 ; +uint8_t prefix[] = {0x05 ,0x00 };/* Radio Measurment Request */ + + msg =l_genl_msg_new_sized (NL80211_CMD_REGISTER_FRAME ,34 ); + +l_genl_msg_append_attr (msg,NL80211_ATTR_IFINDEX ,4 , &ifindex); +l_genl_msg_append_attr (msg,NL80211_ATTR_FRAME_TYPE ,2 , &frame_type); +l_genl_msg_append_attr (msg,NL80211_ATTR_FRAME_MATCH , +sizeof (prefix), prefix); +
src/rrm.c line 255
Quoted 10 lines
src/rrm.c new file +10
-0+l_genl_family_send (nl80211, msg, rrm_register_frame_cb, +L_UINT_TO_PTR (frame_type),NULL ); +} + +static void rrm_netdev_watch (struct netdev *netdev, +enum netdev_watch_event event,void *user_data) +{ +switch (event) { +case NETDEV_WATCH_EVENT_NEW : +rrm_register_frame (netdev_get_ifindex (netdev));
Might want to check that we're in station mode here. You sort of do in the actual frame unicast watch callback, but not registering to it unless needed in the first place is even better.
Also, any reason for not using netdev_frame_watch_add?
Quoted 6 lines
src/rrm.c new file +6
-0+return ; +default : +break ; + } +} +
<snip>
Quoted 16 lines
src/rrm.c new file +16
-0+static bool rrm_handle_beacon_scan (struct rrm_request_info *info, +bool passive) +{ +struct netdev *netdev =netdev_find (info->ifindex); +struct scan_freq_set *freqs =scan_freq_set_new (); +struct scan_parameters params = { .freqs = freqs, .flush = true }; +enum scan_band band =scan_oper_class_to_band (NULL , + info->beacon->oper_class); +uint32_t freq; +uint32_t scan_id; + + freq =scan_channel_to_freq (info->beacon->channel, band); +scan_freq_set_add (freqs, freq); + +if (passive) + scan_id = scan_passive(netdev_get_wdev_id(netdev), freqs,
src/rrm.c line 449
Quoted 10 lines
src/rrm.c new file +10
-0+ rrm_scan_triggered, + rrm_scan_results, info, + rrm_info_destroy); + else + scan_id =scan_active_full (netdev_get_wdev_id (netdev), ¶ms, + rrm_scan_triggered, + rrm_scan_results, info, + rrm_info_destroy); + +scan_freq_set_free (freqs);
Should we be canceling the scan if station disconnects?
src/rrm.c line 453
Quoted 4 lines
src/rrm.c new file +4
-0+ +return scan_id !=0 ; +} +
<snip>
Regards,
-Denis
PATCH 5/5 auto-t: add RRM autotest
In reply to James PrestwoodThis test merely verifies hostapd receieved our measurement reports and verified they were valid. Hostapd does not verify the actual beacon report body. Really, the only way to test this is on an actual network which makes these requests.
--- autotests/testRRM/connection_test.py | 114 +++++++++++++++++++++++++++ autotests/testRRM/hw.conf | 8 ++ autotests/testRRM/ssidOther.conf | 10 +++ autotests/testRRM/ssidRRM.conf | 13 +++ 4 files changed, 145 insertions(+) create mode 100644 autotests/testRRM/connection_test.py create mode 100644 autotests/testRRM/hw.conf create mode 100644 autotests/testRRM/ssidOther.conf create mode 100644 autotests/testRRM/ssidRRM.conf
autotests/testRRM/connection_test.py new file +114 -0
@@ -0,0 +1,114 @@ +#!/usr/bin/python3 + +import unittest +import sys + +sys.path.append('../util') +import iwd +from iwd import IWD +from iwd import PSKAgent +from iwd import NetworkType +from hostapd import HostapdCLI +import testutil + +from time import sleep + +# Table beacon with wildcard BSSID +basic_beacon = '51000000000002ffffffffffff020100' +# Table beacon with wildcard BSSID and SSID filter +beacon_with_ssid = '51000000000002ffffffffffff02010000077373696452524d' +# Passive beacon with wildcard BSSID +beacon_passive = '510b0000000000ffffffffffff020100' +# Active beacon with wildcard BSSID +beacon_active = '510b0000000001ffffffffffff020100' + +class Test(unittest.TestCase): + + def test_connection_success(self): + hapd = HostapdCLI(config='ssidRRM.conf') + wd = IWD() + + psk_agent = PSKAgent("secret123") + wd.register_psk_agent(psk_agent) + + devices = wd.list_devices(1) + device = devices[0] + + condition = 'not obj.scanning' + wd.wait_for_object_condition(device, condition) + + device.scan() + + condition = 'not obj.scanning' + wd.wait_for_object_condition(device, condition) + + ordered_network = device.get_ordered_network('ssidRRM') + + self.assertEqual(ordered_network.type, NetworkType.psk) + + condition = 'not obj.connected' + wd.wait_for_object_condition(ordered_network.network_object, condition) + + ordered_network.network_object.connect() + + condition = 'obj.connected' + wd.wait_for_object_condition(ordered_network.network_object, condition) + + testutil.test_iface_operstate() + testutil.test_ifaces_connected() + + hapd.wait_for_event('AP-STA-CONNECTED') + + # This should return both APs + hapd.req_beacon(device.address, basic_beacon) + + for e in ['BEACON-RESP-RX', 'BEACON-RESP-RX']: + event = hapd.wait_for_event(e) + if event: + print(event) + + sleep(0.5) + + # This should return just ssidRRM + hapd.req_beacon(device.address, beacon_with_ssid) + event = hapd.wait_for_event('BEACON-RESP-RX') + if event: + print(event) + + sleep(0.5) + + # This should passive scan on channel 11, returning otherSSID + hapd.req_beacon(device.address, beacon_passive) + # TODO: See if we are scanning here (scan not initiated from station) + + event = hapd.wait_for_event('BEACON-RESP-RX') + if event: + print(event) + + sleep(0.5) + + # This should active scan on channel 11, returning otherSSID + hapd.req_beacon(device.address, beacon_active) + # TODO: See if we are scanning here (scan not initiated from station) + + event = hapd.wait_for_event('BEACON-RESP-RX') + if event: + print(event) + + 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): + pass + + @classmethod + def tearDownClass(cls): + IWD.clear_storage() + +if __name__ == '__main__': + unittest.main(exit=True)
autotests/testRRM/hw.conf new file +8 -0
@@ -0,0 +1,8 @@ +[SETUP] +num_radios=3 +max_test_exec_interval_sec=40 +reg_domain=US + +[HOSTAPD] +rad0=ssidRRM.conf +rad1=ssidOther.conf
autotests/testRRM/ssidOther.conf new file +10 -0
@@ -0,0 +1,10 @@ +ctrl_interface=/var/run/hostapd + +hw_mode=g +channel=11 +ssid=ssidOther + +wpa=2 +wpa_pairwise=CCMP +wpa_passphrase=secret123 +rrm_beacon_report=1
autotests/testRRM/ssidRRM.conf new file +13 -0
@@ -0,0 +1,13 @@ +ctrl_interface=/var/run/hostapd + +hw_mode=a +ssid=ssidRRM + +wpa=2 +wpa_pairwise=CCMP +wpa_passphrase=secret123 +rrm_beacon_report=1 +ht_capab=[HT40+] + +ieee80211n=1 +channel=36
Signature · 1 line
2.17.1
Denis Kenzior
AppliedHi James,
Quoted 14 lines · James Prestwood
Hostapd has a feature where you can connect to its control socket and receive events it generates. Currently we only send commands via this socket.
First we open the socket (/var/run/hostapd/<iface>) and send the ATTACH command. This tells hostapd we are ready and after this any events will be sent over this socket.
A new API,
wait_for_event, was added which takes an event string and waits for some timeout. The glib event loop has been integrated into this, though its not technically async since we are selecting over a socket which blocks. To mitigate this a small timeout was chosen for each select call and then wrapped in a while loop which waits for the full timeout.
Quoted 4 lines
--- autotests/util/hostapd.py | 68 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+)
Applied, thanks.
Regards,
-Denis