eiwd

no dbus iwd with iwctl functionality
git clone https://git.daat.foo/repos/eiwd.git
Log | Files | Refs | Submodules | README | LICENSE

commit 4c47246f524c7a8d567bdc00af76a1d5347035c6
parent ce496692dd0e3d20760d5f4b404767b6c45b42a0
Author: illiliti <illiliti@protonmail.com>
Date:   Sat,  6 Nov 2021 19:13:12 +0300

Merge tag '1.19'

Release 1.19

Diffstat:
MChangeLog | 9++++++++-
MMakefile.am | 11+++++++++--
Mautotests/testBSSBlacklist/connection_test.py | 3+++
Aautotests/testChannelSwitch/connection_test.py | 69+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aautotests/testChannelSwitch/hw.conf | 5+++++
Aautotests/testChannelSwitch/ssidCCMP.conf | 8++++++++
Mautotests/testConnectAutoconnect/open_network_test.py | 4++++
Aautotests/testConnectAutoconnect/ssidOpen.open | 0
Mautotests/testFILS/fils_256_test.py | 2++
Mautotests/testFILS/fils_384_test.py | 2++
Mautotests/testFILS/ssidFILS-256.conf | 1+
Mautotests/testFILS/ssidFILS-384.conf | 1+
Mautotests/testFT-FILS/connection_test.py | 2++
Mautotests/testHiddenNetworks/open_network_test.py | 2++
Mautotests/testHiddenNetworks/wpa_network_test.py | 2++
Aautotests/testOWE-transition/connection_test.py | 248+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aautotests/testOWE-transition/hw.conf | 10++++++++++
Aautotests/testOWE-transition/main.conf | 2++
Aautotests/testOWE-transition/ssidOWE.conf | 14++++++++++++++
Aautotests/testOWE-transition/ssidOpen.conf | 8++++++++
Aautotests/testOWE-transition/transition.open | 0
Mautotests/testPSK-roam/connection_test.py | 21+++++++++++++++++++++
Mautotests/testSAE/connection_test.py | 27++++++++++++---------------
Mautotests/testSAQuery-spoofing/connection_test.py | 6++++--
Mautotests/testSAQuery-spoofing/ssidCCMP.conf | 1+
Aautotests/testWPA2-ext-key-id/connection_test.py | 112+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aautotests/testWPA2-ext-key-id/hw.conf | 6++++++
Aautotests/testWPA2-ext-key-id/main_pae.conf | 2++
Aautotests/testWPA2-ext-key-id/ssidCCMP.conf | 11+++++++++++
Mautotests/util/hostapd.py | 25+++++++++++++++++++++++++
Mautotests/util/iwd.py | 3++-
Aclient/daemon.c | 141+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aclient/daemon.h | 23+++++++++++++++++++++++
Mclient/dbus-proxy.h | 1+
Mclient/station.c | 66++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mconfigure.ac | 5+++--
Mdoc/agent-api.txt | 20++++++++++++--------
Mdoc/hostapd.config | 1+
Adoc/ip-configuration-api.txt | 47+++++++++++++++++++++++++++++++++++++++++++++++
Mdoc/p2p-peer-api.txt | 5+++++
Mmonitor/nlmon.c | 266++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Msrc/ap.c | 49+++++++++++++++++++++----------------------------
Msrc/auth-proto.h | 9+++++++++
Msrc/band.c | 458+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/band.h | 21+++++++++++++++++++++
Msrc/eapol.c | 238+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------
Msrc/eapol.h | 2++
Msrc/fils.c | 35+++++++++++++++++++++++++++++++++--
Msrc/fils.h | 2++
Msrc/ft.c | 62++++++++++++++++++++++++++++++++++++++++++++++++++++----------
Msrc/ft.h | 5++++-
Msrc/handshake.c | 247++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
Msrc/handshake.h | 30+++++++++++++++++++++++++++---
Msrc/ie.c | 80+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/ie.h | 18++++++++++++++++++
Msrc/ip-pool.c | 11++---------
Msrc/iwd.config.rst | 7+++++++
Msrc/main.c | 34++++++++++++----------------------
Msrc/manager.c | 68++++++++++++++++++++++++++------------------------------------------
Msrc/missing.h | 7+++++++
Msrc/netconfig.c | 564++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------
Msrc/netconfig.h | 5+++--
Msrc/netdev.c | 881+++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------
Msrc/network.c | 84++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------
Msrc/network.h | 3++-
Msrc/nl80211cmd.c | 3++-
Msrc/nl80211util.c | 44+++++++++++++++++++++++++++++++++++---------
Msrc/nl80211util.h | 4++++
Msrc/p2p.c | 24+++++++++++++++++++-----
Msrc/resolve.c | 12++----------
Msrc/rrm.c | 21++++++++++++++++++---
Msrc/scan.c | 177+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
Msrc/scan.h | 8+++++++-
Msrc/station.c | 575++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------
Msrc/station.h | 14++++++++------
Msrc/util.h | 2+-
Msrc/wiphy.c | 82+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/wiphy.h | 4++++
Mtest/force-roam | 27+++++++++++++++++++++++----
Mtools/test-runner | 44+++++++++++++++++++++++++++++++++++++++-----
Munit/test-band.c | 118+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Munit/test-eapol.c | 15++++++++-------
Munit/test-wsc.c | 2+-
83 files changed, 4349 insertions(+), 939 deletions(-)

diff --git a/ChangeLog b/ChangeLog @@ -1,8 +1,15 @@ +ver 1.19: + Fix issue with handling OCV if offloading is supported. + Fix issue with handling SA Query on channel switch event. + Fix issue with starting FT-over-DS actions after roaming. + Add support for OWE transition networks. + Add support for extended key IDs. + ver 1.18: + Fix issue with handling faulty SAE duplicate commits. Add support for Access Point FILS IP Address Assignment IE. Add support for P2P GO-side 4-way handshake IP allocation. Add support for forcing SAE group 19 if BSS requires it. - Fix issue with handling faulty SAE duplicate commits. ver 1.17: Fix issue with sending additional and vendor IEs. diff --git a/Makefile.am b/Makefile.am @@ -61,7 +61,8 @@ ell_headers = ell/util.h \ ell/path.h \ ell/icmp6.h \ ell/dhcp6.h \ - ell/acd.h + ell/acd.h \ + ell/cleanup.h ell_sources = ell/private.h \ ell/missing.h \ @@ -293,7 +294,9 @@ client_iwctl_SOURCES = client/main.c \ client/network.h client/network.c \ client/properties.h client/properties.c \ client/wsc.c client/station.c \ - client/diagnostic.c client/diagnostic.h + client/diagnostic.c client/diagnostic.h \ + client/daemon.c client/daemon.h + client_iwctl_LDADD = $(ell_ldadd) $(READLINE_LIBS) if MANUAL_PAGES @@ -408,6 +411,7 @@ unit_test_eap_sim_SOURCES = unit/test-eap-sim.c \ src/util.h src/util.c \ src/simauth.h src/simauth.c \ src/erp.h src/erp.c \ + src/band.h src/band.c \ src/eap-sim.c unit_test_eap_sim_LDADD = $(ell_ldadd) @@ -468,6 +472,7 @@ unit_test_eapol_SOURCES = unit/test-eapol.c \ src/eap-md5.c src/util.c \ src/eap-tls-common.h src/eap-tls-common.c \ src/erp.h src/erp.c \ + src/band.h src/band.c \ src/mschaputil.h src/mschaputil.c unit_test_eapol_LDADD = $(ell_ldadd) unit_test_eapol_DEPENDENCIES = $(ell_dependencies) \ @@ -495,6 +500,7 @@ unit_test_wsc_SOURCES = unit/test-wsc.c src/wscutil.h src/wscutil.c \ src/eap.h src/eap.c src/eap-private.h \ src/util.h src/util.c \ src/erp.h src/erp.c \ + src/band.h src/band.c \ src/eap-wsc.h src/eap-wsc.c unit_test_wsc_LDADD = $(ell_ldadd) @@ -523,6 +529,7 @@ unit_test_sae_SOURCES = unit/test-sae.c \ src/ie.h src/ie.c \ src/handshake.h src/handshake.c \ src/erp.h src/erp.c \ + src/band.h src/band.c \ src/util.h src/util.c \ src/mpdu.h src/mpdu.c unit_test_sae_LDADD = $(ell_ldadd) diff --git a/autotests/testBSSBlacklist/connection_test.py b/autotests/testBSSBlacklist/connection_test.py @@ -110,8 +110,11 @@ class Test(unittest.TestCase): # Have both APs drop all packets, both should get blacklisted rule0.drop = True + rule0.enable = True rule1.drop = True + rule1.enable = True rule2.drop = True + rule2.enable = True with self.assertRaises(iwd.FailedEx): ordered_network.network_object.connect() diff --git a/autotests/testChannelSwitch/connection_test.py b/autotests/testChannelSwitch/connection_test.py @@ -0,0 +1,69 @@ +#!/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 + +class Test(unittest.TestCase): + + def test_connection_success(self): + hapd = HostapdCLI() + wd = IWD() + + psk_agent = PSKAgent("secret123") + wd.register_psk_agent(psk_agent) + + devices = wd.list_devices(1) + device = devices[0] + + ordered_network = device.get_ordered_network('ssidCCMP') + + 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.state == DeviceState.connected' + wd.wait_for_object_condition(device, condition) + + testutil.test_iface_operstate() + testutil.test_ifaces_connected() + + hapd.chan_switch(2) + # Make sure the SA Query is verified and IWD doesn't disconnect + with self.assertRaises(TimeoutError): + condition = 'obj.state == DeviceState.disconnected' + wd.wait_for_object_condition(device, condition, 4) + + condition = 'obj.state == DeviceState.connected' + wd.wait_for_object_condition(device, condition) + + testutil.test_iface_operstate() + testutil.test_ifaces_connected() + + 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) diff --git a/autotests/testChannelSwitch/hw.conf b/autotests/testChannelSwitch/hw.conf @@ -0,0 +1,5 @@ +[SETUP] +num_radios=2 + +[HOSTAPD] +rad0=ssidCCMP.conf diff --git a/autotests/testChannelSwitch/ssidCCMP.conf b/autotests/testChannelSwitch/ssidCCMP.conf @@ -0,0 +1,8 @@ +hw_mode=g +channel=1 +ssid=ssidCCMP + +wpa=2 +wpa_pairwise=CCMP +wpa_passphrase=secret123 +ocv=1 diff --git a/autotests/testConnectAutoconnect/open_network_test.py b/autotests/testConnectAutoconnect/open_network_test.py @@ -24,6 +24,10 @@ class TestOpenNetwork(unittest.TestCase): tca.validate('ssidOpen', False) tca.validate('ssidOpen', True) + def setUp(self): + IWD.copy_to_storage('ssidOpen.open') + + def tearDown(self): IWD.clear_storage() if __name__ == '__main__': diff --git a/autotests/testConnectAutoconnect/ssidOpen.open b/autotests/testConnectAutoconnect/ssidOpen.open diff --git a/autotests/testFILS/fils_256_test.py b/autotests/testFILS/fils_256_test.py @@ -55,6 +55,8 @@ class Test(unittest.TestCase): testutil.test_iface_operstate() testutil.test_ifaces_connected(device.name, hapd.ifname) + hapd.rekey(device.address) + device.disconnect() @classmethod diff --git a/autotests/testFILS/fils_384_test.py b/autotests/testFILS/fils_384_test.py @@ -55,6 +55,8 @@ class Test(unittest.TestCase): testutil.test_iface_operstate() testutil.test_ifaces_connected(device.name, hapd.ifname) + hapd.rekey(device.address) + device.disconnect() @classmethod diff --git a/autotests/testFILS/ssidFILS-256.conf b/autotests/testFILS/ssidFILS-256.conf @@ -19,3 +19,4 @@ disable_pmksa_caching=1 wpa_group_rekey=30 wpa_ptk_rekey=30 +ocv=1 diff --git a/autotests/testFILS/ssidFILS-384.conf b/autotests/testFILS/ssidFILS-384.conf @@ -19,3 +19,4 @@ disable_pmksa_caching=1 wpa_group_rekey=30 wpa_ptk_rekey=30 +ocv=1 diff --git a/autotests/testFT-FILS/connection_test.py b/autotests/testFT-FILS/connection_test.py @@ -95,6 +95,8 @@ class Test(unittest.TestCase): self.assertRaises(Exception, testutil.test_ifaces_connected, (self.bss_hostapd[0].ifname, device.name, True, True)) + self.bss_hostapd[1].rekey(device.address) + def test_fils_ft_roam_sha256(self): wd = IWD(True) diff --git a/autotests/testHiddenNetworks/open_network_test.py b/autotests/testHiddenNetworks/open_network_test.py @@ -24,6 +24,8 @@ class TestOpenNetwork(unittest.TestCase): tca.validate('ssidHiddenOpen', False) tca.validate('ssidHiddenOpen', True) + @classmethod + def tearDownClass(cls): IWD.clear_storage() if __name__ == '__main__': diff --git a/autotests/testHiddenNetworks/wpa_network_test.py b/autotests/testHiddenNetworks/wpa_network_test.py @@ -25,6 +25,8 @@ class TestWpaNetwork(unittest.TestCase): tca.validate('ssidHiddenWPA', False, None, True) tca.validate('ssidHiddenWPA', True, None, True) + @classmethod + def tearDownClass(cls): IWD.clear_storage() if __name__ == '__main__': diff --git a/autotests/testOWE-transition/connection_test.py b/autotests/testOWE-transition/connection_test.py @@ -0,0 +1,248 @@ +#!/usr/bin/python3 + +import unittest +import sys + +sys.path.append('../util') +import iwd +from iwd import IWD +from iwd import NetworkType +from hostapd import HostapdCLI +import testutil + +class Test(unittest.TestCase): + + def validate(self, wd, target, autoconnect=False, connect_hidden=False): + devices = wd.list_devices(1) + device = devices[0] + device.autoconnect = autoconnect + + if not autoconnect: + if not connect_hidden: + condition = 'not obj.scanning' + wd.wait_for_object_condition(device, condition) + + device.scan() + + condition = 'obj.scanning' + wd.wait_for_object_condition(device, condition) + + condition = 'not obj.scanning' + wd.wait_for_object_condition(device, condition) + + network = device.get_ordered_network('transition', scan_if_needed=False) + + network.network_object.connect() + else: + device.connect_hidden_network('owe-hidden') + + condition = 'obj.state == DeviceState.connected' + wd.wait_for_object_condition(device, condition) + + testutil.test_iface_operstate() + + if type(target) != list: + testutil.test_ifaces_connected(device.name, target.ifname) + self.assertIn(device.address, target.list_sta()) + else: + found = False + for t in target: + if device.address in t.list_sta(): + testutil.test_ifaces_connected(device.name, t.ifname) + found = True + + self.assertTrue(found) + + device.disconnect() + + # Normal success case + def test_owe_transition(self): + self.hapd_open.set_value('vendor_elements', 'dd15506f9a1c02000000f1000a6f77652d68696464656e') + self.hapd_open.reload() + self.hapd_owe.set_value('vendor_elements', 'dd15506f9a1c02000000f0000a7472616e736974696f6e') + self.hapd_owe.reload() + + self.hapd_owe2.disable() + self.hapd_open2.disable() + + self.validate(self.wd, self.hapd_owe) + + # Normal success case + def test_owe_transition_multi_network(self): + self.hapd_open.set_value('vendor_elements', 'dd15506f9a1c02000000f1000a6f77652d68696464656e') + self.hapd_open.reload() + self.hapd_owe.set_value('vendor_elements', 'dd15506f9a1c02000000f0000a7472616e736974696f6e') + self.hapd_owe.reload() + + self.hapd_open2.set_value('vendor_elements', 'dd17506f9a1c02000000f1000c6f77652d68696464656e2d32') + self.hapd_open2.set_value('ssid', 'transition-2') + self.hapd_open2.reload() + self.hapd_owe2.set_value('vendor_elements', 'dd17506f9a1c02000000f0000c7472616e736974696f6e2d32') + self.hapd_owe2.set_value('ssid', 'owe-hidden-2') + self.hapd_owe2.reload() + + self.validate(self.wd, self.hapd_owe) + + # Two pairs of open/OWE BSS's (OWE BSS's have different SSIDs) */ + def test_owe_transition_multi_bss(self): + self.hapd_open.set_value('vendor_elements', 'dd15506f9a1c02000000f1000a6f77652d68696464656e') + self.hapd_open.reload() + self.hapd_owe.set_value('vendor_elements', 'dd15506f9a1c02000000f0000a7472616e736974696f6e') + self.hapd_owe.reload() + + self.hapd_open2.set_value('vendor_elements', 'dd17506f9a1c02000000f3000c6f77652d68696464656e2d32') + self.hapd_open2.set_value('ssid', 'transition') + self.hapd_open2.reload() + self.hapd_owe2.set_value('vendor_elements', 'dd15506f9a1c02000000f2000a7472616e736974696f6e') + self.hapd_owe2.set_value('ssid', 'owe-hidden-2') + self.hapd_owe2.reload() + + self.validate(self.wd, [self.hapd_owe, self.hapd_owe2]) + + # Two pairs of open/OWE BSS's (OWE BSS's have same SSID) */ + def test_owe_transition_multi_bss_same_ssid(self): + self.hapd_open.set_value('vendor_elements', 'dd15506f9a1c02000000f1000a6f77652d68696464656e') + self.hapd_open.reload() + self.hapd_owe.set_value('vendor_elements', 'dd15506f9a1c02000000f0000a7472616e736974696f6e') + self.hapd_owe.reload() + + self.hapd_open2.set_value('vendor_elements', 'dd15506f9a1c02000000f3000a6f77652d68696464656e') + self.hapd_open2.set_value('ssid', 'transition') + self.hapd_open2.reload() + self.hapd_owe2.set_value('vendor_elements', 'dd15506f9a1c02000000f2000a7472616e736974696f6e') + self.hapd_owe2.set_value('ssid', 'owe-hidden') + self.hapd_owe2.reload() + + self.validate(self.wd, [self.hapd_owe, self.hapd_owe2]) + + # Normal success autoconnect case + def test_owe_transition_autoconnect(self): + self.hapd_open.set_value('vendor_elements', 'dd15506f9a1c02000000f1000a6f77652d68696464656e') + self.hapd_open.reload() + self.hapd_owe.set_value('vendor_elements', 'dd15506f9a1c02000000f0000a7472616e736974696f6e') + self.hapd_owe.reload() + + self.hapd_owe2.disable() + self.hapd_open2.disable() + + IWD.copy_to_storage('transition.open') + + self.validate(self.wd, self.hapd_owe, autoconnect=True) + + # Open BSS has invalid BSSID in OWE transition element + # Expected connection to Open BSS + def test_owe_transition_invalid_open_bssid(self): + self.hapd_open.set_value('vendor_elements', 'dd15506f9a1c02000000ff000a6f77652d68696464656e') + self.hapd_open.reload() + self.hapd_owe.set_value('vendor_elements', 'dd15506f9a1c02000000f0000a7472616e736974696f6e') + self.hapd_owe.reload() + + self.hapd_owe2.disable() + self.hapd_open2.disable() + + self.validate(self.wd, self.hapd_open) + + # OWE BSS has invalid BSSID in OWE transition element + # Expected connection to Open BSS + def test_owe_transition_invalid_owe_bssid(self): + self.hapd_open.set_value('vendor_elements', 'dd15506f9a1c02000000f1000a6f77652d68696464656e') + self.hapd_open.reload() + self.hapd_owe.set_value('vendor_elements', 'dd15506f9a1c02000000ff000a7472616e736974696f6e') + self.hapd_owe.reload() + + self.hapd_owe2.disable() + self.hapd_open2.disable() + + self.validate(self.wd, self.hapd_open) + + # No OWE hidden network exists + # Expected connection to Open BSS + def test_owe_transition_no_hidden_found(self): + self.hapd_open.set_value('vendor_elements', 'dd15506f9a1c02000000f1000a6f77652d68696464656e') + self.hapd_open.reload() + self.hapd_owe.disable() + + self.hapd_owe2.disable() + self.hapd_open2.disable() + + self.validate(self.wd, self.hapd_open) + + # Directly connect to valid OWE hidden network + # Expected connection failure + def test_owe_transition_connect_hidden_valid(self): + self.hapd_open.set_value('vendor_elements', 'dd15506f9a1c02000000f1000a6f77652d68696464656e') + self.hapd_open.reload() + self.hapd_owe.set_value('vendor_elements', 'dd15506f9a1c02000000f0000a7472616e736974696f6e') + self.hapd_owe.reload() + + self.hapd_owe2.disable() + self.hapd_open2.disable() + + with self.assertRaises(iwd.NotFoundEx): + self.validate(self.wd, self.hapd_owe, connect_hidden=True) + + # Directly connect to invalid OWE hidden network. The SSID in the OWE BSS + # IE points to itself. And the Open BSS IE points to a non-existent SSID. + # Expected connection failure + def test_owe_transition_connect_hidden_invalid(self): + self.hapd_open.set_value('vendor_elements', 'dd18506f9a1c02000000f1000d6e6f2d6f77652d68696464656e') + self.hapd_open.reload() + self.hapd_owe.set_value('vendor_elements', 'dd15506f9a1c02000000f0000a6f77652d68696464656e') + self.hapd_owe.reload() + + self.hapd_owe2.disable() + self.hapd_open2.disable() + + with self.assertRaises(iwd.NotFoundEx): + self.validate(self.wd, self.hapd_owe, connect_hidden=True) + + def test_owe_transition_band_info(self): + self.hapd_open.set_value('vendor_elements', 'dd17506f9a1c02000000f1000a6f77652d68696464656e5103') + self.hapd_open.reload() + self.hapd_owe.set_value('vendor_elements', 'dd15506f9a1c02000000f0000a7472616e736974696f6e') + self.hapd_owe.set_value('channel', '3') + self.hapd_owe.reload() + + self.hapd_owe2.disable() + self.hapd_open2.disable() + + self.validate(self.wd, self.hapd_owe) + + def test_owe_transition_wrong_band_info(self): + self.hapd_open.set_value('vendor_elements', 'dd17506f9a1c02000000f1000a6f77652d68696464656e5102') + self.hapd_open.reload() + self.hapd_owe.set_value('vendor_elements', 'dd15506f9a1c02000000f0000a7472616e736974696f6e') + self.hapd_owe.set_value('channel', '3') + self.hapd_owe.reload() + + self.hapd_owe2.disable() + self.hapd_open2.disable() + + self.validate(self.wd, self.hapd_open) + + def setUp(self): + self.wd = IWD(True) + self.hapd_owe = HostapdCLI(config='ssidOWE.conf') + self.hapd_open = HostapdCLI(config='ssidOpen.conf') + self.hapd_owe2 = HostapdCLI(config='ssidOWE-2.conf') + self.hapd_open2 = HostapdCLI(config='ssidOpen-2.conf') + + def tearDown(self): + IWD.clear_storage() + + self.hapd_owe.set_value('channel', '1') + + self.wd = None + self.hapd_open = None + self.hapd_owe = None + + @classmethod + def setUpClass(cls): + pass + + @classmethod + def tearDownClass(cls): + IWD.clear_storage() + +if __name__ == '__main__': + unittest.main(exit=True) diff --git a/autotests/testOWE-transition/hw.conf b/autotests/testOWE-transition/hw.conf @@ -0,0 +1,10 @@ +[SETUP] +num_radios=5 +hwsim_medium=yes +start_iwd=0 + +[HOSTAPD] +rad0=ssidOpen.conf +rad1=ssidOWE.conf +rad2=ssidOpen-2.conf +rad3=ssidOWE-2.conf diff --git a/autotests/testOWE-transition/main.conf b/autotests/testOWE-transition/main.conf @@ -0,0 +1,2 @@ +[Scan] +DisableMacAddressRandomization=true diff --git a/autotests/testOWE-transition/ssidOWE.conf b/autotests/testOWE-transition/ssidOWE.conf @@ -0,0 +1,14 @@ +ssid=owe-hidden +bssid=02:00:00:00:f1:00 +channel=1 +ignore_broadcast_ssid=1 +ieee80211w=1 + +wpa=2 +wpa_key_mgmt=OWE +rsn_pairwise=CCMP + +# You would conventionally use these options but hostapd does not include an +# IE for the OWE network, hence vendor_elements must be used directly +#owe_transition_ssid="transition" +#owe_transition_bssid=02:00:00:00:f0:00 diff --git a/autotests/testOWE-transition/ssidOpen.conf b/autotests/testOWE-transition/ssidOpen.conf @@ -0,0 +1,8 @@ +channel=1 +ssid=transition +bssid=02:00:00:00:f0:00 + +# You would conventionally use these options but hostapd does not include an +# IE for the OWE network, hence vendor_elements must be used directly +#owe_transition_ssid="owe-hidden" +#owe_transition_bssid=02:00:00:00:f1:00 diff --git a/autotests/testOWE-transition/transition.open b/autotests/testOWE-transition/transition.open diff --git a/autotests/testPSK-roam/connection_test.py b/autotests/testPSK-roam/connection_test.py @@ -59,6 +59,9 @@ class Test(unittest.TestCase): self.assertRaises(Exception, testutil.test_ifaces_connected, (self.bss_hostapd[1].ifname, device.name, True, True)) + if over_ds: + self.rule0.enabled = True + device.roam(self.bss_hostapd[1].bssid) condition = 'obj.state == DeviceState.roaming' @@ -77,6 +80,8 @@ class Test(unittest.TestCase): self.assertRaises(Exception, testutil.test_ifaces_connected, (self.bss_hostapd[0].ifname, device.name, True, True)) + self.bss_hostapd[1].rekey(device.address) + device.roam(self.bss_hostapd[0].bssid) condition = 'obj.state == DeviceState.roaming' @@ -100,11 +105,13 @@ class Test(unittest.TestCase): self.bss_hostapd[0].set_value('wpa_key_mgmt', 'FT-PSK') self.bss_hostapd[0].set_value('ft_over_ds', '0') + self.bss_hostapd[0].set_value('ocv', '1') self.bss_hostapd[0].reload() self.bss_hostapd[0].wait_for_event("AP-ENABLED") self.bss_hostapd[1].set_value('wpa_key_mgmt', 'FT-PSK') self.bss_hostapd[1].set_value('ft_over_ds', '0') + self.bss_hostapd[0].set_value('ocv', '1') self.bss_hostapd[1].reload() self.bss_hostapd[1].wait_for_event("AP-ENABLED") @@ -115,11 +122,13 @@ class Test(unittest.TestCase): self.bss_hostapd[0].set_value('wpa_key_mgmt', 'FT-PSK') self.bss_hostapd[0].set_value('ft_over_ds', '1') + self.bss_hostapd[0].set_value('ocv', '1') self.bss_hostapd[0].reload() self.bss_hostapd[0].wait_for_event("AP-ENABLED") self.bss_hostapd[1].set_value('wpa_key_mgmt', 'FT-PSK') self.bss_hostapd[1].set_value('ft_over_ds', '1') + self.bss_hostapd[1].set_value('ocv', '1') self.bss_hostapd[1].reload() self.bss_hostapd[1].wait_for_event("AP-ENABLED") @@ -146,12 +155,24 @@ class Test(unittest.TestCase): os.system('ifconfig "' + self.bss_hostapd[0].ifname + '" up') os.system('ifconfig "' + self.bss_hostapd[1].ifname + '" up') + self.rule0.enabled = False + @classmethod def setUpClass(cls): + hwsim = Hwsim() + IWD.copy_to_storage('TestFT.psk') cls.bss_hostapd = [ HostapdCLI(config='ft-psk-ccmp-1.conf'), HostapdCLI(config='ft-psk-ccmp-2.conf') ] + rad2 = hwsim.get_radio('rad2') + + cls.rule0 = hwsim.rules.create() + cls.rule0.source = rad2.addresses[0] + cls.rule0.bidirectional = True + cls.rule0.signal = -2000 + cls.rule0.prefix = 'b0' + cls.rule0.drop = True # Set interface addresses to those expected by hostapd config files os.system('ifconfig "' + cls.bss_hostapd[0].ifname + diff --git a/autotests/testSAE/connection_test.py b/autotests/testSAE/connection_test.py @@ -62,9 +62,7 @@ class Test(unittest.TestCase): self.hostapd.set_value('vendor_elements', '') self.hostapd.reload() self.hostapd.wait_for_event("AP-ENABLED") - wd = IWD(True) - self.validate_connection(wd) - wd.clear_storage() + self.validate_connection(self.wd) def test_SAE_force_group_19(self): self.hostapd.set_value('sae_pwe', '0') @@ -74,9 +72,7 @@ class Test(unittest.TestCase): self.hostapd.set_value('vendor_elements', 'dd0cf4f5e8050500000000000000') self.hostapd.reload() self.hostapd.wait_for_event("AP-ENABLED") - wd = IWD(True) - self.validate_connection(wd) - wd.clear_storage() + self.validate_connection(self.wd) def test_SAE_Group20(self): self.hostapd.set_value('sae_pwe', '0') @@ -84,9 +80,7 @@ class Test(unittest.TestCase): self.hostapd.set_value('vendor_elements', '') self.hostapd.reload() self.hostapd.wait_for_event("AP-ENABLED") - wd = IWD(True) - self.validate_connection(wd) - wd.clear_storage() + self.validate_connection(self.wd) def test_SAE_H2E(self): self.hostapd.set_value('sae_pwe', '1') @@ -94,9 +88,7 @@ class Test(unittest.TestCase): self.hostapd.set_value('vendor_elements', '') self.hostapd.reload() self.hostapd.wait_for_event("AP-ENABLED") - wd = IWD(True) - self.validate_connection(wd) - wd.clear_storage() + self.validate_connection(self.wd) def test_SAE_H2E_Group20(self): self.hostapd.set_value('sae_pwe', '1') @@ -104,9 +96,14 @@ class Test(unittest.TestCase): self.hostapd.set_value('vendor_elements', '') self.hostapd.reload() self.hostapd.wait_for_event("AP-ENABLED") - wd = IWD(True) - self.validate_connection(wd) - wd.clear_storage() + self.validate_connection(self.wd) + + def setUp(self): + self.wd = IWD(True) + + def tearDown(self): + self.wd.clear_storage() + self.wd = None @classmethod def setUpClass(cls): diff --git a/autotests/testSAQuery-spoofing/connection_test.py b/autotests/testSAQuery-spoofing/connection_test.py @@ -51,8 +51,10 @@ class Test(unittest.TestCase): # Spoof a disassociate frame. This will kick off SA Query procedure. hwsim.spoof_disassociate(radio, hostapd.frequency, device.address) - # sleep to ensure hostapd responds and SA Query does not timeout - sleep(4) + # ensure hostapd responds and SA Query does not timeout + with self.assertRaises(TimeoutError): + condition = 'obj.state == DeviceState.disconnected' + wd.wait_for_object_condition(device, condition, 4) # Since disassociate was spoofed we should still be connected condition = 'obj.state == DeviceState.connected' diff --git a/autotests/testSAQuery-spoofing/ssidCCMP.conf b/autotests/testSAQuery-spoofing/ssidCCMP.conf @@ -9,3 +9,4 @@ wpa_passphrase=secret123 ieee80211w=2 wpa_key_mgmt=WPA-PSK-SHA256 beacon_int=10 +ocv=1 diff --git a/autotests/testWPA2-ext-key-id/connection_test.py b/autotests/testWPA2-ext-key-id/connection_test.py @@ -0,0 +1,112 @@ +#!/usr/bin/python3 + +import unittest +import sys +import os + +sys.path.append('../util') +from iwd import IWD_CONFIG_DIR +from iwd import IWD +from iwd import PSKAgent +from iwd import NetworkType +from hwsim import Hwsim +from hostapd import HostapdCLI +import testutil + +class Test(unittest.TestCase): + def validate(self, wd, resend_m3=False): + hapd = HostapdCLI('ssidCCMP.conf') + + psk_agent = PSKAgent("secret123") + wd.register_psk_agent(psk_agent) + + devices = wd.list_devices(1) + device = devices[0] + + ordered_network = device.get_ordered_network('ssidCCMP') + + 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.state == DeviceState.connected' + wd.wait_for_object_condition(device, condition) + + testutil.test_iface_operstate() + testutil.test_ifaces_connected() + + + # Rekey 5 times just to make sure the key ID can be toggled back and + # forth without causing problems. + for i in range(5): + hapd.rekey(device.address) + + if resend_m3: + hapd.resend_m3(device.address) + + testutil.test_iface_operstate() + testutil.test_ifaces_connected() + + device.disconnect() + + condition = 'not obj.connected' + wd.wait_for_object_condition(ordered_network.network_object, condition) + + wd.unregister_psk_agent(psk_agent) + + def test_ext_key_control_port(self): + self.hapd.set_value('extended_key_id', '1') + self.hapd.reload() + + self.wd = IWD(True) + + self.validate(self.wd) + + def test_ext_key_control_port_retransmit(self): + self.hapd.set_value('extended_key_id', '1') + self.hapd.reload() + + self.wd = IWD(True) + + self.validate(self.wd, resend_m3=True) + + def test_ext_key_index_1(self): + self.hapd.set_value('extended_key_id', '2') + self.hapd.reload() + + self.wd = IWD(True) + + self.validate(self.wd) + + def test_ext_key_pae(self): + self.hapd.set_value('extended_key_id', '1') + self.hapd.reload() + + IWD.copy_to_storage('main_pae.conf', storage_dir=IWD_CONFIG_DIR, name='main.conf') + + self.wd = IWD(True) + + self.validate(self.wd) + + def tearDown(self): + try: + os.system('rm -rf %s/main.conf' % IWD_CONFIG_DIR) + except: + pass + + self.wd = None + + @classmethod + def setUpClass(cls): + cls.hapd = HostapdCLI() + + @classmethod + def tearDownClass(cls): + IWD.clear_storage() + cls.hapd = None + +if __name__ == '__main__': + unittest.main(exit=True) diff --git a/autotests/testWPA2-ext-key-id/hw.conf b/autotests/testWPA2-ext-key-id/hw.conf @@ -0,0 +1,6 @@ +[SETUP] +num_radios=2 +start_iwd=0 + +[HOSTAPD] +rad0=ssidCCMP.conf diff --git a/autotests/testWPA2-ext-key-id/main_pae.conf b/autotests/testWPA2-ext-key-id/main_pae.conf @@ -0,0 +1,2 @@ +[General] +ControlPortOverNL80211=false diff --git a/autotests/testWPA2-ext-key-id/ssidCCMP.conf b/autotests/testWPA2-ext-key-id/ssidCCMP.conf @@ -0,0 +1,11 @@ +hw_mode=g +channel=1 +ssid=ssidCCMP + +wpa=2 +wpa_pairwise=CCMP +wpa_passphrase=secret123 + +ieee80211w=2 +wpa_key_mgmt=WPA-PSK-SHA256 +extended_key_id=1 diff --git a/autotests/util/hostapd.py b/autotests/util/hostapd.py @@ -152,6 +152,9 @@ class HostapdCLI(object): ctx.start_process(self.cmdline + ['disable']).wait() ctx.start_process(self.cmdline + ['enable']).wait() + def disable(self): + ctx.start_process(self.cmdline + ['disable']).wait() + def list_sta(self): proc = ctx.start_process(self.cmdline + ['list_sta']) proc.wait() @@ -199,6 +202,28 @@ class HostapdCLI(object): cmd = self.cmdline + ['req_beacon', addr, request] ctx.start_process(cmd).wait() + def rekey(self, address=None): + if address: + cmd = 'REKEY_PTK %s' % address + self.ctrl_sock.sendall(cmd.encode('utf-8')) + self.wait_for_event('EAPOL-4WAY-HS-COMPLETED') + return + + cmd = 'REKEY_GTK' + self.ctrl_sock.sendall(cmd.encode('utf-8')) + + def resend_m3(self, address): + cmd = 'RESEND_M3 %s' % address + self.ctrl_sock.sendall(cmd.encode('utf-8')) + + def chan_switch(self, channel): + if channel > len(chan_freq_map): + raise Exception("Only 2.4GHz channels supported for chan_switch") + + cmd = self.cmdline + ['chan_switch', '50', str(chan_freq_map[channel])] + ctx.start_process(cmd).wait() + self.wait_for_event('AP-CSA-FINISHED') + @property def bssid(self): cmd = self.cmdline + ['status'] diff --git a/autotests/util/iwd.py b/autotests/util/iwd.py @@ -1133,7 +1133,8 @@ class IWD(AsyncOpAbstract): ' condition was not met in '\ + str(max_wait) + ' sec')) - def wait(self, time): + @staticmethod + def wait(time): ctx.non_block_wait(lambda : False, time, exception=False) @staticmethod diff --git a/client/daemon.c b/client/daemon.c @@ -0,0 +1,141 @@ +/* + * + * Wireless daemon for Linux + * + * Copyright (C) 2021 Intel Corporation. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <errno.h> +#include <ell/ell.h> +#include <unistd.h> + +#include "client/dbus-proxy.h" +#include "client/display.h" +#include "client/command.h" +#include "client/daemon.h" + +static bool netconfig_enabled; + +#define IWD_DAEMON_PATH "/net/connman/iwd" + +int daemon_netconfig_enabled() +{ + const struct proxy_interface *proxy = + proxy_interface_find(IWD_DAEMON_INTERFACE, IWD_DAEMON_PATH); + + if (!proxy) + return -ENOENT; + + return netconfig_enabled; +} + +static void get_info_callback(struct l_dbus_message *message, void *user_data) +{ + struct l_dbus_message_iter iter; + struct l_dbus_message_iter variant; + const char *key; + + if (dbus_message_has_error(message)) + return; + + if (!l_dbus_message_get_arguments(message, "a{sv}", &iter)) { + l_error("Failed to parse GetInfo message"); + return; + } + + while (l_dbus_message_iter_next_entry(&iter, &key, &variant)) { + if (strcmp(key, "NetworkConfigurationEnabled")) + continue; + + l_dbus_message_iter_get_variant(&variant, "b", + &netconfig_enabled); + break; + } + + if (!command_is_interactive_mode()) + return; + + l_dbus_message_get_arguments(message, "a{sv}", &iter); + display("NetworkConfigurationEnabled: %s\n", + netconfig_enabled ? "enabled" : "disabled"); + + while (l_dbus_message_iter_next_entry(&iter, &key, &variant)) { + if (!strcmp(key, "Version") || + !strcmp(key, "StateDirectory")) { + const char *sval; + + if (!l_dbus_message_iter_get_variant(&variant, "s", + &sval)) + continue; + + display("%s: %s\n", key, sval); + } + } +} + +static bool daemon_get_info(void) +{ + const struct proxy_interface *proxy = + proxy_interface_find(IWD_DAEMON_INTERFACE, IWD_DAEMON_PATH); + + if (!proxy) + return false; + + proxy_interface_method_call(proxy, "GetInfo", "", get_info_callback); + + return true; +} + +static void *daemon_create(void) +{ + daemon_get_info(); + return NULL; +} + +static void daemon_destroy(void *data) +{ +} + +static const struct proxy_interface_type_ops daemon_ops = { + .create = daemon_create, + .destroy = daemon_destroy, +}; + +static struct proxy_interface_type daemon_interface_type = { + .interface = IWD_DAEMON_INTERFACE, + .ops = &daemon_ops, +}; + +static int daemon_interface_init(void) +{ + proxy_interface_type_register(&daemon_interface_type); + + return 0; +} + +static void daemon_interface_exit(void) +{ + proxy_interface_type_unregister(&daemon_interface_type); +} + +INTERFACE_TYPE(daemon_interface_type, daemon_interface_init, + daemon_interface_exit) diff --git a/client/daemon.h b/client/daemon.h @@ -0,0 +1,23 @@ +/* + * + * Wireless daemon for Linux + * + * Copyright (C) 2021 Intel Corporation. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +int daemon_netconfig_enabled(void); diff --git a/client/dbus-proxy.h b/client/dbus-proxy.h @@ -35,6 +35,7 @@ struct proxy_interface; #define IWD_STATION_INTERFACE "net.connman.iwd.Station" #define IWD_STATION_DIAGNOSTIC_INTERFACE "net.connman.iwd.StationDiagnostic" #define IWD_AP_DIAGNOSTIC_INTERFACE "net.connman.iwd.AccessPointDiagnostic" +#define IWD_DAEMON_INTERFACE "net.connman.iwd.Daemon" typedef bool (*proxy_property_match_func_t) (const void *a, const void *b); diff --git a/client/station.c b/client/station.c @@ -24,6 +24,9 @@ #include <config.h> #endif +#include <arpa/inet.h> +#include <ifaddrs.h> + #include <ell/ell.h> #include "client/command.h" @@ -32,6 +35,7 @@ #include "client/network.h" #include "client/display.h" #include "client/diagnostic.h" +#include "client/daemon.h" struct station { bool scanning; @@ -141,6 +145,66 @@ static void check_errors_method_callback(struct l_dbus_message *message, dbus_message_has_error(message); } +static void display_addresses(const char *device_name) +{ + struct ifaddrs *ifa; + struct ifaddrs *cur; + bool have_address = false; + char addrstr[INET6_ADDRSTRLEN]; + int r; + + if (getifaddrs(&ifa) == -1) + return; + + for (cur = ifa; cur; cur = cur->ifa_next) { + if (cur->ifa_addr == NULL) + continue; + + if (strcmp(cur->ifa_name, device_name)) + continue; + + if (cur->ifa_addr->sa_family == AF_INET6) { + struct sockaddr_in6 *si6 = + (struct sockaddr_in6 *) cur->ifa_addr; + + if (IN6_IS_ADDR_LINKLOCAL(&si6->sin6_addr)) + continue; + + if (!inet_ntop(AF_INET6, &si6->sin6_addr, + addrstr, sizeof(addrstr))) + continue; + + have_address = true; + display("%s%*s %-*s%-*s\n", MARGIN, 8, "", 20, + "IPv6 address", 47, addrstr); + } else if (cur->ifa_addr->sa_family == AF_INET) { + struct sockaddr_in *si = + (struct sockaddr_in *) cur->ifa_addr; + + if (!inet_ntop(AF_INET, &si->sin_addr, + addrstr, sizeof(addrstr))) + continue; + + have_address = true; + display("%s%*s %-*s%-*s\n", MARGIN, 8, "", 20, + "IPv4 address", 47, addrstr); + } + } + + freeifaddrs(ifa); + + if (have_address) + return; + + r = daemon_netconfig_enabled(); + if (r < 0 || r == 1) + return; + + display("%s%*s %-*s%-*s\n", MARGIN, 8, "", 20, + "No IP addresses", 47, "Is DHCP client configured?"); +} + + static void display_station(const char *device_name, const struct proxy_interface *proxy) { @@ -154,6 +218,8 @@ static void display_station(const char *device_name, display("%s%*s %-*s%-*s\n", MARGIN, 8, "", 20, "Connected network", 47, network_get_name(station->connected_network)); + display_addresses(device_name); + /* * If connected the diagnostic interface is presumably up so * don't add the table footer just yet. diff --git a/configure.ac b/configure.ac @@ -1,5 +1,5 @@ AC_PREREQ(2.60) -AC_INIT(iwd, 1.18) +AC_INIT(iwd, 1.19) AC_CONFIG_HEADERS(config.h) AC_CONFIG_AUX_DIR(build-aux) @@ -129,6 +129,7 @@ AC_DEFINE_UNQUOTED(WIRED_STORAGEDIR, "${wired_storagedir}", AC_CHECK_FUNCS(explicit_bzero) AC_CHECK_FUNCS(rawmemchr) +AC_CHECK_FUNCS(reallocarray) AC_CHECK_HEADERS(linux/types.h linux/if_alg.h) @@ -267,7 +268,7 @@ if (test "${enable_external_ell}" = "yes"); then test "${enable_monitor}" != "no" || test "${enable_wired}" = "yes" || test "${enable_hwsim}" = "yes"); then - ell_min_version="0.44" + ell_min_version="0.45" else ell_min_version="0.5" fi diff --git a/doc/agent-api.txt b/doc/agent-api.txt @@ -7,19 +7,23 @@ Object path /net/connman/iwd Methods void RegisterAgent(object path) - Register new agent for handling user requests. + Register new agent for handling user secrets requests. + The agent object's methods on the net.connman.iwd.Agent + interface will be called when IWD needs to request + credentials for a connection to a network in station + mode. - Possible Errors: [service].Error.InvalidArguments - [service].Error.Failed - [service].Error.AlreadyExists + Possible Errors: [service].InvalidArguments + [service].Failed + [service].AlreadyExists void UnregisterAgent(object path) Unregister an existing agent. - Possible Errors: [service].Error.InvalidArguments - [service].Error.Failed - [service].Error.NotFound + Possible Errors: [service].InvalidArguments + [service].Failed + [service].NotFound Agent hierarchy @@ -80,5 +84,5 @@ Methods void Release() [noreply] Examples Requesting a passphrase for WPA2 network - RequestPassphrase("/network1") + RequestPassphrase("/net/connman/iwd/0/3/54657374_psk") ==> "secret123" diff --git a/doc/hostapd.config b/doc/hostapd.config @@ -36,3 +36,4 @@ CONFIG_OCV=y CONFIG_OWE=y CONFIG_DPP=y CONFIG_SAE=y +CONFIG_TESTING_OPTIONS=y diff --git a/doc/ip-configuration-api.txt b/doc/ip-configuration-api.txt @@ -0,0 +1,47 @@ +IP Configuration hierarchy +=============================== + +Service net.connman.iwd +Interface net.connman.iwd.IPv4Configuration [Experimental] +Interface net.connman.iwd.IPv6Configuration [Experimental] +Object path /net/connman/iwd/{phy0,phy1,...}/{1,2,...} +Object path /net/connman/iwd/{phy0,phy1,...}/p2p_peers/{aa_bb_cc_dd_ee_ff} + +The interfaces net.connman.iwd.IPv4Configuration and +net.connman.iwd.IPv6Configuration currently have the same sets of methods, +signals and properties. In station mode, when network configuration is +enabled there may be one or both interfaces present on a device object in +connected state depending on if IPv4 and IPv6 addresses have both been +configured. In P2P mode only net.connman.iwd.IPv4Configuration is used. + +Properties string Method [readonly] + + Indicates whether the local address was set + statically (value "static") or obtained automatically + such as through DHCP (value "auto"). Even when the + address was obtained from the remote end some + configuration bits, such as DNS addresses, may have + been overridden locally. + + string Address [readonly] + + Holds the local IP address. + + byte PrefixLength [readonly] + + Holds the prefix-length of the local subnet. For + IPv4 this maps to the netmask. + + string Gateway [readonly, optional] + + Holds the gateway address for the subnet if one + exists. + + array(string) DomainNameServers [readonly, optional] + + Holds the list of domain name server addresses + configured if any. + + array(string) DomainNames [readonly, optional] + + Holds the network's local domain names if any exist. diff --git a/doc/p2p-peer-api.txt b/doc/p2p-peer-api.txt @@ -19,6 +19,11 @@ Properties string Name [readonly] P2P Peer's display name + string Address [readonly] + + The peer's unique Device Address in the xx:xx:xx:xx:xx:xx + format. + string DeviceCategory [readonly] The category part of the peer's declared diff --git a/monitor/nlmon.c b/monitor/nlmon.c @@ -698,7 +698,7 @@ static const char *rsn_capabilities_bitfield[] = { "SPP A-MSDU Required", "PBAC", "Extended Key ID for Individually Addressed Frames", - "Reserved", + "Operating Channel Validation Capable", "Reserved", NULL }; @@ -1009,6 +1009,21 @@ static void print_ipv4(unsigned int level, const char *label, addr[0], addr[1], addr[2], addr[3]); } +static void print_ie_wfa_owe_transition(unsigned int level, const char *label, + const void *data, uint16_t size) +{ + struct ie_owe_transition_info info; + + if (ie_parse_owe_transition(data, size, &info) < 0) { + print_attr(level + 1, "Error parsing"); + return; + } + + print_attr(level + 1, "BSSID: "MAC, MAC_STR(info.bssid)); + print_attr(level + 1, "SSID: %s", util_ssid_to_utf8(info.ssid_len, + info.ssid)); +} + static void print_ie_vendor(unsigned int level, const char *label, const void *data, uint16_t size) { @@ -1054,6 +1069,10 @@ static void print_ie_vendor(unsigned int level, const char *label, case 0x12: print_ie_rsn_suites(level + 1, label, data, size); return; + case 0x1c: + print_ie_wfa_owe_transition(level + 1, label, + data - 6, size + 6); + return; default: return; } @@ -2157,6 +2176,108 @@ static void print_measurement_report(unsigned int level, const char *label, } } +static void print_fast_bss_transition(unsigned int level, const char *label, + const void *data, + uint16_t size) +{ + struct ie_ft_info ft_info; + uint32_t mic_len = 16; + + /* Most likely the MIC is 16 bytes */ + if (ie_parse_fast_bss_transition_from_data(data - 2, size + 2, + mic_len, &ft_info) < 0) { + mic_len = 24; + + /* Must be FILS, try 24 */ + if (ie_parse_fast_bss_transition_from_data(data - 2, size + 2, + mic_len, &ft_info) < 0) { + print_attr(level + 1, "error parsing"); + return; + } + } + + print_attr(level, "%s", label); + + if (ft_info.rsnxe_used) + print_attr(level + 1, "RSNXE Used set"); + + print_attr(level + 1, "MIC Element Count: %u", + ft_info.mic_element_count); + print_attr(level + 1, "MIC:"); + print_hexdump(level + 2, ft_info.mic, mic_len); + print_attr(level + 1, "ANONCE:"); + print_hexdump(level + 2, ft_info.anonce, sizeof(ft_info.anonce)); + print_attr(level + 1, "SNONCE:"); + print_hexdump(level + 2, ft_info.snonce, sizeof(ft_info.snonce)); + print_attr(level + 1, "R0KHID:"); + print_hexdump(level + 2, ft_info.r0khid, ft_info.r0khid_len); + + if (ft_info.r1khid_present) { + print_attr(level + 1, "R1KHID:"); + print_hexdump(level + 2, ft_info.r1khid, + sizeof(ft_info.r1khid)); + } + + if (ft_info.gtk_len) { + print_attr(level + 1, "GTK Key ID: %u", ft_info.gtk_key_id); + print_attr(level + 1, "GTK RSC:"); + print_hexdump(level + 2, ft_info.gtk_rsc, + sizeof(ft_info.gtk_rsc)); + print_attr(level + 1, "GTK:"); + print_hexdump(level + 2, ft_info.gtk, ft_info.gtk_len); + } + + if (ft_info.igtk_len) { + print_attr(level + 1, "IGTK Key ID: %u", ft_info.igtk_key_id); + print_attr(level + 1, "IGTK IPN:"); + print_hexdump(level + 2, ft_info.igtk_ipn, + sizeof(ft_info.igtk_ipn)); + print_attr(level + 1, "IGTK:"); + print_hexdump(level + 2, ft_info.igtk, ft_info.igtk_len); + } +} + +static void print_mobility_domain(unsigned int level, const char *label, + const void *data, uint16_t size) +{ + uint16_t mdid; + bool ft_over_ds; + bool resource_req; + + print_attr(level, "%s", label); + + if (ie_parse_mobility_domain_from_data(data - 2, size + 2, &mdid, + &ft_over_ds, &resource_req) < 0) { + print_attr(level + 1, "error parsing"); + return; + } + + print_attr(level + 1, "MDID: %04x", mdid); + + if (ft_over_ds) + print_attr(level + 1, "FT-over-DS bit set"); + + if (resource_req) + print_attr(level + 1, "Resource Request Protocol bit set"); +} + +static void print_rsnx(unsigned int level, const char *label, + const void *data, uint16_t size) +{ + const uint8_t *ptr = data; + uint8_t field_size = bit_field(ptr[0], 0, 4); + + print_attr(level, "%s", label); + + print_attr(level + 1, "Field Size: %u", field_size); + + if (test_bit(ptr, 4)) + print_attr(level + 1, "Protected TWT Operations Support"); + + if (test_bit(ptr, 5)) + print_attr(level + 1, "SAE Hash-to-Element"); +} + static struct attr_entry ie_entry[] = { { IE_TYPE_SSID, "SSID", ATTR_CUSTOM, { .function = print_ie_ssid } }, @@ -2215,6 +2336,12 @@ static struct attr_entry ie_entry[] = { ATTR_CUSTOM, { .function = print_measurement_request } }, { IE_TYPE_MEASUREMENT_REPORT, "Measurement Report", ATTR_CUSTOM, { .function = print_measurement_report } }, + { IE_TYPE_FAST_BSS_TRANSITION, "Fast BSS Transition", + ATTR_CUSTOM, { .function = print_fast_bss_transition } }, + { IE_TYPE_MOBILITY_DOMAIN, "Mobility Domain", + ATTR_CUSTOM, { .function = print_mobility_domain } }, + { IE_TYPE_RSNX, "RSNX", + ATTR_CUSTOM, { .function = print_rsnx } }, { }, }; @@ -4640,6 +4767,68 @@ static void print_rm_action_frame(unsigned int level, const uint8_t *body, } } +static void print_ft_request(unsigned int level, + const uint8_t *body, size_t body_len) +{ + struct ie_tlv_iter iter; + + switch (body[0]) { + case 1: + if (body_len < 13) + return; + + print_attr(level + 1, "FT Request"); + break; + case 2: + if (body_len < 15) + return; + + print_attr(level + 1, "FT Response"); + break; + case 3: + /* FT Confirm/Ack are not yet supported */ + print_attr(level + 1, "FT Confirm"); + return; + case 4: + print_attr(level + 1, "FT ACK"); + return; + } + + print_attr(level + 1, "STA Address: "MAC, MAC_STR(body + 1)); + print_attr(level + 1, "Target AP Address: "MAC, MAC_STR(body + 7)); + + if (body[0] == 2) { + print_attr(level + 1, "Status: %u", l_get_le16(body + 13)); + body += 15; + body_len -= 15; + } else { + body += 13; + body_len -= 15; + } + + ie_tlv_iter_init(&iter, body, body_len); + + while (ie_tlv_iter_next(&iter)) { + uint8_t tag = ie_tlv_iter_get_tag(&iter); + size_t len = ie_tlv_iter_get_length(&iter); + const void *data = ie_tlv_iter_get_data(&iter); + + switch (tag) { + case IE_TYPE_RSN: + print_ie_rsn(level + 1, "RSN", data, len); + break; + case IE_TYPE_MOBILITY_DOMAIN: + print_mobility_domain(level + 1, "Mobility Domain", + data, len); + break; + case IE_TYPE_FAST_BSS_TRANSITION: + print_fast_bss_transition(level + 1, + "Fast BSS Transition", + data, len); + break; + } + } +} static void print_action_mgmt_frame(unsigned int level, const struct mmpdu_header *mmpdu, size_t total_len, bool no_ack) @@ -4702,6 +4891,9 @@ static void print_action_mgmt_frame(unsigned int level, if (!memcmp(oui, wifi_alliance_oui, 3) && oui[3] == 0x09) print_p2p_action_frame(level + 1, body + 5, body_len - 5); + } else if (body[0] == 6) { + print_ft_request(level, body + 1, body_len - 1); + return; } print_mpdu_frame_control(level + 1, &mmpdu->fc); @@ -5671,6 +5863,76 @@ static void print_scan_flags(unsigned int level, const char *label, } } +static void print_key_type(unsigned int level, const char *label, + const void *data, uint16_t size) +{ + const uint8_t *ptr = data; + const char *str; + + switch (*ptr) { + case NL80211_KEYTYPE_GROUP: + str = "Group"; + break; + case NL80211_KEYTYPE_PAIRWISE: + str = "Pairwise"; + break; + case NL80211_KEYTYPE_PEERKEY: + str = "Peerkey"; + break; + default: + str = "Unknown"; + } + + print_attr(level, "%s: %s", label, str); +} + +static void print_key_mode(unsigned int level, const char *label, + const void *data, uint16_t size) +{ + const uint8_t *ptr = data; + const char *str; + + switch (*ptr) { + case NL80211_KEY_RX_TX: + str = "RX/TX"; + break; + case NL80211_KEY_NO_TX: + str = "RX Only"; + break; + case NL80211_KEY_SET_TX: + str = "Set TX"; + break; + default: + str = "Unknown"; + } + + print_attr(level, "%s: %s", label, str); +} + +static const struct attr_entry default_key_type_table[] = { + { NL80211_KEY_DEFAULT_TYPE_UNICAST, "Unicast", ATTR_FLAG }, + { NL80211_KEY_DEFAULT_TYPE_MULTICAST, "Multicast", ATTR_FLAG }, + { } +}; + +static const struct attr_entry key_table[] = { + { NL80211_KEY_DATA, "Key Data", ATTR_BINARY }, + { NL80211_KEY_IDX, "Key Index", ATTR_U8 }, + { NL80211_KEY_CIPHER, "Key Cipher", ATTR_CUSTOM, + { .function = print_cipher_suite } }, + { NL80211_KEY_SEQ, "Key Sequence", ATTR_BINARY }, + { NL80211_KEY_DEFAULT, "Default", ATTR_FLAG }, + { NL80211_KEY_DEFAULT_MGMT, "Default Management", ATTR_FLAG }, + { NL80211_KEY_TYPE, "Key Type", ATTR_CUSTOM, + { .function = print_key_type} }, + { NL80211_KEY_DEFAULT_TYPES, "Default Key Types", ATTR_NESTED, + { default_key_type_table } }, + { NL80211_KEY_MODE, "Key Mode", ATTR_CUSTOM, + { .function = print_key_mode } }, + { NL80211_KEY_DEFAULT_BEACON, "Default Beacon", ATTR_FLAG }, + { } +}; + static const struct attr_entry attr_table[] = { { NL80211_ATTR_WIPHY, "Wiphy", ATTR_U32 }, @@ -5846,7 +6108,7 @@ static const struct attr_entry attr_table[] = { { NL80211_ATTR_PREV_BSSID, "Previous BSSID", ATTR_ADDRESS }, { NL80211_ATTR_KEY, - "Key" }, + "Key", ATTR_NESTED, { key_table } }, { NL80211_ATTR_KEYS, "Keys" }, { NL80211_ATTR_PID, diff --git a/src/ap.c b/src/ap.c @@ -53,6 +53,7 @@ #include "src/wscutil.h" #include "src/eap-wsc.h" #include "src/ip-pool.h" +#include "src/netconfig.h" #include "src/ap.h" #include "src/storage.h" #include "src/diagnostic.h" @@ -132,7 +133,6 @@ struct ap_wsc_pbc_probe_record { uint64_t timestamp; }; -static bool netconfig_enabled; static char **global_addr4_strs; static uint32_t netdev_watch; static struct l_netlink *rtnl; @@ -981,20 +981,16 @@ static void ap_handshake_event(struct handshake_state *hs, break; case HANDSHAKE_EVENT_P2P_IP_REQUEST: { - L_AUTO_FREE_VAR(char *, lease_addr_str) = NULL; - L_AUTO_FREE_VAR(char *, lease_netmask_str) = NULL; char own_addr_str[INET_ADDRSTRLEN]; if (!ap_sta_get_dhcp4_lease(sta)) break; - lease_addr_str = l_dhcp_lease_get_address(sta->ip_alloc_lease); - lease_netmask_str = - l_dhcp_lease_get_netmask(sta->ip_alloc_lease); + sta->hs->client_ip_addr = + l_dhcp_lease_get_address_u32(sta->ip_alloc_lease); + sta->hs->subnet_mask = + l_dhcp_lease_get_netmask_u32(sta->ip_alloc_lease); l_rtnl_address_get_address(ap->netconfig_addr4, own_addr_str); - - sta->hs->client_ip_addr = IP4_FROM_STR(lease_addr_str); - sta->hs->subnet_mask = IP4_FROM_STR(lease_netmask_str); sta->hs->go_ip_addr = IP4_FROM_STR(own_addr_str); break; } @@ -1465,28 +1461,25 @@ static uint32_t ap_assoc_resp(struct ap_state *ap, struct sta_state *sta, struct ie_fils_ip_addr_response_info ip_resp_info = {}; if (ip_req_info->ipv4 && sta && ap_sta_get_dhcp4_lease(sta)) { - L_AUTO_FREE_VAR(char *, lease_addr_str) = - l_dhcp_lease_get_address(sta->ip_alloc_lease); - L_AUTO_FREE_VAR(char *, lease_netmask_str) = - l_dhcp_lease_get_netmask(sta->ip_alloc_lease); uint32_t lease_lifetime = l_dhcp_lease_get_lifetime(sta->ip_alloc_lease); - L_AUTO_FREE_VAR(char *, lease_gateway_str) = - l_dhcp_lease_get_gateway(sta->ip_alloc_lease); + uint32_t gw = + l_dhcp_lease_get_gateway_u32( + sta->ip_alloc_lease); char **lease_dns_str_list = l_dhcp_lease_get_dns(sta->ip_alloc_lease); - ip_resp_info.ipv4_addr = IP4_FROM_STR(lease_addr_str); + ip_resp_info.ipv4_addr = l_dhcp_lease_get_address_u32( + sta->ip_alloc_lease); ip_resp_info.ipv4_prefix_len = - __builtin_popcount(IP4_FROM_STR( - lease_netmask_str)); + l_dhcp_lease_get_prefix_length( + sta->ip_alloc_lease); if (lease_lifetime != 0xffffffff) ip_resp_info.ipv4_lifetime = lease_lifetime; - if (lease_gateway_str) { - ip_resp_info.ipv4_gateway = - IP4_FROM_STR(lease_gateway_str); + if (gw) { + ip_resp_info.ipv4_gateway = gw; memcpy(ip_resp_info.ipv4_gateway_mac, ap->netconfig_gateway4_mac, 6); } @@ -1759,6 +1752,11 @@ static void ap_assoc_reassoc(struct sta_state *sta, bool reassoc, err = MMPDU_REASON_CODE_INVALID_AKMP; goto unsupported; } + + if (rsn_info.group_cipher != ap->group_cipher) { + err = MMPDU_REASON_CODE_INVALID_GROUP_CIPHER; + goto unsupported; + } } /* 802.11-2016 11.3.5.3 j) */ @@ -2828,7 +2826,7 @@ static int ap_load_ipv4(struct ap_state *ap, const struct l_settings *config) unsigned int lease_time = 0; struct in_addr ia; - if (!l_settings_has_group(config, "IPv4") || !netconfig_enabled) + if (!l_settings_has_group(config, "IPv4") || !netconfig_enabled()) return 0; if (l_settings_has_key(config, "IPv4", "Address")) { @@ -3835,12 +3833,7 @@ static int ap_init(void) * Enable network configuration and DHCP only if * [General].EnableNetworkConfiguration is true. */ - if (!l_settings_get_bool(settings, "General", - "EnableNetworkConfiguration", - &netconfig_enabled)) - netconfig_enabled = false; - - if (netconfig_enabled) { + if (netconfig_enabled()) { if (l_settings_get_value(settings, "IPv4", "APAddressPool")) { global_addr4_strs = l_settings_get_string_list(settings, "IPv4", diff --git a/src/auth-proto.h b/src/auth-proto.h @@ -44,6 +44,7 @@ struct auth_proto { const uint8_t *frame, size_t len); int (*rx_associate)(struct auth_proto *driver, const uint8_t *frame, size_t len); + int (*rx_oci)(struct auth_proto *driver); bool (*auth_timeout)(struct auth_proto *ap); bool (*assoc_timeout)(struct auth_proto *ap); uint8_t prev_bssid[6]; @@ -98,3 +99,11 @@ static inline bool auth_proto_assoc_timeout(struct auth_proto *ap) return false; } + +static inline int auth_proto_rx_oci(struct auth_proto *ap) +{ + if (ap && ap->rx_oci) + return ap->rx_oci(ap); + + return -ENOTSUP; +} diff --git a/src/band.c b/src/band.c @@ -465,3 +465,461 @@ try_vht80: return -ENETUNREACH; } + +static int band_channel_info_get_bandwidth(const struct band_chandef *info) +{ + switch (info->channel_width) { + case BAND_CHANDEF_WIDTH_20NOHT: + case BAND_CHANDEF_WIDTH_20: + return 20; + case BAND_CHANDEF_WIDTH_40: + return 40; + case BAND_CHANDEF_WIDTH_80: + return 80; + case BAND_CHANDEF_WIDTH_80P80: + case BAND_CHANDEF_WIDTH_160: + return 160; + default: + break; + } + + return -ENOTSUP; +} + +struct operating_class_info { + uint32_t starting_frequency; + uint32_t flags; + uint8_t channel_set[20]; + uint8_t center_frequencies[8]; + uint16_t channel_spacing; + uint8_t operating_class; +}; + +enum operating_class_flags { + PRIMARY_CHANNEL_UPPER = 0x1, + PRIMARY_CHANNEL_LOWER = 0x2, + PLUS80 = 0x4, +}; + +static const struct operating_class_info e4_operating_classes[] = { + { + .operating_class = 81, + .starting_frequency = 2407, + .channel_set = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }, + .channel_spacing = 20, + }, + { + .operating_class = 82, + .starting_frequency = 2414, + .channel_set = { 14 }, + .channel_spacing = 20, + }, + { + .operating_class = 83, + .starting_frequency = 2407, + .channel_set = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, + .channel_spacing = 40, + .flags = PRIMARY_CHANNEL_LOWER, + }, + { + .operating_class = 84, + .starting_frequency = 2407, + .channel_set = { 5, 6, 7, 8, 9, 10, 11, 12, 13 }, + .channel_spacing = 40, + .flags = PRIMARY_CHANNEL_UPPER, + }, + { + .operating_class = 115, + .starting_frequency = 5000, + .channel_set = { 36, 40, 44, 48}, + .channel_spacing = 20, + }, + { + .operating_class = 116, + .starting_frequency = 5000, + .channel_set = { 36, 44 }, + .channel_spacing = 40, + .flags = PRIMARY_CHANNEL_LOWER, + }, + { + .operating_class = 117, + .starting_frequency = 5000, + .channel_set = { 40, 48 }, + .channel_spacing = 40, + .flags = PRIMARY_CHANNEL_UPPER, + }, + { + .operating_class = 118, + .starting_frequency = 5000, + .channel_set = { 52, 56, 60, 64}, + .channel_spacing = 20, + }, + { + .operating_class = 119, + .starting_frequency = 5000, + .channel_set = { 52, 60 }, + .channel_spacing = 40, + .flags = PRIMARY_CHANNEL_LOWER, + }, + { + .operating_class = 120, + .starting_frequency = 5000, + .channel_set = { 56, 64 }, + .channel_spacing = 40, + .flags = PRIMARY_CHANNEL_UPPER, + }, + { + .operating_class = 121, + .starting_frequency = 5000, + .channel_set = { 100, 104, 108, 112, 116, 120, + 124, 128, 132, 136, 140, 144 }, + .channel_spacing = 20, + }, + { + .operating_class = 122, + .starting_frequency = 5000, + .channel_set = { 100, 108, 116, 124, 132, 140}, + .channel_spacing = 40, + .flags = PRIMARY_CHANNEL_LOWER, + }, + { + .operating_class = 123, + .starting_frequency = 5000, + .channel_set = { 104, 112, 120, 128, 136, 144 }, + .channel_spacing = 40, + .flags = PRIMARY_CHANNEL_UPPER, + }, + { + .operating_class = 124, + .starting_frequency = 5000, + .channel_set = { 149, 153, 157, 161 }, + .channel_spacing = 20, + }, + { + .operating_class = 125, + .starting_frequency = 5000, + .channel_set = { 149, 153, 157, 161, 165, 169, 173}, + .channel_spacing = 20, + }, + { + .operating_class = 126, + .starting_frequency = 5000, + .channel_set = { 149, 157, 165}, + .channel_spacing = 40, + .flags = PRIMARY_CHANNEL_LOWER, + }, + { + .operating_class = 127, + .starting_frequency = 5000, + .channel_set = { 153, 161, 169 }, + .channel_spacing = 40, + .flags = PRIMARY_CHANNEL_UPPER, + }, + { + .operating_class = 128, + .starting_frequency = 5000, + .channel_spacing = 80, + .center_frequencies = { 42, 58, 106, 122, 138, 155 }, + }, + { + .operating_class = 129, + .starting_frequency = 5000, + .channel_spacing = 160, + .center_frequencies = { 50, 114 }, + }, + { + .operating_class = 130, + .starting_frequency = 5000, + .channel_spacing = 80, + .center_frequencies = { 42, 58, 106, 122, 138, 155 }, + .flags = PLUS80, + }, +}; + +static const struct operating_class_info *e4_find_opclass(uint32_t opclass) +{ + unsigned int i; + + for (i = 0; i < L_ARRAY_SIZE(e4_operating_classes); i++) { + if (e4_operating_classes[i].operating_class != opclass) + continue; + + return &e4_operating_classes[i]; + } + + return NULL; +} + +static int e4_channel_to_frequency(const struct operating_class_info *info, + uint32_t channel) +{ + unsigned int i; + unsigned int offset; + + for (i = 0; info->channel_set[i] && + i < L_ARRAY_SIZE(info->channel_set); i++) { + if (info->channel_set[i] != channel) + continue; + + return channel * 5 + info->starting_frequency; + } + + /* + * Only classes in Table E4 with center frequencies are 128-130, + * which use 20 Mhz wide channels. Since E4 gives a center frequency, + * calculate the channel offset based on channel spacing. + * + * Typically +/- 6 for 80 Mhz channels and +/- 14 for 160 Mhz channels + */ + offset = (info->channel_spacing - 20) / 5 / 2; + + /* + * Check that the channel is in the frequency range given by one of + * the center frequencies listed for the operating class. The channel + * must be a valid channel for a lower operating class and spaced + * 20 mhz apart + */ + for (i = 0; info->center_frequencies[i] && + i < L_ARRAY_SIZE(info->center_frequencies); i++) { + + unsigned int upper = info->center_frequencies[i] + offset; + unsigned int j = info->center_frequencies[i] - offset; + + while (j <= upper && channel >= j) { + if (channel == j) + return channel * 5 + info->starting_frequency; + + j += 4; + } + } + + return -EINVAL; +} + +static int e4_frequency_to_channel(const struct operating_class_info *info, + uint32_t frequency) +{ + return (frequency - info->starting_frequency) / 5; +} + +static int e4_has_frequency(const struct operating_class_info *info, + uint32_t frequency) +{ + unsigned int i; + unsigned int channel = e4_frequency_to_channel(info, frequency); + + for (i = 0; info->channel_set[i] && + i < L_ARRAY_SIZE(info->channel_set); i++) { + if (info->channel_set[i] != channel) + continue; + + return 0; + } + + return -ENOENT; +} + +static int e4_has_ccfi(const struct operating_class_info *info, + uint32_t center_frequency) +{ + unsigned int i; + unsigned int ccfi = e4_frequency_to_channel(info, center_frequency); + + for (i = 0; info->center_frequencies[i] && + i < L_ARRAY_SIZE(info->center_frequencies); i++) { + if (info->center_frequencies[i] != ccfi) + continue; + + return 0; + } + + return -ENOENT; +} + +static int e4_class_matches(const struct operating_class_info *info, + const struct band_chandef *chandef) +{ + int own_bandwidth = band_channel_info_get_bandwidth(chandef); + int r; + + if (own_bandwidth < 0) + return own_bandwidth; + + switch (chandef->channel_width) { + case BAND_CHANDEF_WIDTH_20NOHT: + case BAND_CHANDEF_WIDTH_20: + case BAND_CHANDEF_WIDTH_40: + if (own_bandwidth != info->channel_spacing) + return -ENOENT; + + if (own_bandwidth == 40) { + uint32_t behavior; + + if (chandef->center1_frequency > chandef->frequency) + behavior = PRIMARY_CHANNEL_LOWER; + else + behavior = PRIMARY_CHANNEL_UPPER; + + if ((info->flags & behavior) != behavior) + return -ENOENT; + } + + return e4_has_frequency(info, chandef->frequency); + case BAND_CHANDEF_WIDTH_80: + case BAND_CHANDEF_WIDTH_160: + if (info->flags & PLUS80) + return -ENOENT; + + if (own_bandwidth != info->channel_spacing) + return -ENOENT; + + return e4_has_ccfi(info, chandef->center1_frequency); + case BAND_CHANDEF_WIDTH_80P80: + if (!(info->flags & PLUS80)) + return -ENOENT; + + r = e4_has_ccfi(info, chandef->center1_frequency); + if (r < 0) + return r; + + return e4_has_ccfi(info, chandef->center2_frequency); + default: + break; + } + + return -ENOTSUP; +} + +int oci_to_frequency(uint32_t operating_class, uint32_t channel) +{ + const struct operating_class_info *info; + + info = e4_find_opclass(operating_class); + if (!info) + return -ENOENT; + + return e4_channel_to_frequency(info, channel); +} + +int oci_verify(const uint8_t oci[static 3], const struct band_chandef *own) +{ + const struct operating_class_info *info; + int oci_frequency; + int own_bandwidth; + int oci_bandwidth; + + info = e4_find_opclass(oci[0]); + if (!info) + return -ENOENT; + + /* + * 802.11-2020, 12.2.9: + * Verifying that the maximum bandwidth used by the STA to transmit or + * receive PPDUs to/from the peer STA from which the OCI was received + * is no greater than the bandwidth of the operating class specified + * in the Operating Class field of the received OCI + */ + own_bandwidth = band_channel_info_get_bandwidth(own); + if (own_bandwidth < 0) + return own_bandwidth; + + oci_bandwidth = info->channel_spacing; + if (info->flags & PLUS80) + oci_bandwidth *= 2; + + if (own_bandwidth > oci_bandwidth) + return -EPERM; + + /* + * 802.11-2020, 12.2.9: + * Verifying that the primary channel used by the STA to transmit or + * receive PPDUs to/from the peer STA from which the OCI was received + * is equal to the Primary Channel Number field (for the corresponding + * operating class) + */ + oci_frequency = e4_channel_to_frequency(info, oci[1]); + if (oci_frequency < 0) + return oci_frequency; + + if (oci_frequency != (int) own->frequency) + return -EPERM; + + /* + * 802.11-2020, 12.2.9: + * Verifying that, when 40 MHz bandwidth is used by the STA to transmit + * or receive PPDUs to/from the peer STA from which the OCI was + * received, the nonprimary 20 MHz used matches the operating class + * (i.e., upper/lower behavior) specified in the Operating Class field + * of the received OCI + * + * NOTE: For now we only check this if the STA and peer are operating + * on 40 Mhz channels. If the STA is operating on 40 Mhz while the + * peer is operating on 80 or 160 Mhz wide channels, then only the + * primary channel validation is performed + */ + if (own_bandwidth == 40 && oci_bandwidth == 40) { + uint32_t behavior; + + /* + * - Primary Channel Upper Behavior -> Secondary channel below + * primary channel. Or HT40MINUS. + * - Primary Channel Lower Behavior -> Secondary channel above + * primary channel. Or HT40PLUS. + */ + if (own->center1_frequency > own->frequency) + behavior = PRIMARY_CHANNEL_LOWER; + else + behavior = PRIMARY_CHANNEL_UPPER; + + if ((info->flags & behavior) != behavior) + return -EPERM; + } + + /* + * 802.11-2020, 12.2.9: + * Verifying that, if operating an 80+80 MHz operating class, the + * frequency segment 1 channel number used by the STA to transmit or + * receive PPDUs to/from the peer STA from which the OCI was received + * is equal to the Frequency Segment 1 Channel Number field of the OCI. + */ + if (own->channel_width == BAND_CHANDEF_WIDTH_80P80) { + uint32_t freq_segment1_chan_num; + + if (!(info->flags & PLUS80)) + return -EPERM; + + freq_segment1_chan_num = + e4_frequency_to_channel(info, own->center2_frequency); + + if (freq_segment1_chan_num != oci[2]) + return -EPERM; + } + + return 0; +} + +int oci_from_chandef(const struct band_chandef *own, uint8_t oci[static 3]) +{ + unsigned int i; + + for (i = 0; i < L_ARRAY_SIZE(e4_operating_classes); i++) { + const struct operating_class_info *info = + &e4_operating_classes[i]; + + if (e4_class_matches(info, own) < 0) + continue; + + oci[0] = info->operating_class; + oci[1] = e4_frequency_to_channel(info, own->frequency); + + if (own->center2_frequency) + oci[2] = e4_frequency_to_channel(info, + own->center2_frequency); + else + oci[2] = 0; + + return 0; + } + + return -ENOENT; +} diff --git a/src/band.h b/src/band.h @@ -27,6 +27,22 @@ enum ofdm_channel_width { OFDM_CHANNEL_WIDTH_160MHZ, }; +enum band_chandef_width { + BAND_CHANDEF_WIDTH_20NOHT = 0, + BAND_CHANDEF_WIDTH_20, + BAND_CHANDEF_WIDTH_40, + BAND_CHANDEF_WIDTH_80, + BAND_CHANDEF_WIDTH_80P80, + BAND_CHANDEF_WIDTH_160, +}; + +struct band_chandef { + uint32_t frequency; + uint32_t channel_width; + uint32_t center1_frequency; + uint32_t center2_frequency; +}; + struct band { uint8_t vht_mcs_set[8]; uint8_t vht_capabilities[4]; @@ -55,3 +71,8 @@ int band_estimate_nonht_rate(const struct band *band, const uint8_t *supported_rates, const uint8_t *ext_supported_rates, int32_t rssi, uint64_t *out_data_rate); + +int oci_to_frequency(uint32_t operating_class, uint32_t channel); + +int oci_verify(const uint8_t oci[static 3], const struct band_chandef *own); +int oci_from_chandef(const struct band_chandef *own, uint8_t oci[static 3]); diff --git a/src/eapol.c b/src/eapol.c @@ -43,6 +43,7 @@ #include "src/watchlist.h" #include "src/erp.h" #include "src/iwd.h" +#include "src/band.h" static struct l_queue *state_machines; static struct l_queue *preauths; @@ -230,6 +231,11 @@ static size_t eapol_get_mic_length(enum ie_rsn_akm_suite akm, size_t pmk_len) l_error("Invalid PMK length of %zu for OWE", pmk_len); return 0; } + case IE_RSN_AKM_SUITE_FILS_SHA256: + case IE_RSN_AKM_SUITE_FILS_SHA384: + case IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA256: + case IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA384: + return 0; default: return 16; } @@ -253,6 +259,8 @@ uint8_t *eapol_decrypt_key_data(enum ie_rsn_akm_suite akm, const uint8_t *kek, switch (akm) { case IE_RSN_AKM_SUITE_FILS_SHA256: case IE_RSN_AKM_SUITE_FILS_SHA384: + case IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA256: + case IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA384: if (key_data_len < 16) return NULL; @@ -328,13 +336,16 @@ uint8_t *eapol_decrypt_key_data(enum ie_rsn_akm_suite akm, const uint8_t *kek, break; case IE_RSN_AKM_SUITE_FILS_SHA256: case IE_RSN_AKM_SUITE_FILS_SHA384: + case IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA256: + case IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA384: { struct iovec ad[1]; ad[0].iov_base = (void *)frame; ad[0].iov_len = key_data - (const uint8_t *)frame; - if (akm == IE_RSN_AKM_SUITE_FILS_SHA256) + if (akm == IE_RSN_AKM_SUITE_FILS_SHA256 || akm == + IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA256) kek_len = 32; else kek_len = 64; @@ -761,6 +772,8 @@ struct eapol_key *eapol_create_gtk_2_of_2( enum eapol_protocol_version protocol, enum eapol_key_descriptor_version version, uint64_t key_replay_counter, + size_t extra_len, + const uint8_t *extra_data, bool is_wpa, uint8_t wpa_key_id, size_t mic_len) { uint8_t snonce[32]; @@ -769,7 +782,8 @@ struct eapol_key *eapol_create_gtk_2_of_2( memset(snonce, 0, sizeof(snonce)); step2 = eapol_create_common(protocol, version, true, key_replay_counter, snonce, - 0, NULL, 0, is_wpa, mic_len); + extra_len, extra_data, 0, is_wpa, + mic_len); if (!step2) return step2; @@ -846,6 +860,7 @@ struct eapol_sm { uint8_t installed_igtk_len; uint8_t installed_igtk[CRYPTO_MAX_IGTK_LEN]; unsigned int mic_len; + bool rekey : 1; }; static void eapol_sm_destroy(void *value) @@ -1107,6 +1122,20 @@ static inline size_t append_ie(uint8_t *ies, const uint8_t *ie) return ie[1] + 2; } +static size_t append_oci(uint8_t *ies, const struct band_chandef *chandef) +{ + unsigned int len = 0; + + ies[len++] = IE_TYPE_VENDOR_SPECIFIC; + ies[len++] = 4 + 3; + l_put_be32(HANDSHAKE_KDE_OCI, ies + len); + len += 4; + oci_from_chandef(chandef, ies + len); + len += 3; + + return len; +} + static void eapol_handle_ptk_1_of_4(struct eapol_sm *sm, const struct eapol_key *ek, bool unencrypted) @@ -1143,7 +1172,8 @@ static void eapol_handle_ptk_1_of_4(struct eapol_sm *sm, * send no PMKID KDE. */ if (!sm->eap_exchanged && !sm->handshake->wpa_ie && - rsn_info.num_pmkids) { + rsn_info.num_pmkids && + sm->require_handshake) { bool found = false; int i; @@ -1208,6 +1238,9 @@ static void eapol_handle_ptk_1_of_4(struct eapol_sm *sm, return; } + if (sm->handshake->ptk_complete) + sm->rekey = true; + handshake_state_new_snonce(sm->handshake); handshake_state_set_anonce(sm->handshake, ek->key_nonce); @@ -1215,10 +1248,7 @@ static void eapol_handle_ptk_1_of_4(struct eapol_sm *sm, goto error_unspecified; } - if (sm->handshake->akm_suite & - (IE_RSN_AKM_SUITE_FT_OVER_8021X | - IE_RSN_AKM_SUITE_FT_USING_PSK | - IE_RSN_AKM_SUITE_FT_OVER_SAE_SHA256)) { + if (IE_AKM_IS_FT(sm->handshake->akm_suite)) { /* * Rebuild the RSNE to include the PMKR1Name and append * MDE + FTE. @@ -1245,6 +1275,15 @@ static void eapol_handle_ptk_1_of_4(struct eapol_sm *sm, } /* + * IEEE 802.11-2020 Section 12.7.6.3 + * "Additionally, contains an OCI KDE when + * dot11RSNAOperatingChannelValidationActivated is true on the + * Supplicant." + */ + if (sm->handshake->supplicant_ocvc && sm->handshake->chandef) + ies_len += append_oci(ies + ies_len, sm->handshake->chandef); + + /* * 802.11-2020, Section 12.7.6.3: * "The RSNXE that the Supplicant sent in its (Re)Association Request * frame, if this element is present in the (Re)Association Request @@ -1616,18 +1655,24 @@ static void eapol_handle_ptk_3_of_4(struct eapol_sm *sm, struct handshake_state *hs = sm->handshake; const uint8_t *kck; const uint8_t *kek; - struct eapol_key *step4; + _auto_(l_free) struct eapol_key *step4 = NULL; uint8_t mic[MIC_MAXLEN]; const uint8_t *gtk = NULL; size_t gtk_len; const uint8_t *igtk = NULL; size_t igtk_len; + const uint8_t *key_id = NULL; + size_t key_id_len; const uint8_t *rsne; + struct ie_rsn_info rsn_info; const uint8_t *optional_rsne = NULL; const uint8_t *transition_disable; size_t transition_disable_len; uint8_t gtk_key_index; uint16_t igtk_key_index; + const uint8_t *oci; + size_t oci_len; + int r; l_debug("ifindex=%u", hs->ifindex); @@ -1667,20 +1712,29 @@ static void eapol_handle_ptk_3_of_4(struct eapol_sm *sm, if (!rsne) goto error_ie_different; - if (!handshake_util_ap_ie_matches(rsne, hs->authenticator_ie, - hs->wpa_ie)) + if (!hs->wpa_ie) + r = ie_parse_rsne_from_data(rsne, rsne[1] + 2, &rsn_info); + else + r = ie_parse_wpa_from_data(rsne, rsne[1] + 2, &rsn_info); + + if (r < 0) + goto error_ie_different; + + if ((rsne[1] != hs->authenticator_ie[1] || + memcmp(rsne + 2, hs->authenticator_ie + 2, rsne[1])) && + !handshake_util_ap_ie_matches(&rsn_info, + hs->authenticator_ie, + hs->wpa_ie)) goto error_ie_different; + oci = handshake_util_find_kde(HANDSHAKE_KDE_OCI, decrypted_key_data, + decrypted_key_data_size, &oci_len); + if (hs->akm_suite & (IE_RSN_AKM_SUITE_FT_OVER_8021X | IE_RSN_AKM_SUITE_FT_USING_PSK | IE_RSN_AKM_SUITE_FT_OVER_SAE_SHA256)) { - struct ie_rsn_info ie_info; - - if (ie_parse_rsne_from_data(rsne, rsne[1] + 2, &ie_info) < 0) - goto error_ie_different; - - if (ie_info.num_pmkids != 1 || memcmp(ie_info.pmkids, + if (rsn_info.num_pmkids != 1 || memcmp(rsn_info.pmkids, hs->pmk_r1_name, 16)) goto error_ie_different; @@ -1713,6 +1767,19 @@ static void eapol_handle_ptk_3_of_4(struct eapol_sm *sm, goto error_ie_different; /* + * 802.11-2020, Section 12.7.6.4 + * If dot11RSNAOperatingChannelValidationActivated is true and + * Authenticator RSNE indicates OCVC capability, the Supplicant + * silently discards message 3 if any of the following are true: + * - OCI KDE or FTE OCI subelement is missing in the message + * - Channel information in the OCI does not match current operating + * channel parameters (see 12.2.9) + */ + if (hs->authenticator_ocvc && + handshake_state_verify_oci(hs, oci, oci_len) < 0) + return; + + /* * If ptk_complete is set, then we are receiving Message 3 again. * It must be a retransmission, otherwise the anonce wouldn't match * and we wouldn't get here. Skip processing the rest of the message @@ -1727,13 +1794,9 @@ static void eapol_handle_ptk_3_of_4(struct eapol_sm *sm, * RSNE or deauthenticates." */ if (optional_rsne) { - struct ie_rsn_info info1; struct ie_rsn_info info2; uint16_t override; - if (ie_parse_rsne_from_data(rsne, rsne[1] + 2, &info1) < 0) - goto error_ie_different; - if (ie_parse_rsne_from_data(optional_rsne, optional_rsne[1] + 2, &info2) < 0) goto error_ie_different; @@ -1757,14 +1820,14 @@ static void eapol_handle_ptk_3_of_4(struct eapol_sm *sm, * and rsne2 * - Check that rsne2 pairwise_ciphers is a subset of rsne */ - if (info1.akm_suites != info2.akm_suites || - info1.group_cipher != info2.group_cipher) + if (rsn_info.akm_suites != info2.akm_suites || + rsn_info.group_cipher != info2.group_cipher) goto error_ie_different; override = info2.pairwise_ciphers; - if (override == info1.pairwise_ciphers || - !(info1.pairwise_ciphers & override) || + if (override == rsn_info.pairwise_ciphers || + !(rsn_info.pairwise_ciphers & override) || __builtin_popcount(override) != 1) { handshake_failed(sm, MMPDU_REASON_CODE_INVALID_PAIRWISE_CIPHER); @@ -1789,8 +1852,7 @@ static void eapol_handle_ptk_3_of_4(struct eapol_sm *sm, gtk_key_index = bit_field(gtk[0], 0, 2); gtk += 2; gtk_len -= 2; - } else - gtk = NULL; + } if (hs->mfp) { igtk = handshake_util_find_igtk_kde(decrypted_key_data, @@ -1804,8 +1866,45 @@ static void eapol_handle_ptk_3_of_4(struct eapol_sm *sm, igtk_key_index = l_get_le16(igtk); igtk += 2; igtk_len -= 2; - } else - igtk = NULL; + } + + key_id = handshake_util_find_kde(HANDSHAKE_KDE_KEY_ID, + decrypted_key_data, + decrypted_key_data_size, &key_id_len); + if (hs->ext_key_id_capable) { + uint8_t idx; + + if (!key_id) { + l_debug("No extended key KDE in frame 3/4"); + handshake_failed(sm, MMPDU_REASON_CODE_INVALID_IE); + return; + } + + if (key_id_len != 2) { + l_error("invalid Key ID KDE format"); + handshake_failed(sm, MMPDU_REASON_CODE_INVALID_IE); + return; + } + + idx = bit_field(key_id[0], 0, 2); + + /* + * IEEE 802.11-2020 - 12.7.6.4 4-way handshake message 3 + * "... the Authenticator assigns a new Key ID for the PTKSA in + * the range of 0 to 1 that is different from the Key ID + * assigned in the previous handshake" + */ + if ((idx != 0 && idx != 1) || (sm->rekey && + idx == hs->active_tk_index)) { + l_error("invalid Key ID KDE value (%u)", idx); + handshake_failed(sm, MMPDU_REASON_CODE_INVALID_IE); + return; + } + + hs->active_tk_index = idx; + + l_debug("using Extended key ID %u", hs->active_tk_index); + } if (hs->support_ip_allocation) { size_t len; @@ -1868,7 +1967,6 @@ retransmit: if (!eapol_calculate_mic(hs->akm_suite, kck, step4, mic, sm->mic_len)) { l_debug("MIC Calculation failed"); - l_free(step4); handshake_failed(sm, MMPDU_REASON_CODE_UNSPECIFIED); return; } @@ -1879,18 +1977,11 @@ retransmit: handshake_state_get_kek_len(hs), step4, NULL, 0)) { l_debug("AES-SIV encryption failed"); - l_free(step4); handshake_failed(sm, MMPDU_REASON_CODE_UNSPECIFIED); return; } } - eapol_sm_write(sm, (struct eapol_frame *) step4, unencrypted); - l_free(step4); - - if (hs->ptk_complete) - return; - /* * For WPA1 the group handshake should be happening after we set the * ptk, this flag tells netdev to wait for the gtk/igtk before @@ -1905,6 +1996,24 @@ retransmit: if (igtk) eapol_install_igtk(sm, igtk_key_index, igtk, igtk_len); + /* + * Only install if this is the first 3/4 message (not retransmitting) + * and a rekey. Initial associations don't need the special RX -> TX + * procedure and can install the TK normally + */ + if (key_id && hs->ext_key_id_capable && sm->rekey) { + handshake_state_install_ext_ptk(hs, hs->active_tk_index, + (struct eapol_frame *) step4, + ETH_P_PAE, unencrypted); + + return; + } + + eapol_sm_write(sm, (struct eapol_frame *) step4, unencrypted); + + if (hs->ptk_complete) + return; + handshake_state_install_ptk(hs); if (rekey_offload) @@ -1960,6 +2069,7 @@ static void eapol_handle_gtk_1_of_2(struct eapol_sm *sm, size_t decrypted_key_data_size, bool unencrypted) { + struct handshake_state *hs = sm->handshake; const uint8_t *kck; struct eapol_key *step2; uint8_t mic[MIC_MAXLEN]; @@ -1969,13 +2079,35 @@ static void eapol_handle_gtk_1_of_2(struct eapol_sm *sm, const uint8_t *igtk; size_t igtk_len; uint16_t igtk_key_index; + const uint8_t *oci; + size_t oci_len; + uint8_t ies[1024]; + size_t ies_len = 0; + + l_debug("ifindex=%u", hs->ifindex); - if (!eapol_verify_gtk_1_of_2(ek, sm->handshake->wpa_ie, sm->mic_len)) { + if (!eapol_verify_gtk_1_of_2(ek, hs->wpa_ie, sm->mic_len)) { handshake_failed(sm, MMPDU_REASON_CODE_UNSPECIFIED); return; } - if (!sm->handshake->wpa_ie) { + oci = handshake_util_find_kde(HANDSHAKE_KDE_OCI, decrypted_key_data, + decrypted_key_data_size, &oci_len); + + /* + * 802.11-2020, Section 12.7.2.2 + * If dot11RSNAOperatingChannelValidationActivated is true and + * Authenticator RSNE indicates OCVC capability, the Supplicant + * silently discards message 1 if any of the following are true: + * - OCI KDE is missing in the message + * - Channel information in the OCI KDE does not match current + * operating channel parameters (see 12.2.9) + */ + if (hs->authenticator_ocvc && + handshake_state_verify_oci(hs, oci, oci_len) < 0) + return; + + if (!hs->wpa_ie) { gtk = handshake_util_find_gtk_kde(decrypted_key_data, decrypted_key_data_size, &gtk_len); @@ -1996,7 +2128,7 @@ static void eapol_handle_gtk_1_of_2(struct eapol_sm *sm, gtk_key_index = ek->wpa_key_id; } - if (sm->handshake->mfp) { + if (hs->mfp) { igtk = handshake_util_find_igtk_kde(decrypted_key_data, decrypted_key_data_size, &igtk_len); @@ -2010,6 +2142,18 @@ static void eapol_handle_gtk_1_of_2(struct eapol_sm *sm, igtk = NULL; /* + * IEEE 802.11-2020 Section 12.7.7.3 + * "Key Data = OCI KDE when dot11RSNAOperatingChannelValidationActivated + * on the [Supplicant]" + * + * Note: The spec reads "Authenticator" but this is incorrect and + * appears to be a copy-paste from a previous section. Above it has been + * changed to Supplicant. + */ + if (sm->handshake->supplicant_ocvc && sm->handshake->chandef) + ies_len += append_oci(ies + ies_len, sm->handshake->chandef); + + /* * 802.11-2016, Section 12.7.7.2: * " * a) Verifies that the Key Replay Counter field value has not yet been @@ -2030,14 +2174,15 @@ static void eapol_handle_gtk_1_of_2(struct eapol_sm *sm, step2 = eapol_create_gtk_2_of_2(sm->protocol_version, ek->key_descriptor_version, sm->replay_counter, - sm->handshake->wpa_ie, ek->wpa_key_id, + ies_len, ies, + hs->wpa_ie, ek->wpa_key_id, sm->mic_len); - kck = handshake_state_get_kck(sm->handshake); + kck = handshake_state_get_kck(hs); if (sm->mic_len) { - if (!eapol_calculate_mic(sm->handshake->akm_suite, kck, - step2, mic, sm->mic_len)) { + if (!eapol_calculate_mic(hs->akm_suite, kck, + step2, mic, sm->mic_len)) { l_debug("MIC calculation failed"); l_free(step2); handshake_failed(sm, MMPDU_REASON_CODE_UNSPECIFIED); @@ -2046,10 +2191,9 @@ static void eapol_handle_gtk_1_of_2(struct eapol_sm *sm, memcpy(EAPOL_KEY_MIC(step2), mic, sm->mic_len); } else { - if (!eapol_aes_siv_encrypt( - handshake_state_get_kek(sm->handshake), - handshake_state_get_kek_len(sm->handshake), - step2, NULL, 0)) { + if (!eapol_aes_siv_encrypt(handshake_state_get_kek(hs), + handshake_state_get_kek_len(hs), + step2, NULL, 0)) { l_debug("AES-SIV encryption failed"); l_free(step2); handshake_failed(sm, MMPDU_REASON_CODE_UNSPECIFIED); diff --git a/src/eapol.h b/src/eapol.h @@ -95,6 +95,8 @@ struct eapol_key *eapol_create_gtk_2_of_2( enum eapol_protocol_version protocol, enum eapol_key_descriptor_version version, uint64_t key_replay_counter, + size_t extra_len, + const uint8_t *extra_data, bool is_wpa, uint8_t wpa_key_id, size_t mic_len); diff --git a/src/fils.c b/src/fils.c @@ -37,6 +37,7 @@ #include "src/missing.h" #include "src/erp.h" #include "src/auth-proto.h" +#include "src/band.h" #define FILS_NONCE_LEN 16 #define FILS_SESSION_LEN 8 @@ -49,6 +50,7 @@ struct fils_sm { fils_tx_authenticate_func_t auth; fils_tx_associate_func_t assoc; + fils_get_oci_func_t get_oci; uint8_t nonce[FILS_NONCE_LEN]; uint8_t anonce[FILS_NONCE_LEN]; @@ -148,12 +150,13 @@ static int fils_derive_key_data(struct fils_sm *fils) uint8_t data[44]; uint8_t *ptr = data; size_t hash_len; - struct iovec iov[4]; + struct iovec iov[5]; size_t iov_elems = 0; size_t fils_ft_len = 0; bool sha384; size_t ie_len; uint8_t *rsne = NULL; + uint8_t oci[6]; rmsk = erp_get_rmsk(fils->erp, &rmsk_len); @@ -302,6 +305,23 @@ static int fils_derive_key_data(struct fils_sm *fils) iov_elems += 1; } + /* + * IEEE 802.11 Section 12.11.2.6.2 + * "If dot11RSNAOperatingChannelValidationActivated is true and AP + * indicates OCVC capability, the STA shall include OCI element in the + * request" + */ + if (fils->hs->supplicant_ocvc && fils->hs->chandef) { + oci[0] = IE_TYPE_EXTENSION; + oci[1] = 4; + oci[2] = IE_TYPE_OCI & 0xff; + oci_from_chandef(fils->hs->chandef, oci + 3); + + iov[iov_elems].iov_base = oci; + iov[iov_elems].iov_len = 6; + iov_elems++; + } + memcpy(data, fils->nonce, sizeof(fils->nonce)); memcpy(data + sizeof(fils->nonce), fils->anonce, sizeof(fils->anonce)); @@ -436,12 +456,20 @@ static int fils_rx_authenticate(struct auth_proto *driver, const uint8_t *frame, if (erp_rx_packet(fils->erp, wrapped, wrapped_len) < 0) goto invalid_ies; - return fils_derive_key_data(fils); + return fils->get_oci(fils->user_data); + invalid_ies: return MMPDU_STATUS_CODE_INVALID_ELEMENT; } +static int fils_rx_oci(struct auth_proto *driver) +{ + struct fils_sm *fils = l_container_of(driver, struct fils_sm, ap); + + return fils_derive_key_data(fils); +} + static int fils_rx_associate(struct auth_proto *driver, const uint8_t *frame, size_t len) { @@ -564,6 +592,7 @@ invalid_ies: struct auth_proto *fils_sm_new(struct handshake_state *hs, fils_tx_authenticate_func_t auth, fils_tx_associate_func_t assoc, + fils_get_oci_func_t get_oci, void *user_data) { struct fils_sm *fils; @@ -572,11 +601,13 @@ struct auth_proto *fils_sm_new(struct handshake_state *hs, fils->auth = auth; fils->assoc = assoc; + fils->get_oci = get_oci; fils->user_data = user_data; fils->hs = hs; fils->ap.start = fils_start; fils->ap.free = fils_free; + fils->ap.rx_oci = fils_rx_oci; fils->ap.rx_authenticate = fils_rx_authenticate; fils->ap.rx_associate = fils_rx_associate; diff --git a/src/fils.h b/src/fils.h @@ -30,8 +30,10 @@ typedef void (*fils_tx_associate_func_t)(struct iovec *iov, size_t iov_len, const uint8_t *kek, size_t kek_len, const uint8_t *nonces, size_t nonces_len, void *user_data); +typedef int (*fils_get_oci_func_t)(void *user_data); struct auth_proto *fils_sm_new(struct handshake_state *hs, fils_tx_authenticate_func_t auth, fils_tx_associate_func_t assoc, + fils_get_oci_func_t get_oci, void *user_data); diff --git a/src/ft.c b/src/ft.c @@ -32,6 +32,7 @@ #include "src/ft.h" #include "src/mpdu.h" #include "src/auth-proto.h" +#include "src/band.h" struct ft_sm { struct auth_proto ap; @@ -39,8 +40,11 @@ struct ft_sm { ft_tx_authenticate_func_t tx_auth; ft_tx_associate_func_t tx_assoc; + ft_get_oci get_oci; void *user_data; + + bool over_ds : 1; }; /* @@ -234,6 +238,10 @@ static int ft_tx_reassociate(struct ft_sm *ft) rsn_info.num_pmkids = 1; rsn_info.pmkids = hs->pmk_r1_name; + /* Always set OCVC false for FT-over-DS */ + if (ft->over_ds) + rsn_info.ocvc = false; + rsne = alloca(256); ie_build_rsne(&rsn_info, rsne); @@ -273,6 +281,22 @@ static int ft_tx_reassociate(struct ft_sm *ft) memcpy(ft_info.anonce, hs->anonce, 32); memcpy(ft_info.snonce, hs->snonce, 32); + /* + * IEEE 802.11-2020 Section 13.7.1 FT reassociation in an RSN + * + * "If dot11RSNAOperatingChannelValidationActivated is true and + * the FTO indicates OCVC capability, the target AP shall + * ensure that OCI subelement of the FTE matches by ensuring + * that all of the following are true: + * - OCI subelement is present + * - Channel information in the OCI matches current + * operating channel parameters (see 12.2.9)" + */ + if (hs->supplicant_ocvc && hs->chandef) { + oci_from_chandef(hs->chandef, ft_info.oci); + ft_info.oci_present = true; + } + fte = alloca(256); ie_build_fast_bss_transition(&ft_info, kck_len, fte); @@ -323,13 +347,14 @@ static bool ft_verify_rsne(const uint8_t *rsne, const uint8_t *pmk_r0_name, memcmp(msg2_rsne.pmkids, pmk_r0_name, 16)) return false; - if (!handshake_util_ap_ie_matches(rsne, authenticator_ie, false)) + if (!handshake_util_ap_ie_matches(&msg2_rsne, authenticator_ie, false)) return false; return true; } static int ft_parse_ies(struct handshake_state *hs, + const uint8_t *authenticator_ie, const uint8_t *ies, size_t ies_len, const uint8_t **mde_out, const uint8_t **fte_out) @@ -370,8 +395,7 @@ static int ft_parse_ies(struct handshake_state *hs, is_rsn = hs->supplicant_ie != NULL; if (is_rsn) { - if (!ft_verify_rsne(rsne, hs->pmk_r0_name, - hs->authenticator_ie)) + if (!ft_verify_rsne(rsne, hs->pmk_r0_name, authenticator_ie)) goto ft_error; } else if (rsne) goto ft_error; @@ -461,7 +485,8 @@ bool ft_over_ds_parse_action_ies(struct ft_ds_info *info, const uint8_t *fte = NULL; bool is_rsn = hs->supplicant_ie != NULL; - if (ft_parse_ies(hs, ies, ies_len, &mde, &fte) < 0) + if (ft_parse_ies(hs, info->authenticator_ie, ies, ies_len, + &mde, &fte) < 0) return false; if (!mde_equal(info->mde, mde)) @@ -492,10 +517,10 @@ static int ft_process_ies(struct handshake_state *hs, const uint8_t *ies, if (!ies) goto ft_error; - if (ft_parse_ies(hs, ies, ies_len, &mde, &fte) < 0) + if (ft_parse_ies(hs, hs->authenticator_ie, ies, ies_len, + &mde, &fte) < 0) goto ft_error; - if (!mde_equal(hs->mde, mde)) goto ft_error; @@ -593,6 +618,9 @@ void ft_ds_info_free(struct ft_ds_info *info) if (info->fte) l_free(info->fte); + if (info->authenticator_ie) + l_free(info->authenticator_ie); + if (destroy) destroy(info); } @@ -624,7 +652,7 @@ static int ft_rx_authenticate(struct auth_proto *ap, const uint8_t *frame, if (ret < 0) goto auth_error; - return ft_tx_reassociate(ft); + return ft->get_oci(ft->user_data); auth_error: return (int)status_code; @@ -674,7 +702,8 @@ static int ft_rx_associate(struct auth_proto *ap, const uint8_t *frame, memcmp(msg4_rsne.pmkids, hs->pmk_r1_name, 16)) return -EBADMSG; - if (!handshake_util_ap_ie_matches(rsne, hs->authenticator_ie, + if (!handshake_util_ap_ie_matches(&msg4_rsne, + hs->authenticator_ie, false)) return -EBADMSG; } else { @@ -766,6 +795,13 @@ static int ft_rx_associate(struct auth_proto *ap, const uint8_t *frame, return 0; } +static int ft_rx_oci(struct auth_proto *ap) +{ + struct ft_sm *ft = l_container_of(ap, struct ft_sm, ap); + + return ft_tx_reassociate(ft); +} + static void ft_sm_free(struct auth_proto *ap) { struct ft_sm *ft = l_container_of(ap, struct ft_sm, ap); @@ -780,7 +816,7 @@ static bool ft_over_ds_start(struct auth_proto *ap) return ft_tx_reassociate(ft) == 0; } -bool ft_build_authenticate_ies(struct handshake_state *hs, +bool ft_build_authenticate_ies(struct handshake_state *hs, bool ocvc, const uint8_t *new_snonce, uint8_t *buf, size_t *len) { @@ -809,6 +845,7 @@ bool ft_build_authenticate_ies(struct handshake_state *hs, rsn_info.num_pmkids = 1; rsn_info.pmkids = hs->pmk_r0_name; + rsn_info.ocvc = ocvc; ie_build_rsne(&rsn_info, ptr); ptr += ptr[1] + 2; @@ -859,7 +896,8 @@ static bool ft_start(struct auth_proto *ap) uint8_t buf[512]; size_t len; - if (!ft_build_authenticate_ies(hs, hs->snonce, buf, &len)) + if (!ft_build_authenticate_ies(hs, hs->supplicant_ocvc, hs->snonce, + buf, &len)) return false; iov.iov_base = buf; @@ -873,12 +911,14 @@ static bool ft_start(struct auth_proto *ap) struct auth_proto *ft_over_air_sm_new(struct handshake_state *hs, ft_tx_authenticate_func_t tx_auth, ft_tx_associate_func_t tx_assoc, + ft_get_oci get_oci, void *user_data) { struct ft_sm *ft = l_new(struct ft_sm, 1); ft->tx_auth = tx_auth; ft->tx_assoc = tx_assoc; + ft->get_oci = get_oci; ft->hs = hs; ft->user_data = user_data; @@ -886,6 +926,7 @@ struct auth_proto *ft_over_air_sm_new(struct handshake_state *hs, ft->ap.rx_associate = ft_rx_associate; ft->ap.start = ft_start; ft->ap.free = ft_sm_free; + ft->ap.rx_oci = ft_rx_oci; return &ft->ap; } @@ -899,6 +940,7 @@ struct auth_proto *ft_over_ds_sm_new(struct handshake_state *hs, ft->tx_assoc = tx_assoc; ft->hs = hs; ft->user_data = user_data; + ft->over_ds = true; ft->ap.rx_associate = ft_rx_associate; ft->ap.start = ft_over_ds_start; diff --git a/src/ft.h b/src/ft.h @@ -26,6 +26,7 @@ typedef void (*ft_tx_authenticate_func_t)(struct iovec *iov, size_t iov_len, void *user_data); typedef int (*ft_tx_associate_func_t)(struct iovec *ie_iov, size_t iov_len, void *user_data); +typedef int (*ft_get_oci)(void *user_data); typedef void (*ft_ds_free_func_t)(void *user_data); @@ -35,6 +36,7 @@ struct ft_ds_info { uint8_t snonce[32]; uint8_t mde[3]; uint8_t *fte; + uint8_t *authenticator_ie; struct ie_ft_info ft_info; @@ -43,7 +45,7 @@ struct ft_ds_info { void ft_ds_info_free(struct ft_ds_info *info); -bool ft_build_authenticate_ies(struct handshake_state *hs, +bool ft_build_authenticate_ies(struct handshake_state *hs, bool ocvc, const uint8_t *new_snonce, uint8_t *buf, size_t *len); @@ -60,6 +62,7 @@ bool ft_over_ds_parse_action_ies(struct ft_ds_info *info, struct auth_proto *ft_over_air_sm_new(struct handshake_state *hs, ft_tx_authenticate_func_t tx_auth, ft_tx_associate_func_t tx_assoc, + ft_get_oci get_oci, void *user_data); struct auth_proto *ft_over_ds_sm_new(struct handshake_state *hs, diff --git a/src/handshake.c b/src/handshake.c @@ -42,6 +42,7 @@ #include "src/util.h" #include "src/handshake.h" #include "src/erp.h" +#include "src/band.h" static inline unsigned int n_ecc_groups() { @@ -75,6 +76,7 @@ static handshake_get_nonce_func_t get_nonce = handshake_get_nonce; static handshake_install_tk_func_t install_tk = NULL; static handshake_install_gtk_func_t install_gtk = NULL; static handshake_install_igtk_func_t install_igtk = NULL; +static handshake_install_ext_tk_func_t install_ext_tk = NULL; void __handshake_set_get_nonce_func(handshake_get_nonce_func_t func) { @@ -96,6 +98,11 @@ void __handshake_set_install_igtk_func(handshake_install_igtk_func_t func) install_igtk = func; } +void __handshake_set_install_ext_tk_func(handshake_install_ext_tk_func_t func) +{ + install_ext_tk = func; +} + void handshake_state_free(struct handshake_state *s) { __typeof__(s->free) destroy = s->free; @@ -112,6 +119,8 @@ void handshake_state_free(struct handshake_state *s) if (s->erp_cache) erp_cache_put(s->erp_cache); + l_free(s->chandef); + if (s->passphrase) { explicit_bzero(s->passphrase, strlen(s->passphrase)); l_free(s->passphrase); @@ -170,83 +179,77 @@ void handshake_state_set_8021x_config(struct handshake_state *s, s->settings_8021x = settings; } -static bool handshake_state_setup_own_ciphers(struct handshake_state *s, - const struct ie_rsn_info *info) -{ - if (__builtin_popcount(info->akm_suites) != 1) - return false; - - s->akm_suite = info->akm_suites; - s->group_cipher = info->group_cipher; - s->group_management_cipher = info->group_management_cipher; - - /* - * Don't set MFP for OSEN otherwise EAPoL will attempt to negotiate a - * iGTK which is not allowed for OSEN. - */ - if (!s->osen_ie) - s->mfp = info->mfpc; - - return true; -} - bool handshake_state_set_authenticator_ie(struct handshake_state *s, const uint8_t *ie) { struct ie_rsn_info info; + if (!ie_parse_rsne_from_data(ie, ie[1] + 2, &info)) + goto valid_ie; + + if (!ie_parse_wpa_from_data(ie, ie[1] + 2, &info)) + goto valid_ie; + + if (ie_parse_osen_from_data(ie, ie[1] + 2, &info) < 0) + return false; + +valid_ie: l_free(s->authenticator_ie); s->authenticator_ie = l_memdup(ie, ie[1] + 2u); - s->wpa_ie = is_ie_wpa_ie(ie + 2, ie[1]); - s->osen_ie = is_ie_wfa_ie(ie + 2, ie[1], IE_WFA_OI_OSEN); - - if (!s->authenticator) - return true; - if (s->wpa_ie) { - if (ie_parse_wpa_from_data(ie, ie[1] + 2, &info) < 0) - return false; - } else if (s->osen_ie) { - if (ie_parse_osen_from_data(ie, ie[1] + 2, &info) < 0) - return false; - } else { - if (ie_parse_rsne_from_data(ie, ie[1] + 2, &info) < 0) - return false; - } + s->authenticator_ocvc = info.ocvc; - return handshake_state_setup_own_ciphers(s, &info); + return true; } bool handshake_state_set_supplicant_ie(struct handshake_state *s, const uint8_t *ie) { struct ie_rsn_info info; + bool wpa_ie = false; + bool osen_ie = false; - l_free(s->supplicant_ie); - s->supplicant_ie = l_memdup(ie, ie[1] + 2u); - s->wpa_ie = is_ie_wpa_ie(ie + 2, ie[1]); - s->osen_ie = is_ie_wfa_ie(ie + 2, ie[1], IE_WFA_OI_OSEN); + if (!ie_parse_rsne_from_data(ie, ie[1] + 2, &info)) + goto valid_ie; - if (s->wpa_ie) { - if (ie_parse_wpa_from_data(ie, ie[1] + 2, &info) < 0) - return false; - } else if (s->osen_ie) { - if (ie_parse_osen_from_data(ie, ie[1] + 2, &info) < 0) - return false; - } else { - if (ie_parse_rsne_from_data(ie, ie[1] + 2, &info) < 0) - return false; + if (!ie_parse_wpa_from_data(ie, ie[1] + 2, &info)) { + wpa_ie = true; + goto valid_ie; } + if (ie_parse_osen_from_data(ie, ie[1] + 2, &info) < 0) + return false; + + osen_ie = true; + +valid_ie: if (__builtin_popcount(info.pairwise_ciphers) != 1) return false; + if (__builtin_popcount(info.akm_suites) != 1) + return false; + + l_free(s->supplicant_ie); + s->supplicant_ie = l_memdup(ie, ie[1] + 2u); + + s->osen_ie = osen_ie; + s->wpa_ie = wpa_ie; + s->pairwise_cipher = info.pairwise_ciphers; + s->group_cipher = info.group_cipher; + s->group_management_cipher = info.group_management_cipher; + s->akm_suite = info.akm_suites; + s->supplicant_ocvc = info.ocvc; + s->ext_key_id_capable = info.extended_key_id; - if (s->authenticator) - return true; + /* + * Don't set MFP for OSEN otherwise EAPoL will attempt to negotiate a + * iGTK which is not allowed for OSEN. + */ + if (!s->osen_ie) + s->mfp = info.mfpc; - return handshake_state_setup_own_ciphers(s, &info); + return true; } static void replace_ie(uint8_t **old, const uint8_t *new) @@ -663,10 +666,27 @@ void handshake_state_install_ptk(struct handshake_state *s) handshake_event(s, HANDSHAKE_EVENT_SETTING_KEYS); - install_tk(s, handshake_get_tk(s), cipher); + install_tk(s, s->active_tk_index, handshake_get_tk(s), cipher); + } +} + +void handshake_state_install_ext_ptk(struct handshake_state *s, + uint8_t key_idx, + struct eapol_frame *ek, uint16_t proto, + bool noencrypt) +{ + s->ptk_complete = true; + + if (install_ext_tk) { + uint32_t cipher = + ie_rsn_cipher_suite_to_cipher(s->pairwise_cipher); + + install_ext_tk(s, key_idx, handshake_get_tk(s), cipher, ek, + proto, noencrypt); } } + void handshake_state_install_gtk(struct handshake_state *s, uint16_t gtk_key_index, const uint8_t *gtk, size_t gtk_len, @@ -759,87 +779,69 @@ void handshake_state_set_gtk(struct handshake_state *s, const uint8_t *key, * results vs the RSN/WPA IE obtained as part of the 4-way handshake. If they * don't match, the EAPoL packet must be silently discarded. */ -bool handshake_util_ap_ie_matches(const uint8_t *msg_ie, +bool handshake_util_ap_ie_matches(const struct ie_rsn_info *msg_info, const uint8_t *scan_ie, bool is_wpa) { - struct ie_rsn_info msg_info; struct ie_rsn_info scan_info; + int r; - /* - * First check that the sizes match, if they do, run a bitwise - * comparison. - */ - if (msg_ie[1] == scan_ie[1] && - !memcmp(msg_ie + 2, scan_ie + 2, msg_ie[1])) - return true; - - /* - * Otherwise we have to parse the IEs and compare the individual - * fields - */ - if (!is_wpa) { - if (ie_parse_rsne_from_data(msg_ie, msg_ie[1] + 2, - &msg_info) < 0) - return false; + if (!is_wpa) + r = ie_parse_rsne_from_data(scan_ie, + scan_ie[1] + 2, &scan_info); + else + r = ie_parse_wpa_from_data(scan_ie, scan_ie[1] + 2, &scan_info); - if (ie_parse_rsne_from_data(scan_ie, scan_ie[1] + 2, - &scan_info) < 0) - return false; - } else { - if (ie_parse_wpa_from_data(msg_ie, msg_ie[1] + 2, - &msg_info) < 0) - return false; + if (r < 0) + return false; - if (ie_parse_wpa_from_data(scan_ie, scan_ie[1] + 2, - &scan_info) < 0) - return false; - } + if (msg_info->group_cipher != scan_info.group_cipher) + return false; - if (msg_info.group_cipher != scan_info.group_cipher) + if (msg_info->pairwise_ciphers != scan_info.pairwise_ciphers) return false; - if (msg_info.pairwise_ciphers != scan_info.pairwise_ciphers) + if (msg_info->akm_suites != scan_info.akm_suites) return false; - if (msg_info.akm_suites != scan_info.akm_suites) + if (msg_info->preauthentication != scan_info.preauthentication) return false; - if (msg_info.preauthentication != scan_info.preauthentication) + if (msg_info->no_pairwise != scan_info.no_pairwise) return false; - if (msg_info.no_pairwise != scan_info.no_pairwise) + if (msg_info->ptksa_replay_counter != scan_info.ptksa_replay_counter) return false; - if (msg_info.ptksa_replay_counter != scan_info.ptksa_replay_counter) + if (msg_info->gtksa_replay_counter != scan_info.gtksa_replay_counter) return false; - if (msg_info.gtksa_replay_counter != scan_info.gtksa_replay_counter) + if (msg_info->mfpr != scan_info.mfpr) return false; - if (msg_info.mfpr != scan_info.mfpr) + if (msg_info->mfpc != scan_info.mfpc) return false; - if (msg_info.mfpc != scan_info.mfpc) + if (msg_info->peerkey_enabled != scan_info.peerkey_enabled) return false; - if (msg_info.peerkey_enabled != scan_info.peerkey_enabled) + if (msg_info->spp_a_msdu_capable != scan_info.spp_a_msdu_capable) return false; - if (msg_info.spp_a_msdu_capable != scan_info.spp_a_msdu_capable) + if (msg_info->spp_a_msdu_required != scan_info.spp_a_msdu_required) return false; - if (msg_info.spp_a_msdu_required != scan_info.spp_a_msdu_required) + if (msg_info->pbac != scan_info.pbac) return false; - if (msg_info.pbac != scan_info.pbac) + if (msg_info->extended_key_id != scan_info.extended_key_id) return false; - if (msg_info.extended_key_id != scan_info.extended_key_id) + if (msg_info->ocvc != scan_info.ocvc) return false; /* We don't check the PMKIDs since these might actually be different */ - if (msg_info.group_management_cipher != + if (msg_info->group_management_cipher != scan_info.group_management_cipher) return false; @@ -1042,3 +1044,54 @@ bool handshake_state_add_ecc_sae_pt(struct handshake_state *s, s->ecc_sae_pts[i] = l_ecc_point_clone(pt); return true; } + +void handshake_state_set_chandef(struct handshake_state *s, + struct band_chandef *chandef) +{ + if (s->chandef) + l_free(s->chandef); + + s->chandef = chandef; +} + +int handshake_state_verify_oci(struct handshake_state *s, const uint8_t *oci, + size_t oci_len) +{ + int r = -ENOENT; + bool ocvc; + + l_debug("oci_len: %zu", oci ? oci_len : 0); + + if (!oci) + goto done; + + r = -EBADMSG; + if (oci_len != 3) + goto done; + + l_debug("operating_class: %hu", oci[0]); + l_debug("primary_channel_number: %hu", oci[1]); + l_debug("frequency segment 1 channel number: %hu", oci[2]); + + r = -EINVAL; + + if (!s->chandef) { + l_debug("Own chandef unavailable"); + goto done; + } + + r = oci_verify(oci, s->chandef); + if (r < 0) + l_debug("OCI verification failed: %s", strerror(-r)); + +done: + if (!r) + return r; + + /* Only enforce validation if we're configured to do so */ + ocvc = s->authenticator ? s->authenticator_ocvc : s->supplicant_ocvc; + if (!ocvc) + r = 0; + + return r; +} diff --git a/src/handshake.h b/src/handshake.h @@ -27,13 +27,13 @@ struct handshake_state; enum crypto_cipher; +struct eapol_frame; enum handshake_kde { - /* 802.11-2016 Table 12-6 in section 12.7.2 */ + /* 802.11-2020 Table 12-9 in section 12.7.2 */ HANDSHAKE_KDE_GTK = 0x000fac01, HANDSHAKE_KDE_MAC_ADDRESS = 0x000fac03, HANDSHAKE_KDE_PMKID = 0x000fac04, - HANDSHAKE_KDE_SMK = 0x000fac05, HANDSHAKE_KDE_NONCE = 0x000fac06, HANDSHAKE_KDE_LIFETIME = 0x000fac07, HANDSHAKE_KDE_ERROR = 0x000fac08, @@ -41,6 +41,8 @@ enum handshake_kde { HANDSHAKE_KDE_KEY_ID = 0x000fac0a, HANDSHAKE_KDE_MULTIBAND_GTK = 0x000fac0b, HANDSHAKE_KDE_MULTIBAND_KEY_ID = 0x000fac0c, + HANDSHAKE_KDE_OCI = 0x000fac0d, + HANDSHAKE_KDE_BIGTK = 0x000fac0e, /* Wi-Fi P2P Technical Specification v1.7 4.2.8 */ HANDSHAKE_KDE_IP_ADDRESS_REQ = 0x506f9a04, HANDSHAKE_KDE_IP_ADDRESS_ALLOC = 0x506f9a05, @@ -66,6 +68,7 @@ typedef void (*handshake_event_func_t)(struct handshake_state *hs, typedef bool (*handshake_get_nonce_func_t)(uint8_t nonce[]); typedef void (*handshake_install_tk_func_t)(struct handshake_state *hs, + uint8_t key_index, const uint8_t *tk, uint32_t cipher); typedef void (*handshake_install_gtk_func_t)(struct handshake_state *hs, uint16_t key_index, @@ -77,11 +80,17 @@ typedef void (*handshake_install_igtk_func_t)(struct handshake_state *hs, const uint8_t *igtk, uint8_t igtk_len, const uint8_t *ipn, uint8_t ipn_len, uint32_t cipher); +typedef void (*handshake_install_ext_tk_func_t)(struct handshake_state *hs, + uint8_t key_idx, const uint8_t *tk, + uint32_t cipher, + const struct eapol_frame *step4, + uint16_t proto, bool noencrypt); void __handshake_set_get_nonce_func(handshake_get_nonce_func_t func); void __handshake_set_install_tk_func(handshake_install_tk_func_t func); void __handshake_set_install_gtk_func(handshake_install_gtk_func_t func); void __handshake_set_install_igtk_func(handshake_install_igtk_func_t func); +void __handshake_set_install_ext_tk_func(handshake_install_ext_tk_func_t func); struct handshake_state { uint32_t ifindex; @@ -125,6 +134,9 @@ struct handshake_state { bool wait_for_gtk : 1; bool no_rekey : 1; bool support_fils : 1; + bool authenticator_ocvc : 1; + bool supplicant_ocvc : 1; + bool ext_key_id_capable : 1; uint8_t ssid[32]; size_t ssid_len; char *passphrase; @@ -135,6 +147,7 @@ struct handshake_state { uint8_t gtk_rsc[6]; uint8_t proto_version : 2; unsigned int gtk_index; + uint8_t active_tk_index; struct erp_cache_entry *erp_cache; bool support_ip_allocation : 1; uint32_t client_ip_addr; @@ -142,6 +155,7 @@ struct handshake_state { uint32_t go_ip_addr; uint8_t *fils_ip_req_ie; uint8_t *fils_ip_resp_ie; + struct band_chandef *chandef; void *user_data; void (*free)(struct handshake_state *s); @@ -220,6 +234,11 @@ size_t handshake_state_get_kek_len(struct handshake_state *s); const uint8_t *handshake_state_get_kek(struct handshake_state *s); void handshake_state_install_ptk(struct handshake_state *s); +void handshake_state_install_ext_ptk(struct handshake_state *s, + uint8_t key_idx, + struct eapol_frame *ek, uint16_t proto, + bool noencrypt); + void handshake_state_install_gtk(struct handshake_state *s, uint16_t gtk_key_index, const uint8_t *gtk, size_t gtk_len, @@ -241,7 +260,12 @@ bool handshake_decode_fte_key(struct handshake_state *s, const uint8_t *wrapped, void handshake_state_set_gtk(struct handshake_state *s, const uint8_t *key, unsigned int key_index, const uint8_t *rsc); -bool handshake_util_ap_ie_matches(const uint8_t *msg_ie, +void handshake_state_set_chandef(struct handshake_state *s, + struct band_chandef *chandef); +int handshake_state_verify_oci(struct handshake_state *s, const uint8_t *oci, + size_t oci_len); + +bool handshake_util_ap_ie_matches(const struct ie_rsn_info *msg_info, const uint8_t *scan_ie, bool is_wpa); const uint8_t *handshake_util_find_kde(enum handshake_kde selector, diff --git a/src/ie.c b/src/ie.c @@ -739,6 +739,7 @@ static int parse_ciphers(const uint8_t *data, size_t len, out_info->spp_a_msdu_required = test_bit(data + 1, 3); out_info->pbac = test_bit(data + 1, 4); out_info->extended_key_id = test_bit(data + 1, 5); + out_info->ocvc = test_bit(data + 1, 6); /* * BIP-default group management cipher suite in an RSNA with @@ -1094,6 +1095,9 @@ static int build_ciphers_common(const struct ie_rsn_info *info, uint8_t *to, if (info->extended_key_id) to[pos] |= 0x20; + if (info->ocvc) + to[pos] |= 0x40; + pos += 1; /* Short hand the generated RSNE if possible */ @@ -1343,6 +1347,8 @@ bool is_ie_wfa_ie(const uint8_t *data, uint8_t len, uint8_t oi_type) return false; else if (oi_type == IE_WFA_OI_HS20_INDICATION && len != 5 && len != 7) return false; + else if (oi_type == IE_WFA_OI_OWE_TRANSITION && len < 12) + return false; else if (len < 4) /* OI not handled, but at least check length */ return false; @@ -1726,6 +1732,7 @@ int ie_parse_fast_bss_transition(struct ie_tlv_iter *iter, uint32_t mic_len, memset(info, 0, sizeof(*info)); + info->rsnxe_used = test_bit(data, 0); info->mic_element_count = data[1]; memcpy(info->mic, data + 2, mic_len); @@ -1863,6 +1870,13 @@ bool ie_build_fast_bss_transition(const struct ie_ft_info *info, L_WARN_ON(info->igtk_len); /* Not implemented */ + if (info->oci_present) { + to[0] = 5; + to[1] = 3; + memcpy(to + 2, info->oci, sizeof(info->oci)); + *len += 5; + } + return true; } @@ -2492,3 +2506,69 @@ int ie_parse_network_cost(const void *data, size_t len, *flags = l_get_le16(ie + 8); return 0; } + +int ie_parse_owe_transition(const void *data, size_t len, + struct ie_owe_transition_info *info) +{ + const uint8_t *ie = data; + const uint8_t *bssid; + const uint8_t *ssid; + uint8_t oper_class = 0; + uint8_t channel = 0; + size_t slen; + + if (len < 14 || ie[0] != IE_TYPE_VENDOR_SPECIFIC) + return -ENOMSG; + + if (!is_ie_wfa_ie(ie + 2, len - 2, IE_WFA_OI_OWE_TRANSITION)) + return -ENOMSG; + + slen = l_get_u8(ie + 12); + if (slen > 32) + return -ENOMSG; + + /* + * WFA OWE Specification 2.3.1 + * + * "Band Info and Channel Info are optional fields. If configured, + * both fields shall be included in an OWE Transition Mode element" + */ + if (len != slen + 13 && len != slen + 15) + return -ENOMSG; + + bssid = ie + 6; + ssid = ie + 13; + + if (len == slen + 15) { + oper_class = l_get_u8(ie + 13 + slen); + channel = l_get_u8(ie + 14 + slen); + } + + memcpy(info->bssid, bssid, 6); + memcpy(info->ssid, ssid, slen); + info->ssid_len = slen; + info->oper_class = oper_class; + info->channel = channel; + + return 0; +} + +int ie_parse_oci(const void *data, size_t len, const uint8_t **oci) +{ + struct ie_tlv_iter iter; + + ie_tlv_iter_init(&iter, data, len); + + if (!ie_tlv_iter_next(&iter)) + return -EMSGSIZE; + + if (ie_tlv_iter_get_length(&iter) != 3) + return -EMSGSIZE; + + if (ie_tlv_iter_get_tag(&iter) != IE_TYPE_OCI) + return -EPROTOTYPE; + + *oci = ie_tlv_iter_get_data(&iter); + + return 0; +} diff --git a/src/ie.h b/src/ie.h @@ -290,6 +290,7 @@ enum ie_type { enum ie_vendor_wfa_oi_type { IE_WFA_OI_HS20_INDICATION = 0x10, IE_WFA_OI_OSEN = 0x12, + IE_WFA_OI_OWE_TRANSITION = 0x1c, IE_WFA_OI_ROAMING_SELECTION = 0x1d, }; @@ -393,6 +394,7 @@ struct ie_rsn_info { bool spp_a_msdu_required:1; bool pbac:1; bool extended_key_id:1; + bool ocvc:1; uint8_t num_pmkids; const uint8_t *pmkids; enum ie_rsn_cipher_suite group_management_cipher; @@ -417,6 +419,7 @@ enum ie_bss_capability { struct ie_ft_info { uint8_t mic_element_count; + bool rsnxe_used : 1; uint8_t mic[24]; uint8_t anonce[32]; uint8_t snonce[32]; @@ -432,6 +435,8 @@ struct ie_ft_info { uint8_t igtk_ipn[6]; uint8_t igtk_len; uint8_t igtk[24]; + bool oci_present:1; + uint8_t oci[3]; }; /* See chapter 8.4.2.47 for radio measurement capability details */ @@ -486,6 +491,14 @@ struct ie_fils_ip_addr_response_info { uint8_t ipv6_lifetime; /* Zero if not provided */ }; +struct ie_owe_transition_info { + uint8_t bssid[6]; + uint8_t ssid[32]; + size_t ssid_len; + uint8_t oper_class; + uint8_t channel; +}; + extern const unsigned char ieee_oui[3]; extern const unsigned char microsoft_oui[3]; extern const unsigned char wifi_alliance_oui[3]; @@ -629,3 +642,8 @@ void ie_build_fils_ip_addr_response( int ie_parse_network_cost(const void *data, size_t len, uint16_t *flags, uint16_t *level); + +int ie_parse_owe_transition(const void *data, size_t len, + struct ie_owe_transition_info *info); + +int ie_parse_oci(const void *data, size_t len, const uint8_t **oci); diff --git a/src/ip-pool.c b/src/ip-pool.c @@ -34,6 +34,7 @@ #include "src/iwd.h" #include "src/module.h" #include "src/netdev.h" +#include "src/netconfig.h" #include "src/ip-pool.h" struct ip_pool_addr4_record { @@ -357,15 +358,7 @@ static void ip_pool_addr4_dump_cb(int error, static int ip_pool_init(void) { - const struct l_settings *settings = iwd_get_config(); - bool netconfig_enabled; - - if (!l_settings_get_bool(settings, "General", - "EnableNetworkConfiguration", - &netconfig_enabled)) - netconfig_enabled = false; - - if (!netconfig_enabled) + if (!netconfig_enabled()) return 0; rtnl = iwd_get_rtnl(); diff --git a/src/iwd.config.rst b/src/iwd.config.rst @@ -181,6 +181,13 @@ The group ``[General]`` contains general settings. off by default. If you want to easily utilize Hotspot 2.0 networks, then setting ``DisableANQP`` to ``false`` is recommended. + * - DisableOCV + - Value: **false**, true + + Disable Operating Channel Validation. Support for this is not advertised + by the kernel so if kernels/drivers exist which don't support OCV it can + be disabled here. + Network ------- diff --git a/src/main.c b/src/main.c @@ -31,6 +31,8 @@ #include <signal.h> #include <ell/ell.h> +#include <ell/useful.h> + #include "linux/nl80211.h" #include "src/iwd.h" @@ -42,6 +44,7 @@ #include "src/rfkill.h" #include "src/storage.h" #include "src/anqp.h" +#include "src/netconfig.h" #include "src/backtrace.h" @@ -217,28 +220,15 @@ static struct l_dbus_message *iwd_dbus_get_info(struct l_dbus *dbus, struct l_dbus_message *message, void *user_data) { - struct l_dbus_message *reply; - struct l_dbus_message_builder *builder; - L_AUTO_FREE_VAR(char *, storage_dir) = storage_get_path(NULL); - bool netconfig_enabled; - - if (!l_settings_get_bool(iwd_config, "General", - "EnableNetworkConfiguration", - &netconfig_enabled)) - netconfig_enabled = false; - - reply = l_dbus_message_new_method_return(message); - builder = l_dbus_message_builder_new(reply); - l_dbus_message_builder_enter_array(builder, "{sv}"); - - dbus_append_dict_basic(builder, "NetworkConfigurationEnabled", 'b', - &netconfig_enabled); - dbus_append_dict_basic(builder, "StateDirectory", 's', storage_dir); - dbus_append_dict_basic(builder, "Version", 's', VERSION); - - l_dbus_message_builder_leave_array(builder); - l_dbus_message_builder_finalize(builder); - l_dbus_message_builder_destroy(builder); + struct l_dbus_message *reply = + l_dbus_message_new_method_return(message); + _auto_(l_free) char *storage_dir = storage_get_path(NULL); + + l_dbus_message_set_arguments(reply, "a{sv}", 3, + "NetworkConfigurationEnabled", + "b", netconfig_enabled(), + "StateDirectory", "s", storage_dir, + "Version", "s", VERSION); return reply; } diff --git a/src/manager.c b/src/manager.c @@ -63,36 +63,17 @@ struct wiphy_setup_state { * interface. */ bool use_default; - struct l_genl_msg *default_if_msg; + struct l_queue *default_interfaces; }; static struct l_queue *pending_wiphys; -/* With these drivers don't even try creating our interfaces */ -static const char *default_if_driver_list[] = { - /* - * The out-of-tree rtl88x2bu crashes the kernel hard. Seemingly - * many other drivers are built from the same source code so - * blacklist all of them. Unfortunately there are in-tree drivers - * that also match these names and may be fine. Use - * UseDefaultInterface to override. - */ - "rtl81*", - "rtl87*", - "rtl88*", - "rtw_*", - "brcmfmac", - "bcmsdh_sdmmc", - - NULL, -}; - static void wiphy_setup_state_free(void *data) { struct wiphy_setup_state *state = data; - if (state->default_if_msg) - l_genl_msg_unref(state->default_if_msg); + l_queue_destroy(state->default_interfaces, + (l_queue_destroy_func_t) l_genl_msg_unref); L_WARN_ON(state->pending_cmd_count); l_free(state); @@ -108,22 +89,30 @@ static bool manager_use_default(struct wiphy_setup_state *state) { uint8_t addr_buf[6]; uint8_t *addr = NULL; + const struct l_queue_entry *entry; l_debug(""); - if (!state->default_if_msg) { + if (!state->default_interfaces) { l_error("No default interface for wiphy %u", (unsigned int) state->id); state->retry = true; return false; } - if (randomize) { - wiphy_generate_random_address(state->wiphy, addr_buf); - addr = addr_buf; + entry = l_queue_get_entries(state->default_interfaces); + while (entry) { + struct l_genl_msg *msg = entry->data; + + if (randomize) { + wiphy_generate_random_address(state->wiphy, addr_buf); + addr = addr_buf; + } + + netdev_create_from_genl(msg, addr); + entry = entry->next; } - netdev_create_from_genl(state->default_if_msg, addr); return true; } @@ -407,10 +396,14 @@ static void manager_get_interface_cb(struct l_genl_msg *msg, void *user_data) if ((iftype == NL80211_IFTYPE_ADHOC || iftype == NL80211_IFTYPE_STATION || iftype == NL80211_IFTYPE_AP) && - !state->default_if_msg && (!whitelist_filter || whitelisted) && - !blacklisted) - state->default_if_msg = l_genl_msg_ref(msg); + !blacklisted) { + if (!state->default_interfaces) + state->default_interfaces = l_queue_new(); + + l_queue_push_head(state->default_interfaces, + l_genl_msg_ref(msg)); + } delete_interface: if (state->use_default) @@ -579,18 +572,9 @@ static void manager_wiphy_dump_done(void *user_data) if (whitelist_filter || blacklist_filter) state->use_default = true; - if (!state->use_default) { - const char *driver = wiphy_get_driver(state->wiphy); - - if (driver) { - const char **e; - - for (e = default_if_driver_list; *e; e++) - if (fnmatch(*e, driver, 0) == 0) - state->use_default = true; - } else - state->use_default = true; - } + if (!state->use_default) + state->use_default = + wiphy_uses_default_if(state->wiphy); if (state->use_default) l_info("Wiphy %s will only use the default interface", diff --git a/src/missing.h b/src/missing.h @@ -37,3 +37,10 @@ _Pragma("GCC diagnostic ignored \"-Wstringop-overflow=\"") _Pragma("GCC diagnostic pop") } #endif + +#ifndef HAVE_REALLOCARRAY +static inline void *reallocarray(void *ptr, size_t nmemb, size_t size) +{ + return realloc(ptr, nmemb * size); +} +#endif diff --git a/src/netconfig.c b/src/netconfig.c @@ -58,9 +58,17 @@ struct netconfig { uint8_t rtm_protocol; uint8_t rtm_v6_protocol; struct l_rtnl_address *v4_address; + struct l_rtnl_address *v6_address; char **dns4_overrides; char **dns6_overrides; + char **dns4_list; + char **dns6_list; + char *mdns; struct ie_fils_ip_addr_response_info *fils_override; + char *v4_gateway_str; + char *v6_gateway_str; + char *v4_domain; + char **v6_domains; const struct l_settings *active_settings; @@ -74,6 +82,7 @@ struct netconfig { uint32_t addr4_add_cmd_id; uint32_t addr6_add_cmd_id; uint32_t route4_add_gateway_cmd_id; + uint32_t route6_add_cmd_id; }; static struct l_netlink *rtnl; @@ -125,6 +134,22 @@ static int sysfs_write_ipv6_setting(const char *ifname, const char *setting, return r; } +static void netconfig_free_settings(struct netconfig *netconfig) +{ + l_rtnl_address_free(netconfig->v4_address); + netconfig->v4_address = NULL; + l_rtnl_address_free(netconfig->v6_address); + netconfig->v6_address = NULL; + + l_strfreev(netconfig->dns4_overrides); + netconfig->dns4_overrides = NULL; + l_strfreev(netconfig->dns6_overrides); + netconfig->dns6_overrides = NULL; + + l_free(netconfig->mdns); + netconfig->mdns = NULL; +} + static void netconfig_free(void *data) { struct netconfig *netconfig = data; @@ -152,20 +177,6 @@ static struct netconfig *netconfig_find(uint32_t ifindex) return NULL; } -#define APPEND_STRDUPV(dest, index, src) \ - do { \ - char **p; \ - for (p = src; p && *p; p++) \ - dest[index++] = l_strdup(*p); \ - } while (0) \ - -#define APPENDV(dest, index, src) \ - do { \ - char **p; \ - for (p = src; p && *p; p++) \ - dest[index++] = *p; \ - } while (0) \ - static inline char *netconfig_ipv4_to_string(uint32_t addr) { struct in_addr in_addr = { .s_addr = addr }; @@ -196,98 +207,136 @@ static inline char *netconfig_ipv6_to_string(const uint8_t *addr) return addr_str; } -static void netconfig_set_neighbor_entry_cb(int error, - uint16_t type, const void *data, - uint32_t len, void *user_data) +static char **netconfig_get_dns_list(struct netconfig *netconfig, int af, + const uint8_t **out_dns_mac) { - if (error) - l_error("l_rtnl_neighbor_set_hwaddr failed: %s (%i)", - strerror(-error), error); -} - -static int netconfig_set_dns(struct netconfig *netconfig) -{ - char **dns6_list = NULL; - char **dns4_list = NULL; - unsigned int n_entries = 0; - char **dns_list; - const uint8_t *fils_dns4_mac = NULL; - const uint8_t *fils_dns6_mac = NULL; const struct ie_fils_ip_addr_response_info *fils = netconfig->fils_override; - if (!netconfig->dns4_overrides && - netconfig->rtm_protocol == RTPROT_DHCP) { + if (af == AF_INET) { const struct l_dhcp_lease *lease; + if (netconfig->dns4_overrides) + return l_strv_copy(netconfig->dns4_overrides); + + if (netconfig->rtm_protocol != RTPROT_DHCP) + return NULL; + if (fils && fils->ipv4_dns) { - dns4_list = l_new(char *, 2); - dns4_list[0] = netconfig_ipv4_to_string(fils->ipv4_dns); + char **dns_list = l_new(char *, 2); if (!l_memeqzero(fils->ipv4_dns_mac, 6) && + out_dns_mac && util_ip_subnet_match( fils->ipv4_prefix_len, &fils->ipv4_addr, &fils->ipv4_dns)) - fils_dns4_mac = fils->ipv4_dns_mac; - } else if ((lease = l_dhcp_client_get_lease( - netconfig->dhcp_client))) - dns4_list = l_dhcp_lease_get_dns(lease); - } + *out_dns_mac = fils->ipv4_dns_mac; - if (!netconfig->dns6_overrides && - netconfig->rtm_v6_protocol == RTPROT_DHCP) { + dns_list[0] = netconfig_ipv4_to_string(fils->ipv4_dns); + return dns_list; + } + + if (!(lease = l_dhcp_client_get_lease(netconfig->dhcp_client))) + return NULL; + + return l_dhcp_lease_get_dns(lease); + } else { const struct l_dhcp6_lease *lease; - if (fils && !l_memeqzero(fils->ipv6_dns, - 16)) { - dns6_list = l_new(char *, 2); - dns6_list[0] = netconfig_ipv6_to_string(fils->ipv6_dns); + if (netconfig->dns6_overrides) + return l_strv_copy(netconfig->dns6_overrides); + + if (netconfig->rtm_v6_protocol != RTPROT_DHCP) + return NULL; + + if (fils && !l_memeqzero(fils->ipv6_dns, 16)) { + char **dns_list = l_new(char *, 2); if (!l_memeqzero(fils->ipv6_dns_mac, 6) && + out_dns_mac && util_ip_subnet_match( fils->ipv6_prefix_len, fils->ipv6_addr, fils->ipv6_dns)) - fils_dns6_mac = fils->ipv6_dns_mac; - } else if ((lease = l_dhcp6_client_get_lease( + *out_dns_mac = fils->ipv6_dns_mac; + + dns_list[0] = netconfig_ipv6_to_string(fils->ipv6_dns); + return dns_list; + } + + if (!(lease = l_dhcp6_client_get_lease( netconfig->dhcp6_client))) - dns6_list = l_dhcp6_lease_get_dns(lease); + return NULL; + + return l_dhcp6_lease_get_dns(lease); + } +} + +static void netconfig_set_neighbor_entry_cb(int error, + uint16_t type, const void *data, + uint32_t len, void *user_data) +{ + if (error) + l_error("l_rtnl_neighbor_set_hwaddr failed: %s (%i)", + strerror(-error), error); +} + +static void netconfig_set_dns(struct netconfig *netconfig) +{ + if (!netconfig->dns4_list && !netconfig->dns6_list) + return; + + if (netconfig->dns4_list && netconfig->dns6_list) { + unsigned int n_entries4 = l_strv_length(netconfig->dns4_list); + unsigned int n_entries6 = l_strv_length(netconfig->dns6_list); + char **dns_list = l_malloc(sizeof(char *) * + (n_entries4 + n_entries6 + 1)); + + memcpy(dns_list, netconfig->dns4_list, + sizeof(char *) * n_entries4); + memcpy(dns_list + n_entries4, netconfig->dns6_list, + sizeof(char *) * (n_entries6 + 1)); + resolve_set_dns(netconfig->resolve, dns_list); + l_free(dns_list); + return; } - n_entries += l_strv_length(netconfig->dns4_overrides); - n_entries += l_strv_length(netconfig->dns6_overrides); - n_entries += l_strv_length(dns4_list); - n_entries += l_strv_length(dns6_list); + resolve_set_dns(netconfig->resolve, + netconfig->dns4_list ?: netconfig->dns6_list); +} - dns_list = l_new(char *, n_entries + 1); - n_entries = 0; +static bool netconfig_dns_list_update(struct netconfig *netconfig, uint8_t af) +{ + const uint8_t *fils_dns_mac = NULL; + char ***dns_list_ptr = af == AF_INET ? + &netconfig->dns4_list : &netconfig->dns6_list; + char **new_dns_list = netconfig_get_dns_list(netconfig, af, + &fils_dns_mac); + + if (l_strv_eq(*dns_list_ptr, new_dns_list)) { + l_strv_free(new_dns_list); + return false; + } - APPEND_STRDUPV(dns_list, n_entries, netconfig->dns4_overrides); - APPENDV(dns_list, n_entries, dns4_list); - APPEND_STRDUPV(dns_list, n_entries, netconfig->dns6_overrides); - APPENDV(dns_list, n_entries, dns6_list); - resolve_set_dns(netconfig->resolve, dns_list); + l_strv_free(*dns_list_ptr); + *dns_list_ptr = new_dns_list; - if (fils_dns4_mac && !l_rtnl_neighbor_set_hwaddr(rtnl, - netconfig->ifindex, AF_INET, - &fils->ipv4_dns, fils_dns4_mac, 6, - netconfig_set_neighbor_entry_cb, NULL, - NULL)) - l_debug("l_rtnl_neighbor_set_hwaddr failed"); + if (fils_dns_mac) { + const struct ie_fils_ip_addr_response_info *fils = + netconfig->fils_override; + const void *dns_ip = af == AF_INET ? + (const void *) &fils->ipv4_dns : + (const void *) &fils->ipv6_dns; - if (fils_dns6_mac && !l_rtnl_neighbor_set_hwaddr(rtnl, - netconfig->ifindex, AF_INET6, - fils->ipv6_dns, fils_dns6_mac, 6, - netconfig_set_neighbor_entry_cb, NULL, - NULL)) - l_debug("l_rtnl_neighbor_set_hwaddr failed"); + if (!l_rtnl_neighbor_set_hwaddr(rtnl, netconfig->ifindex, af, + dns_ip, fils_dns_mac, 6, + netconfig_set_neighbor_entry_cb, + NULL, NULL)) + l_debug("l_rtnl_neighbor_set_hwaddr failed"); + } - l_strv_free(dns_list); - /* Contents belonged to dns_list, so not l_strfreev */ - l_free(dns4_list); - l_free(dns6_list); - return 0; + return true; } static void append_domain(char **domains, unsigned int *n_domains, @@ -306,47 +355,71 @@ static void append_domain(char **domains, unsigned int *n_domains, *n_domains += 1; } -static int netconfig_set_domains(struct netconfig *netconfig) +static void netconfig_set_domains(struct netconfig *netconfig) { char *domains[31]; unsigned int n_domains = 0; - char *v4_domain = NULL; - char **v6_domains = NULL; char **p; memset(domains, 0, sizeof(domains)); - /* Allow to override the DHCP domain name with setting entry. */ - v4_domain = l_settings_get_string(netconfig->active_settings, - "IPv4", "DomainName"); - if (!v4_domain && netconfig->rtm_protocol == RTPROT_DHCP) { - const struct l_dhcp_lease *lease = - l_dhcp_client_get_lease(netconfig->dhcp_client); + append_domain(domains, &n_domains, + L_ARRAY_SIZE(domains) - 1, netconfig->v4_domain); - if (lease) - v4_domain = l_dhcp_lease_get_domain_name(lease); - } + for (p = netconfig->v6_domains; p && *p; p++) + append_domain(domains, &n_domains, + L_ARRAY_SIZE(domains) - 1, *p); - append_domain(domains, &n_domains, - L_ARRAY_SIZE(domains) - 1, v4_domain); + resolve_set_domains(netconfig->resolve, domains); +} - if (netconfig->rtm_v6_protocol == RTPROT_DHCP) { - const struct l_dhcp6_lease *lease = - l_dhcp6_client_get_lease(netconfig->dhcp6_client); +static bool netconfig_domains_update(struct netconfig *netconfig, uint8_t af) +{ + bool changed = false; - if (lease) - v6_domains = l_dhcp6_lease_get_domains(lease); - } + if (af == AF_INET) { + /* Allow to override the DHCP domain name with setting entry. */ + char *v4_domain = l_settings_get_string( + netconfig->active_settings, + "IPv4", "DomainName"); - for (p = v6_domains; p && *p; p++) - append_domain(domains, &n_domains, - L_ARRAY_SIZE(domains) - 1, *p); + if (!v4_domain && netconfig->rtm_protocol == RTPROT_DHCP) { + const struct l_dhcp_lease *lease = + l_dhcp_client_get_lease(netconfig->dhcp_client); - resolve_set_domains(netconfig->resolve, domains); - l_strv_free(v6_domains); - l_free(v4_domain); + if (lease) + v4_domain = l_dhcp_lease_get_domain_name(lease); + } - return 0; + if (l_streq0(v4_domain, netconfig->v4_domain)) + l_free(v4_domain); + else { + l_free(netconfig->v4_domain); + netconfig->v4_domain = v4_domain; + changed = true; + } + } else { + char **v6_domains = NULL; + + if (netconfig->rtm_v6_protocol == RTPROT_DHCP) { + const struct l_dhcp6_lease *lease = + l_dhcp6_client_get_lease( + netconfig->dhcp6_client); + + if (lease) + v6_domains = l_dhcp6_lease_get_domains(lease); + } + + if (l_strv_eq(netconfig->v6_domains, v6_domains)) + l_strv_free(v6_domains); + else { + l_strv_free(netconfig->v6_domains); + netconfig->v6_domains = v6_domains; + changed = true; + } + } + + return changed; } static struct l_rtnl_address *netconfig_get_static4_address( @@ -402,6 +475,8 @@ static char *netconfig_ipv4_get_gateway(struct netconfig *netconfig, { const struct l_dhcp_lease *lease; char *gateway; + const struct ie_fils_ip_addr_response_info *fils = + netconfig->fils_override; switch (netconfig->rtm_protocol) { case RTPROT_STATIC: @@ -415,15 +490,12 @@ static char *netconfig_ipv4_get_gateway(struct netconfig *netconfig, return gateway; case RTPROT_DHCP: - if (netconfig->fils_override && - netconfig->fils_override->ipv4_gateway) { - gateway = netconfig_ipv4_to_string( - netconfig->fils_override->ipv4_gateway); + if (fils && fils->ipv4_gateway) { + gateway = netconfig_ipv4_to_string(fils->ipv4_gateway); - if (gateway && !l_memeqzero(netconfig->fils_override-> - ipv4_gateway_mac, 6)) - *out_mac = netconfig->fils_override-> - ipv4_gateway_mac; + if (gateway && out_mac && + !l_memeqzero(fils->ipv4_gateway_mac, 6)) + *out_mac = fils->ipv4_gateway_mac; return gateway; } @@ -461,7 +533,7 @@ static struct l_rtnl_address *netconfig_get_static6_address( prefix_len = strtoul(p, NULL, 10); if (!unlikely(errno == EINVAL || errno == ERANGE || !prefix_len || prefix_len > 128)) { - l_error("netconfig: Invalid prefix '%s' provided in network" + l_error("netconfig: Invalid prefix '%s' provided in network" " configuration file", p); return NULL; } @@ -477,10 +549,12 @@ no_prefix_len: static struct l_rtnl_route *netconfig_get_static6_gateway( struct netconfig *netconfig, + char **out_str, const uint8_t **out_mac) { L_AUTO_FREE_VAR(char *, gateway); struct l_rtnl_route *ret; + const uint8_t *mac = NULL; gateway = l_settings_get_string(netconfig->active_settings, "IPv6", "Gateway"); @@ -492,7 +566,7 @@ static struct l_rtnl_route *netconfig_get_static6_gateway( netconfig->fils_override->ipv6_gateway); if (!l_memeqzero(netconfig->fils_override->ipv6_gateway_mac, 6)) - *out_mac = netconfig->fils_override->ipv6_gateway_mac; + mac = netconfig->fils_override->ipv6_gateway_mac; } else if (!gateway) return NULL; @@ -506,6 +580,8 @@ static struct l_rtnl_route *netconfig_get_static6_gateway( l_rtnl_route_set_priority(ret, ROUTE_PRIORITY_OFFSET); l_rtnl_route_set_protocol(ret, RTPROT_STATIC); + *out_str = l_steal_ptr(gateway); + *out_mac = mac; return ret; } @@ -516,9 +592,7 @@ static struct l_rtnl_address *netconfig_get_dhcp4_address( const struct l_dhcp_lease *lease = l_dhcp_client_get_lease(netconfig->dhcp_client); L_AUTO_FREE_VAR(char *, ip) = NULL; - L_AUTO_FREE_VAR(char *, netmask) = NULL; L_AUTO_FREE_VAR(char *, broadcast) = NULL; - struct in_addr in_addr; uint32_t prefix_len; struct l_rtnl_address *ret; @@ -526,12 +600,10 @@ static struct l_rtnl_address *netconfig_get_dhcp4_address( return NULL; ip = l_dhcp_lease_get_address(lease); - netmask = l_dhcp_lease_get_netmask(lease); broadcast = l_dhcp_lease_get_broadcast(lease); - if (netmask && inet_pton(AF_INET, netmask, &in_addr) > 0) - prefix_len = __builtin_popcountl(in_addr.s_addr); - else + prefix_len = l_dhcp_lease_get_prefix_length(lease); + if (!prefix_len) prefix_len = 24; ret = l_rtnl_address_new(ip, prefix_len); @@ -545,6 +617,39 @@ static struct l_rtnl_address *netconfig_get_dhcp4_address( return ret; } +static void netconfig_gateway_to_arp(struct netconfig *netconfig) +{ + const struct l_dhcp_lease *lease; + _auto_(l_free) char *server_id = NULL; + _auto_(l_free) char *gw = NULL; + const uint8_t *server_mac; + struct in_addr in_gw; + + /* Can only do this for DHCP in certain network setups */ + if (netconfig->rtm_protocol != RTPROT_DHCP) + return; + + lease = l_dhcp_client_get_lease(netconfig->dhcp_client); + if (!lease) + return; + + server_id = l_dhcp_lease_get_server_id(lease); + gw = l_dhcp_lease_get_gateway(lease); + server_mac = l_dhcp_lease_get_server_mac(lease); + + if (!gw || strcmp(server_id, gw) || !server_mac) + return; + + l_debug("Gateway MAC is known, setting into ARP cache"); + in_gw.s_addr = l_dhcp_lease_get_gateway_u32(lease); + + if (!l_rtnl_neighbor_set_hwaddr(rtnl, netconfig->ifindex, AF_INET, + &in_gw, server_mac, ETH_ALEN, + netconfig_set_neighbor_entry_cb, NULL, + NULL)) + l_debug("l_rtnl_neighbor_set_hwaddr failed"); +} + static void netconfig_ifaddr_added(struct netconfig *netconfig, const struct ifaddrmsg *ifa, uint32_t len) @@ -724,6 +829,21 @@ static void netconfig_route_add_cmd_cb(int error, uint16_t type, netconfig->notify = NULL; } +static void netconfig_route6_add_cb(int error, uint16_t type, + const void *data, uint32_t len, + void *user_data) +{ + struct netconfig *netconfig = user_data; + + netconfig->route6_add_cmd_id = 0; + + if (error) { + l_error("netconfig: Failed to add route. Error %d: %s", + error, strerror(-error)); + return; + } +} + static bool netconfig_ipv4_routes_install(struct netconfig *netconfig) { L_AUTO_FREE_VAR(char *, gateway) = NULL; @@ -817,6 +937,8 @@ static void netconfig_ipv4_ifaddr_add_cmd_cb(int error, uint16_t type, return; } + netconfig_gateway_to_arp(netconfig); + if (!netconfig_ipv4_routes_install(netconfig)) { l_error("netconfig: Failed to install IPv4 routes."); return; @@ -832,7 +954,7 @@ static void netconfig_ipv6_ifaddr_add_cmd_cb(int error, uint16_t type, { struct netconfig *netconfig = user_data; struct l_rtnl_route *gateway; - const uint8_t *gateway_mac = NULL; + const uint8_t *gateway_mac; netconfig->addr6_add_cmd_id = 0; @@ -842,12 +964,16 @@ static void netconfig_ipv6_ifaddr_add_cmd_cb(int error, uint16_t type, return; } - gateway = netconfig_get_static6_gateway(netconfig, &gateway_mac); + gateway = netconfig_get_static6_gateway(netconfig, + &netconfig->v6_gateway_str, + &gateway_mac); if (gateway) { - L_WARN_ON(!l_rtnl_route_add(rtnl, netconfig->ifindex, - gateway, - netconfig_route_generic_cb, - netconfig, NULL)); + netconfig->route6_add_cmd_id = l_rtnl_route_add(rtnl, + netconfig->ifindex, + gateway, + netconfig_route6_add_cb, + netconfig, NULL); + L_WARN_ON(unlikely(!netconfig->route6_add_cmd_id)); l_rtnl_route_free(gateway); if (gateway_mac && !l_rtnl_neighbor_set_hwaddr(rtnl, @@ -897,22 +1023,40 @@ static void netconfig_ipv4_dhcp_event_handler(struct l_dhcp_client *client, netconfig->v4_address, netconfig_ifaddr_del_cmd_cb, netconfig, NULL)); - l_rtnl_address_free(netconfig->v4_address); /* Fall through. */ case L_DHCP_CLIENT_EVENT_LEASE_OBTAINED: - netconfig->v4_address = netconfig_get_dhcp4_address(netconfig); + { + char *gateway_str; + struct l_rtnl_address *address; + + gateway_str = netconfig_ipv4_get_gateway(netconfig, NULL); + if (l_streq0(netconfig->v4_gateway_str, gateway_str)) + l_free(gateway_str); + else { + l_free(netconfig->v4_gateway_str); + netconfig->v4_gateway_str = gateway_str; + } + + address = netconfig_get_dhcp4_address(netconfig); + l_rtnl_address_free(netconfig->v4_address); + netconfig->v4_address = address; + if (!netconfig->v4_address) { l_error("netconfig: Failed to obtain IP addresses from " "DHCPv4 lease."); return; } + netconfig_dns_list_update(netconfig, AF_INET); + netconfig_domains_update(netconfig, AF_INET); + L_WARN_ON(!(netconfig->addr4_add_cmd_id = l_rtnl_ifaddr_add(rtnl, netconfig->ifindex, netconfig->v4_address, netconfig_ipv4_ifaddr_add_cmd_cb, netconfig, NULL))); break; + } case L_DHCP_CLIENT_EVENT_LEASE_RENEWED: break; case L_DHCP_CLIENT_EVENT_LEASE_EXPIRED: @@ -922,6 +1066,7 @@ static void netconfig_ipv4_dhcp_event_handler(struct l_dhcp_client *client, netconfig, NULL)); l_rtnl_address_free(netconfig->v4_address); netconfig->v4_address = NULL; + l_free(l_steal_ptr(netconfig->v4_gateway_str)); /* Fall through. */ case L_DHCP_CLIENT_EVENT_NO_LEASE: @@ -950,13 +1095,45 @@ static void netconfig_dhcp6_event_handler(struct l_dhcp6_client *client, case L_DHCP6_CLIENT_EVENT_IP_CHANGED: case L_DHCP6_CLIENT_EVENT_LEASE_OBTAINED: case L_DHCP6_CLIENT_EVENT_LEASE_RENEWED: + { + const struct l_dhcp6_lease *lease = + l_dhcp6_client_get_lease(netconfig->dhcp6_client); + _auto_(l_free) char *addr_str = + l_dhcp6_lease_get_address(lease); + struct l_rtnl_address *address; + struct l_icmp6_client *icmp6 = + l_dhcp6_client_get_icmp6(netconfig->dhcp6_client); + const struct l_icmp6_router *router = + l_icmp6_client_get_router(icmp6); + char *gateway_str = l_icmp6_router_get_address(router); + + if (l_streq0(netconfig->v6_gateway_str, gateway_str)) + l_free(gateway_str); + else { + l_free(netconfig->v6_gateway_str); + netconfig->v6_gateway_str = gateway_str; + } + + address = l_rtnl_address_new(addr_str, + l_dhcp6_lease_get_prefix_length(lease)); + l_rtnl_address_free(netconfig->v6_address); + netconfig->v6_address = address; + + netconfig_dns_list_update(netconfig, AF_INET6); + netconfig_domains_update(netconfig, AF_INET6); netconfig_set_dns(netconfig); netconfig_set_domains(netconfig); break; + } case L_DHCP6_CLIENT_EVENT_LEASE_EXPIRED: l_debug("Lease for interface %u expired", netconfig->ifindex); + netconfig_dns_list_update(netconfig, AF_INET6); + netconfig_domains_update(netconfig, AF_INET6); netconfig_set_dns(netconfig); netconfig_set_domains(netconfig); + l_rtnl_address_free(netconfig->v6_address); + netconfig->v6_address = NULL; + l_free(l_steal_ptr(netconfig->v6_gateway_str)); /* Fall through */ case L_DHCP6_CLIENT_EVENT_NO_LEASE: @@ -985,14 +1162,18 @@ static void netconfig_reset_v4(struct netconfig *netconfig) if (netconfig->rtm_protocol) { netconfig_remove_v4_address(netconfig); - l_strfreev(netconfig->dns4_overrides); - netconfig->dns4_overrides = NULL; + l_strv_free(l_steal_ptr(netconfig->dns4_overrides)); + l_strv_free(l_steal_ptr(netconfig->dns4_list)); l_dhcp_client_stop(netconfig->dhcp_client); netconfig->rtm_protocol = 0; l_acd_destroy(netconfig->acd); netconfig->acd = NULL; + + l_free(l_steal_ptr(netconfig->v4_gateway_str)); + + l_free(l_steal_ptr(netconfig->v4_domain)); } } @@ -1031,6 +1212,7 @@ static void netconfig_ipv4_acd_event(enum l_acd_event event, void *user_data) static bool netconfig_ipv4_select_and_install(struct netconfig *netconfig) { + struct netdev *netdev = netdev_find(netconfig->ifindex); bool set_address = (netconfig->rtm_protocol == RTPROT_STATIC); if (netconfig->rtm_protocol == RTPROT_DHCP && @@ -1068,6 +1250,9 @@ static bool netconfig_ipv4_select_and_install(struct netconfig *netconfig) ip))) return false; + netconfig_dns_list_update(netconfig, AF_INET); + netconfig_domains_update(netconfig, AF_INET); + netconfig->acd = l_acd_new(netconfig->ifindex); l_acd_set_event_handler(netconfig->acd, netconfig_ipv4_acd_event, netconfig, @@ -1091,6 +1276,9 @@ static bool netconfig_ipv4_select_and_install(struct netconfig *netconfig) return true; } + l_dhcp_client_set_address(netconfig->dhcp_client, ARPHRD_ETHER, + netdev_get_address(netdev), ETH_ALEN); + if (l_dhcp_client_start(netconfig->dhcp_client)) return true; @@ -1102,7 +1290,6 @@ static bool netconfig_ipv4_select_and_install(struct netconfig *netconfig) static bool netconfig_ipv6_select_and_install(struct netconfig *netconfig) { struct netdev *netdev = netdev_find(netconfig->ifindex); - struct l_rtnl_address *address = NULL; if (netconfig->rtm_v6_protocol == RTPROT_UNSPEC) { l_debug("IPV6 configuration disabled"); @@ -1111,10 +1298,7 @@ static bool netconfig_ipv6_select_and_install(struct netconfig *netconfig) sysfs_write_ipv6_setting(netdev_get_name(netdev), "disable_ipv6", "0"); - if (netconfig->rtm_v6_protocol == RTPROT_STATIC) - address = netconfig_get_static6_address( - netconfig->active_settings); - else if (netconfig->rtm_v6_protocol == RTPROT_DHCP && + if (netconfig->rtm_v6_protocol == RTPROT_DHCP && netconfig->fils_override && !l_memeqzero(netconfig->fils_override->ipv6_addr, 16)) { uint8_t prefix_len = netconfig->fils_override->ipv6_prefix_len; @@ -1124,11 +1308,12 @@ static bool netconfig_ipv6_select_and_install(struct netconfig *netconfig) if (unlikely(!addr_str)) return false; - if (L_WARN_ON(unlikely(!(address = l_rtnl_address_new(addr_str, - prefix_len))))) + netconfig->v6_address = l_rtnl_address_new(addr_str, + prefix_len); + if (L_WARN_ON(unlikely(!netconfig->v6_address))) return false; - l_rtnl_address_set_noprefixroute(address, true); + l_rtnl_address_set_noprefixroute(netconfig->v6_address, true); /* * TODO: If netconfig->fils_override->ipv6_lifetime is set, @@ -1138,16 +1323,21 @@ static bool netconfig_ipv6_select_and_install(struct netconfig *netconfig) */ } - if (address) { + if (netconfig->v6_address) { + netconfig_dns_list_update(netconfig, AF_INET6); + L_WARN_ON(!(netconfig->addr6_add_cmd_id = - l_rtnl_ifaddr_add(rtnl, netconfig->ifindex, address, + l_rtnl_ifaddr_add(rtnl, netconfig->ifindex, + netconfig->v6_address, netconfig_ipv6_ifaddr_add_cmd_cb, netconfig, NULL))); - l_rtnl_address_free(address); return true; } - /* DHCP */ + /* DHCPv6 or RA, update MAC */ + l_dhcp6_client_set_address(netconfig->dhcp6_client, ARPHRD_ETHER, + netdev_get_address(netdev), ETH_ALEN); + return true; } @@ -1181,17 +1371,16 @@ static int validate_dns_list(int family, char **dns_list) } bool netconfig_load_settings(struct netconfig *netconfig, - const struct l_settings *active_settings, - const uint8_t *mac_address) + const struct l_settings *active_settings) { - char *mdns; + _auto_(l_free) char *mdns = NULL; bool send_hostname; bool v6_enabled; char hostname[HOST_NAME_MAX + 1]; - char **dns4_overrides = NULL; - char **dns6_overrides = NULL; - struct l_rtnl_address *v4_address = NULL; - struct l_rtnl_address *v6_address = NULL; + _auto_(l_strv_free) char **dns4_overrides = NULL; + _auto_(l_strv_free) char **dns6_overrides = NULL; + _auto_(l_rtnl_address_free) struct l_rtnl_address *v4_address = NULL; + _auto_(l_rtnl_address_free) struct l_rtnl_address *v6_address = NULL; dns4_overrides = l_settings_get_string_list(active_settings, "IPv4", "DNS", ' '); @@ -1203,7 +1392,7 @@ bool netconfig_load_settings(struct netconfig *netconfig, dns4_overrides = NULL; if (r < 0) - goto err_dns4; + return false; } if (r == 0) @@ -1221,7 +1410,7 @@ bool netconfig_load_settings(struct netconfig *netconfig, dns6_overrides = NULL; if (r < 0) - goto err_dns6; + return false; } if (r == 0) @@ -1248,7 +1437,7 @@ bool netconfig_load_settings(struct netconfig *netconfig, if (unlikely(!v4_address)) { l_error("netconfig: Can't parse IPv4 address"); - goto err_v4_addr; + return false; } } @@ -1258,21 +1447,15 @@ bool netconfig_load_settings(struct netconfig *netconfig, if (l_settings_has_key(active_settings, "IPv6", "Address")) { v6_address = netconfig_get_static6_address(active_settings); - l_rtnl_address_free(v6_address); if (unlikely(!v6_address)) { l_error("netconfig: Can't parse IPv6 address"); - goto err_v6_addr; + return false; } } /* No more validation steps for now, commit new values */ - - if (v4_address) { - netconfig->v4_address = v4_address; - netconfig->rtm_protocol = RTPROT_STATIC; - } else - netconfig->rtm_protocol = RTPROT_DHCP; + netconfig->rtm_protocol = v4_address ? RTPROT_STATIC : RTPROT_DHCP; if (!v6_enabled) netconfig->rtm_v6_protocol = RTPROT_UNSPEC; @@ -1284,28 +1467,19 @@ bool netconfig_load_settings(struct netconfig *netconfig, if (send_hostname) l_dhcp_client_set_hostname(netconfig->dhcp_client, hostname); - resolve_set_mdns(netconfig->resolve, mdns); - l_free(mdns); + netconfig_free_settings(netconfig); - l_dhcp_client_set_address(netconfig->dhcp_client, ARPHRD_ETHER, - mac_address, ETH_ALEN); - l_dhcp6_client_set_address(netconfig->dhcp6_client, ARPHRD_ETHER, - mac_address, ETH_ALEN); + if (netconfig->rtm_protocol == RTPROT_STATIC) + netconfig->v4_address = l_steal_ptr(v4_address); + + if (netconfig->rtm_v6_protocol == RTPROT_STATIC) + netconfig->v6_address = l_steal_ptr(v6_address); netconfig->active_settings = active_settings; - netconfig->dns4_overrides = dns4_overrides; - netconfig->dns6_overrides = dns6_overrides; + netconfig->dns4_overrides = l_steal_ptr(dns4_overrides); + netconfig->dns6_overrides = l_steal_ptr(dns6_overrides); + netconfig->mdns = l_steal_ptr(mdns); return true; - -err_v6_addr: - l_rtnl_address_free(v4_address); -err_v4_addr: - l_free(mdns); - l_strfreev(dns6_overrides); -err_dns6: - l_strfreev(dns4_overrides); -err_dns4: - return false; } bool netconfig_configure(struct netconfig *netconfig, @@ -1320,11 +1494,22 @@ bool netconfig_configure(struct netconfig *netconfig, if (unlikely(!netconfig_ipv6_select_and_install(netconfig))) return false; + resolve_set_mdns(netconfig->resolve, netconfig->mdns); + return true; } bool netconfig_reconfigure(struct netconfig *netconfig) { + /* + * Starting with kernel 4.20, ARP cache is flushed when the netdev + * detects NO CARRIER. This can result in unnecessarily long delays + * (about 1 second on some networks) due to ARP query response being + * lost or delayed. Try to force the gateway into the ARP cache + * to alleviate this + */ + netconfig_gateway_to_arp(netconfig); + if (netconfig->rtm_protocol == RTPROT_DHCP) { /* TODO l_dhcp_client sending a DHCP inform request */ } @@ -1345,6 +1530,11 @@ bool netconfig_reset(struct netconfig *netconfig) netconfig->route4_add_gateway_cmd_id = 0; } + if (netconfig->route6_add_cmd_id) { + l_netlink_cancel(rtnl, netconfig->route6_add_cmd_id); + netconfig->route6_add_cmd_id = 0; + } + if (netconfig->addr4_add_cmd_id) { l_netlink_cancel(rtnl, netconfig->addr4_add_cmd_id); netconfig->addr4_add_cmd_id = 0; @@ -1361,14 +1551,21 @@ bool netconfig_reset(struct netconfig *netconfig) netconfig_reset_v4(netconfig); if (netconfig->rtm_v6_protocol) { - l_strfreev(netconfig->dns6_overrides); - netconfig->dns6_overrides = NULL; + l_rtnl_address_free(netconfig->v6_address); + netconfig->v6_address = NULL; + + l_strv_free(l_steal_ptr(netconfig->dns6_overrides)); + l_strv_free(l_steal_ptr(netconfig->dns6_list)); l_dhcp6_client_stop(netconfig->dhcp6_client); netconfig->rtm_v6_protocol = 0; sysfs_write_ipv6_setting(netdev_get_name(netdev), "disable_ipv6", "1"); + + l_free(l_steal_ptr(netconfig->v6_gateway_str)); + + l_strv_free(l_steal_ptr(netconfig->v6_domains)); } l_free(l_steal_ptr(netconfig->fils_override)); @@ -1485,6 +1682,15 @@ void netconfig_destroy(struct netconfig *netconfig) netconfig_free(netconfig); } +bool netconfig_enabled(void) +{ + bool enabled; + + return l_settings_get_bool(iwd_get_config(), "General", + "EnableNetworkConfiguration", + &enabled) && enabled; +} + static int netconfig_init(void) { uint32_t r; diff --git a/src/netconfig.h b/src/netconfig.h @@ -32,8 +32,7 @@ typedef void (*netconfig_notify_func_t)(enum netconfig_event event, void *user_data); bool netconfig_load_settings(struct netconfig *netconfig, - const struct l_settings *active_settings, - const uint8_t *mac_address); + const struct l_settings *active_settings); bool netconfig_configure(struct netconfig *netconfig, netconfig_notify_func_t notify, void *user_data); @@ -47,3 +46,5 @@ void netconfig_handle_fils_ip_resp(struct netconfig *netconfig, struct netconfig *netconfig_new(uint32_t ifindex); void netconfig_destroy(struct netconfig *netconfig); + +bool netconfig_enabled(void); diff --git a/src/netdev.c b/src/netdev.c @@ -64,6 +64,7 @@ #include "src/auth-proto.h" #include "src/frame-xchg.h" #include "src/diagnostic.h" +#include "src/band.h" #ifndef ENOTSUPP #define ENOTSUPP 524 @@ -86,6 +87,7 @@ struct netdev_handshake_state { uint32_t group_management_new_key_cmd_id; uint32_t set_station_cmd_id; uint32_t set_pmk_cmd_id; + uint32_t pairwise_set_key_tx_cmd_id; bool ptk_installed; bool gtk_installed; bool igtk_installed; @@ -101,6 +103,12 @@ struct netdev_ft_over_ds_info { bool parsed : 1; }; +struct netdev_ext_key_info { + uint16_t proto; + bool noencrypt; + struct eapol_frame frame[0]; +}; + struct netdev { uint32_t index; uint64_t wdev_id; @@ -129,10 +137,12 @@ struct netdev { uint32_t rekey_offload_cmd_id; uint32_t qos_map_cmd_id; uint32_t mac_change_cmd_id; + uint32_t get_oci_cmd_id; enum netdev_result result; uint16_t last_code; /* reason or status, depending on result */ struct l_timeout *neighbor_report_timeout; struct l_timeout *sa_query_timeout; + struct l_timeout *sa_query_delay; struct l_timeout *group_handshake_timeout; uint16_t sa_query_id; uint8_t prev_snonce[32]; @@ -168,6 +178,8 @@ struct netdev { struct l_queue *ft_ds_list; + struct netdev_ext_key_info *ext_key_info; + bool connected : 1; bool associated : 1; bool operational : 1; @@ -201,7 +213,6 @@ static struct l_netlink *rtnl = NULL; static struct l_genl_family *nl80211; static struct l_queue *netdev_list; static struct watchlist netdev_watches; -static bool pae_over_nl80211; static bool mac_per_ssid; static unsigned int iov_ie_append(struct iovec *iov, @@ -335,6 +346,11 @@ static void netdev_handshake_state_cancel_all( l_genl_family_cancel(nl80211, nhs->set_pmk_cmd_id); nhs->set_pmk_cmd_id = 0; } + + if (nhs->pairwise_set_key_tx_cmd_id) { + l_genl_family_cancel(nl80211, nhs->pairwise_set_key_tx_cmd_id); + nhs->pairwise_set_key_tx_cmd_id = 0; + } } static void netdev_handshake_state_free(struct handshake_state *hs) @@ -765,6 +781,11 @@ static void netdev_connect_free(struct netdev *netdev) netdev->handshake = NULL; } + if (netdev->ext_key_info) { + l_free(netdev->ext_key_info); + netdev->ext_key_info = NULL; + } + if (netdev->neighbor_report_cb) { netdev->neighbor_report_cb(netdev, -ENOTCONN, NULL, 0, netdev->user_data); @@ -777,6 +798,11 @@ static void netdev_connect_free(struct netdev *netdev) netdev->sa_query_timeout = NULL; } + if (netdev->sa_query_delay) { + l_timeout_remove(netdev->sa_query_delay); + netdev->sa_query_delay = NULL; + } + if (netdev->group_handshake_timeout) { l_timeout_remove(netdev->group_handshake_timeout); netdev->group_handshake_timeout = NULL; @@ -923,6 +949,11 @@ static void netdev_free(void *data) netdev->get_station_cmd_id = 0; } + if (netdev->get_oci_cmd_id) { + l_genl_family_cancel(nl80211, netdev->get_oci_cmd_id); + netdev->get_oci_cmd_id = 0; + } + if (netdev->fw_roam_bss) scan_bss_free(netdev->fw_roam_bss); @@ -1687,6 +1718,24 @@ static void netdev_set_igtk(struct handshake_state *hs, uint16_t key_index, netdev_setting_keys_failed(nhs, -EIO); } +static struct l_genl_msg *netdev_build_cmd_set_key_tx(struct netdev *netdev) +{ + uint8_t key_mode = NL80211_KEY_SET_TX; + struct l_genl_msg *msg = l_genl_msg_new_sized(NL80211_CMD_SET_KEY, 512); + + l_genl_msg_append_attr(msg, NL80211_ATTR_MAC, ETH_ALEN, + netdev->handshake->aa); + l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &netdev->index); + + l_genl_msg_enter_nested(msg, NL80211_ATTR_KEY); + l_genl_msg_append_attr(msg, NL80211_KEY_IDX, 1, + &netdev->handshake->active_tk_index); + l_genl_msg_append_attr(msg, NL80211_KEY_MODE, 1, &key_mode); + l_genl_msg_leave_nested(msg); + + return msg; +} + static void netdev_new_pairwise_key_cb(struct l_genl_msg *msg, void *data) { struct netdev_handshake_state *nhs = data; @@ -1724,14 +1773,171 @@ error: netdev_setting_keys_failed(nhs, err); } +static struct l_genl_msg *netdev_build_control_port_frame(struct netdev *netdev, + const uint8_t *to, + uint16_t proto, + bool unencrypted, + const void *body, + size_t body_len) +{ + struct l_genl_msg *msg; + + msg = l_genl_msg_new_sized(NL80211_CMD_CONTROL_PORT_FRAME, + 128 + body_len); + + l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &netdev->index); + l_genl_msg_append_attr(msg, NL80211_ATTR_FRAME, body_len, body); + l_genl_msg_append_attr(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, 2, + &proto); + l_genl_msg_append_attr(msg, NL80211_ATTR_MAC, ETH_ALEN, to); + + if (unencrypted) + l_genl_msg_append_attr(msg, + NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT, 0, NULL); + + return msg; +} + +static void netdev_control_port_frame_cb(struct l_genl_msg *msg, + void *user_data) +{ + int err = l_genl_msg_get_error(msg); + const char *ext_error; + + if (err >= 0) + return; + + ext_error = l_genl_msg_get_extended_error(msg); + l_error("CMD_CONTROL_PORT failed: %s", + ext_error ? ext_error : strerror(-err)); +} + +static int netdev_control_port_write_pae(struct netdev *netdev, + const uint8_t *dest, + uint16_t proto, + const struct eapol_frame *ef, + bool noencrypt) +{ + int fd = l_io_get_fd(netdev->pae_io); + struct sockaddr_ll sll; + size_t frame_size = sizeof(struct eapol_header) + + L_BE16_TO_CPU(ef->header.packet_len); + ssize_t r; + + memset(&sll, 0, sizeof(sll)); + sll.sll_family = AF_PACKET; + sll.sll_ifindex = netdev->index; + sll.sll_protocol = htons(proto); + sll.sll_halen = ETH_ALEN; + memcpy(sll.sll_addr, dest, ETH_ALEN); + + r = sendto(fd, ef, frame_size, 0, + (struct sockaddr *) &sll, sizeof(sll)); + if (r < 0) + l_error("EAPoL write socket: %s", strerror(errno)); + + return r; +} + +static int netdev_control_port_frame(uint32_t ifindex, + const uint8_t *dest, uint16_t proto, + const struct eapol_frame *ef, + bool noencrypt, + void *user_data) +{ + struct l_genl_msg *msg; + struct netdev *netdev; + size_t frame_size; + + netdev = netdev_find(ifindex); + if (!netdev) + return -ENOENT; + + frame_size = sizeof(struct eapol_header) + + L_BE16_TO_CPU(ef->header.packet_len); + + if (!netdev->pae_over_nl80211) + return netdev_control_port_write_pae(netdev, dest, proto, + ef, noencrypt); + + msg = netdev_build_control_port_frame(netdev, dest, proto, noencrypt, + ef, frame_size); + if (!msg) + return -ENOMEM; + + if (!l_genl_family_send(nl80211, msg, netdev_control_port_frame_cb, + netdev, NULL)) { + l_genl_msg_unref(msg); + return -EINVAL; + } + + return 0; +} + +static int netdev_set_key_tx(struct netdev *netdev) +{ + struct netdev_handshake_state *nhs = l_container_of(netdev->handshake, + struct netdev_handshake_state, super); + struct l_genl_msg *msg = netdev_build_cmd_set_key_tx(netdev); + + nhs->pairwise_set_key_tx_cmd_id = l_genl_family_send(nl80211, msg, + netdev_new_pairwise_key_cb, + nhs, NULL); + if (nhs->pairwise_set_key_tx_cmd_id > 0) + return 0; + + l_genl_msg_unref(msg); + + return -EIO; +} + +static void netdev_new_rx_pairwise_key_cb(struct l_genl_msg *msg, void *data) +{ + struct netdev_handshake_state *nhs = data; + struct netdev *netdev = nhs->netdev; + struct netdev_ext_key_info *info = netdev->ext_key_info; + int err = l_genl_msg_get_error(msg); + + nhs->pairwise_new_key_cmd_id = 0; + + if (err < 0) { + const char *ext_error = l_genl_msg_get_extended_error(msg); + + l_error("New Key for RX Pairwise Key failed for ifindex: %d:%s", + netdev->index, + ext_error ? ext_error : strerror(-err)); + goto error; + } + + if (!info) + return; + + err = netdev_control_port_write_pae(netdev, nhs->super.aa, info->proto, + info->frame, info->noencrypt); + l_free(netdev->ext_key_info); + netdev->ext_key_info = NULL; + + if (err < 0) + goto error; + + err = netdev_set_key_tx(netdev); + if (err < 0) + goto error; + + return; + +error: + netdev_setting_keys_failed(nhs, err); +} + static struct l_genl_msg *netdev_build_cmd_new_key_pairwise( struct netdev *netdev, uint32_t cipher, const uint8_t *aa, const uint8_t *tk, - size_t tk_len) + size_t tk_len, + uint8_t key_id) { - uint8_t key_id = 0; struct l_genl_msg *msg; msg = l_genl_msg_new_sized(NL80211_CMD_NEW_KEY, 512); @@ -1745,6 +1951,36 @@ static struct l_genl_msg *netdev_build_cmd_new_key_pairwise( return msg; } +static struct l_genl_msg *netdev_build_cmd_new_rx_key_pairwise( + struct netdev *netdev, + uint32_t cipher, + const uint8_t *aa, + const uint8_t *tk, + size_t tk_len, + uint8_t key_id) +{ + uint8_t key_mode = NL80211_KEY_NO_TX; + uint32_t key_type = NL80211_KEYTYPE_PAIRWISE; + struct l_genl_msg *msg; + + msg = l_genl_msg_new_sized(NL80211_CMD_NEW_KEY, 512); + + l_genl_msg_append_attr(msg, NL80211_ATTR_MAC, ETH_ALEN, aa); + l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &netdev->index); + + l_genl_msg_enter_nested(msg, NL80211_ATTR_KEY); + + l_genl_msg_append_attr(msg, NL80211_KEY_DATA, tk_len, tk); + l_genl_msg_append_attr(msg, NL80211_KEY_CIPHER, 4, &cipher); + l_genl_msg_append_attr(msg, NL80211_KEY_IDX, 1, &key_id); + l_genl_msg_append_attr(msg, NL80211_KEY_MODE, 1, &key_mode); + l_genl_msg_append_attr(msg, NL80211_KEY_TYPE, 4, &key_type); + + l_genl_msg_leave_nested(msg); + + return msg; +} + static void netdev_group_timeout_cb(struct l_timeout *timeout, void *user_data) { struct netdev_handshake_state *nhs = user_data; @@ -1769,7 +2005,7 @@ static void netdev_group_timeout_cb(struct l_timeout *timeout, void *user_data) netdev_connect_ok(nhs->netdev); } -static void netdev_set_tk(struct handshake_state *hs, +static void netdev_set_tk(struct handshake_state *hs, uint8_t key_index, const uint8_t *tk, uint32_t cipher) { struct netdev_handshake_state *nhs = @@ -1817,7 +2053,8 @@ static void netdev_set_tk(struct handshake_state *hs, goto invalid_key; msg = netdev_build_cmd_new_key_pairwise(netdev, cipher, addr, tk_buf, - crypto_cipher_key_len(cipher)); + crypto_cipher_key_len(cipher), + key_index); nhs->pairwise_new_key_cmd_id = l_genl_family_send(nl80211, msg, netdev_new_pairwise_key_cb, nhs, NULL); @@ -1830,6 +2067,74 @@ invalid_key: netdev_setting_keys_failed(nhs, err); } +static void netdev_set_ext_tk(struct handshake_state *hs, uint8_t key_idx, + const uint8_t *tk, uint32_t cipher, + const struct eapol_frame *step4, uint16_t proto, + bool noencrypt) +{ + struct netdev_handshake_state *nhs = + l_container_of(hs, struct netdev_handshake_state, super); + uint8_t tk_buf[32]; + struct netdev *netdev = nhs->netdev; + struct l_genl_msg *msg; + const uint8_t *addr = netdev_choose_key_address(nhs); + int err; + size_t frame_size = sizeof(struct eapol_header) + + L_BE16_TO_CPU(step4->header.packet_len); + + err = -ENOENT; + if (!netdev_copy_tk(tk_buf, tk, cipher, false)) + goto error; + + msg = netdev_build_cmd_new_rx_key_pairwise(netdev, cipher, addr, tk_buf, + crypto_cipher_key_len(cipher), + hs->active_tk_index); + nhs->pairwise_new_key_cmd_id = + l_genl_family_send(nl80211, msg, netdev_new_rx_pairwise_key_cb, + nhs, NULL); + + if (!nhs->pairwise_new_key_cmd_id) + goto io_error; + + /* + * Without control port we cannot guarantee the order that messages go + * out and must wait for NEW_KEY to call back before sending message 4 + */ + if (!netdev->pae_over_nl80211) { + netdev->ext_key_info = l_malloc( + sizeof(struct netdev_ext_key_info) + + frame_size); + memcpy(netdev->ext_key_info->frame, step4, frame_size); + netdev->ext_key_info->proto = proto; + netdev->ext_key_info->noencrypt = noencrypt; + return; + } + + /* + * Otherwise, order of messages will be guaranteed. Therefore we can + * send send message 4, and set the TK to TX (below) without waiting for + * any callbacks + */ + err = netdev_control_port_frame(netdev->index, netdev->handshake->aa, + proto, step4, noencrypt, NULL); + if (err < 0) + goto error; + + /* Then toggle to RX + TX */ + err = netdev_set_key_tx(netdev); + if (err < 0) + goto error; + + return; + +io_error: + err = -EIO; + l_genl_msg_unref(msg); + +error: + netdev_setting_keys_failed(nhs, err); +} + static void netdev_set_pmk_cb(struct l_genl_msg *msg, void *user_data) { struct netdev_handshake_state *nhs = user_data; @@ -2038,43 +2343,109 @@ static void netdev_send_qos_map_set(struct netdev *netdev, netdev, NULL); } -static void parse_request_ies(struct netdev *netdev, const uint8_t *ies, - size_t ies_len) +static void netdev_get_oci_cb(struct l_genl_msg *msg, void *user_data) { - struct ie_tlv_iter iter; - const void *data; - const uint8_t *rsnxe = NULL; - - /* - * The driver may have modified the IEs we passed to CMD_CONNECT - * before sending them out, the actual IE sent is reflected in the - * ATTR_REQ_IE sequence. These are the values EAPoL will need to use. - */ - ie_tlv_iter_init(&iter, ies, ies_len); + struct netdev *netdev = user_data; + int err = l_genl_msg_get_error(msg); + _auto_(l_free) struct band_chandef *chandef = + l_new(struct band_chandef, 1); - while (ie_tlv_iter_next(&iter)) { - data = ie_tlv_iter_get_data(&iter); + netdev->get_oci_cmd_id = 0; - switch (ie_tlv_iter_get_tag(&iter)) { - case IE_TYPE_RSN: - handshake_state_set_supplicant_ie(netdev->handshake, - data - 2); - break; - case IE_TYPE_RSNX: - if (!rsnxe) - rsnxe = data - 2; - break; - case IE_TYPE_VENDOR_SPECIFIC: - if (!is_ie_wpa_ie(data, ie_tlv_iter_get_length(&iter))) - break; + if (err < 0) { + const char *ext_error = l_genl_msg_get_extended_error(msg); - handshake_state_set_supplicant_ie(netdev->handshake, - data - 2); - break; - case IE_TYPE_MOBILITY_DOMAIN: - handshake_state_set_mde(netdev->handshake, data - 2); - break; - } + l_error("Could not get OCI info: %s", + ext_error ? ext_error : strerror(-err)); + goto done; + } + + if (nl80211_parse_chandef(msg, chandef) < 0) { + l_debug("Couldn't parse operating channel info."); + goto done; + } + + l_debug("Obtained OCI: freq: %u, width: %u, center1: %u, center2: %u", + chandef->frequency, chandef->channel_width, + chandef->center1_frequency, chandef->center2_frequency); + + handshake_state_set_chandef(netdev->handshake, l_steal_ptr(chandef)); + +done: + if (netdev->ap) { + /* + * Cant do much here. IWD assumes every kernel/driver supports + * this. There is no way of detecting support either. + */ + if (L_WARN_ON(err < 0)) + netdev_connect_failed(netdev, + NETDEV_RESULT_AUTHENTICATION_FAILED, + MMPDU_STATUS_CODE_UNSPECIFIED); + else + auth_proto_rx_oci(netdev->ap); + + return; + } + + L_WARN_ON(!eapol_start(netdev->sm)); +} + +static int netdev_get_oci(void *user_data) +{ + struct netdev *netdev = user_data; + struct l_genl_msg *msg = + l_genl_msg_new_sized(NL80211_CMD_GET_INTERFACE, 64); + + l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &netdev->index); + + netdev->get_oci_cmd_id = l_genl_family_send(nl80211, msg, + netdev_get_oci_cb, netdev, + NULL); + if (!netdev->get_oci_cmd_id) { + l_genl_msg_unref(msg); + return -EIO; + } + + return 0; +} + +static void parse_request_ies(struct netdev *netdev, const uint8_t *ies, + size_t ies_len) +{ + struct ie_tlv_iter iter; + const void *data; + const uint8_t *rsnxe = NULL; + + /* + * The driver may have modified the IEs we passed to CMD_CONNECT + * before sending them out, the actual IE sent is reflected in the + * ATTR_REQ_IE sequence. These are the values EAPoL will need to use. + */ + ie_tlv_iter_init(&iter, ies, ies_len); + + while (ie_tlv_iter_next(&iter)) { + data = ie_tlv_iter_get_data(&iter); + + switch (ie_tlv_iter_get_tag(&iter)) { + case IE_TYPE_RSN: + handshake_state_set_supplicant_ie(netdev->handshake, + data - 2); + break; + case IE_TYPE_RSNX: + if (!rsnxe) + rsnxe = data - 2; + break; + case IE_TYPE_VENDOR_SPECIFIC: + if (!is_ie_wpa_ie(data, ie_tlv_iter_get_length(&iter))) + break; + + handshake_state_set_supplicant_ie(netdev->handshake, + data - 2); + break; + case IE_TYPE_MOBILITY_DOMAIN: + handshake_state_set_mde(netdev->handshake, data - 2); + break; + } } /* RSNXE element might be omitted when FTing */ @@ -2492,10 +2863,7 @@ process_resp_ies: netdev->owe_sm = NULL; } - /* FILS handles its own FT key derivation */ - if (fte && !(netdev->handshake->akm_suite & - (IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA256 | - IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA384))) { + if (fte) { uint32_t kck_len = handshake_state_get_kck_len(netdev->handshake); /* @@ -2523,12 +2891,11 @@ process_resp_ies: } if (netdev->sm) { - /* - * Start processing EAPoL frames now that the state machine - * has all the input data even in FT mode. - */ - if (L_WARN_ON(!eapol_start(netdev->sm))) - goto deauth; + if (!netdev->handshake->chandef) { + if (netdev_get_oci(netdev) < 0) + goto deauth; + } else if (!eapol_start(netdev->sm)) + goto deauth; return; } @@ -2549,6 +2916,8 @@ error: return; deauth: + netdev->result = NETDEV_RESULT_ASSOCIATION_FAILED; + netdev->last_code = MMPDU_STATUS_CODE_UNSPECIFIED; msg = netdev_build_cmd_deauthenticate(netdev, MMPDU_REASON_CODE_UNSPECIFIED); netdev->disconnect_cmd_id = l_genl_family_send(nl80211, @@ -3617,6 +3986,7 @@ static void netdev_connect_common(struct netdev *netdev, case IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA384: netdev->ap = fils_sm_new(hs, netdev_fils_tx_authenticate, netdev_fils_tx_associate, + netdev_get_oci, netdev); break; default: @@ -4041,10 +4411,13 @@ static void prepare_ft(struct netdev *netdev, const struct scan_bss *target_bss) target_bss->rsne); memcpy(netdev->handshake->mde + 2, target_bss->mde, 3); + netdev->handshake->active_tk_index = 0; netdev->associated = false; netdev->operational = false; netdev->in_ft = true; + handshake_state_set_chandef(netdev->handshake, NULL); + /* * Cancel commands that could be running because of EAPoL activity * like re-keying, this way the callbacks for those commands don't @@ -4141,9 +4514,8 @@ static void netdev_ft_response_frame_event(const struct mmpdu_header *hdr, goto ft_error; } - ret = ft_over_ds_parse_action_ies(&info->super, netdev->handshake, - ies, ies_len); - if (ret < 0) + if (!ft_over_ds_parse_action_ies(&info->super, netdev->handshake, + ies, ies_len)) goto ft_error; info->parsed = true; @@ -4220,7 +4592,8 @@ int netdev_fast_transition(struct netdev *netdev, netdev->ap = ft_over_air_sm_new(netdev->handshake, netdev_ft_tx_authenticate, - netdev_ft_tx_associate, netdev); + netdev_ft_tx_associate, + netdev_get_oci, netdev); memcpy(netdev->ap->prev_bssid, orig_bss->addr, ETH_ALEN); wiphy_radio_work_insert(netdev->wiphy, &netdev->work, 1, @@ -4315,6 +4688,11 @@ int netdev_fast_transition_over_ds_action(struct netdev *netdev, memcpy(info->super.spa, hs->spa, ETH_ALEN); memcpy(info->super.aa, target_bss->addr, ETH_ALEN); memcpy(info->super.mde, target_bss->mde, sizeof(info->super.mde)); + + if (target_bss->rsne) + info->super.authenticator_ie = l_memdup(target_bss->rsne, + target_bss->rsne[1] + 2); + l_getrandom(info->super.snonce, 32); info->super.free = netdev_ft_ds_info_free; @@ -4326,7 +4704,8 @@ int netdev_fast_transition_over_ds_action(struct netdev *netdev, iovs[0].iov_base = ft_req; iovs[0].iov_len = sizeof(ft_req); - if (!ft_build_authenticate_ies(hs, info->super.snonce, buf, &len)) + if (!ft_build_authenticate_ies(hs, false, info->super.snonce, + buf, &len)) goto failed; iovs[1].iov_base = buf; @@ -4490,13 +4869,97 @@ static void netdev_sa_query_resp_cb(struct l_genl_msg *msg, void *user_data) ext_error ? ext_error : strerror(-err)); } +static int netdev_build_oci(struct netdev *netdev, uint8_t *out) +{ + out[0] = IE_TYPE_EXTENSION; + out[1] = 4; + out[2] = IE_TYPE_OCI & 0xff; + + return oci_from_chandef(netdev->handshake->chandef, out + 3); +} + +static void netdev_sa_query_timeout(struct l_timeout *timeout, + void *user_data) +{ + struct netdev *netdev = user_data; + struct l_genl_msg *msg; + + l_info("SA Query timed out, connection is invalid. Disconnecting..."); + + l_timeout_remove(netdev->sa_query_timeout); + netdev->sa_query_timeout = NULL; + + msg = netdev_build_cmd_disconnect(netdev, + MMPDU_REASON_CODE_PREV_AUTH_NOT_VALID); + netdev->disconnect_cmd_id = l_genl_family_send(nl80211, msg, + netdev_disconnect_cb, netdev, NULL); +} + +static void netdev_sa_query_req_cb(struct l_genl_msg *msg, void *user_data) +{ + struct netdev *netdev = user_data; + int err = l_genl_msg_get_error(msg); + const char *ext_error; + + if (err >= 0) + return; + + ext_error = l_genl_msg_get_extended_error(msg); + l_debug("error sending SA Query request: %s", + ext_error ? ext_error : strerror(-err)); + + l_timeout_remove(netdev->sa_query_timeout); + netdev->sa_query_timeout = NULL; +} + +static bool netdev_send_sa_query_request(struct netdev *netdev) +{ + uint8_t req[10]; + uint8_t *ptr = req; + + ptr[0] = 0x08; /* Category: SA Query */ + ptr[1] = 0x00; /* SA Query Action: Request */ + + /* Transaction ID */ + l_getrandom(ptr + 2, 2); + + ptr += 4; + + if (netdev->handshake->supplicant_ocvc && + netdev->handshake->authenticator_ocvc) { + if (netdev_build_oci(netdev, ptr) < 0) { + l_debug("Could not build OCI"); + return false; + } + + ptr += 6; + } + + if (!netdev_send_action_frame(netdev, netdev->handshake->aa, req, + ptr - req, netdev->frequency, + netdev_sa_query_req_cb, netdev)) { + l_error("error sending SA Query action frame"); + return false; + } + + netdev->sa_query_id = l_get_u16(req + 2); + netdev->sa_query_timeout = l_timeout_create(3, + netdev_sa_query_timeout, netdev, NULL); + + return true; +} + static void netdev_sa_query_req_frame_event(const struct mmpdu_header *hdr, const void *body, size_t body_len, int rssi, void *user_data) { - uint8_t sa_resp[4]; + uint8_t sa_resp[10]; + uint8_t *ptr = sa_resp; uint16_t transaction; + const uint8_t *oci; struct netdev *netdev = user_data; + bool ocvc = netdev->handshake->supplicant_ocvc && + netdev->handshake->authenticator_ocvc; if (body_len < 4) { l_debug("SA Query request too short"); @@ -4512,15 +4975,57 @@ static void netdev_sa_query_req_frame_event(const struct mmpdu_header *hdr, transaction = l_get_u16(body + 2); - sa_resp[0] = 0x08; /* SA Query */ - sa_resp[1] = 0x01; /* Response */ - memcpy(sa_resp + 2, &transaction, 2); + body_len -= 4; + + if (ocvc) { + /* + * IEEE 802.11 Section 11.13 + * + * "A STA that supports the SA Query procedure and receives an + * SA Query Request frame shall respond with an SA Query + * Response frame if none of the following are true... + * - OCI element is not present in the request or + * - Operating channel information indicated does not match the + * current channel information (see 12.2.9)." + */ + if (ie_parse_oci(body + 4, body_len, &oci) < 0) { + l_debug("Could not parse OCI"); + return; + } + + if (oci_verify(oci, netdev->handshake->chandef) < 0) { + l_debug("Could not verify OCI"); + return; + } + } + + ptr[0] = 0x08; /* SA Query */ + ptr[1] = 0x01; /* Response */ + memcpy(ptr + 2, &transaction, 2); + + ptr += 4; + + /* + * IEEE 802.11 Section 11.13 + * + * "A STA that responds with an SA Query Response frame to a STA that + * indicated OCVC capability shall include OCI element in the response + * frame if dot11RSNAOperatingChannelValidationActivated is true" + */ + if (ocvc) { + if (netdev_build_oci(netdev, ptr) < 0) { + l_debug("Could not build OCI"); + return; + } + + ptr += 6; + } l_info("received SA Query request from "MAC", transaction=%u", MAC_STR(hdr->address_2), transaction); if (!netdev_send_action_frame(netdev, netdev->handshake->aa, - sa_resp, sizeof(sa_resp), + sa_resp, ptr - sa_resp, netdev->frequency, netdev_sa_query_resp_cb, netdev)) { l_error("error sending SA Query response"); @@ -4533,15 +5038,22 @@ static void netdev_sa_query_resp_frame_event(const struct mmpdu_header *hdr, int rssi, void *user_data) { struct netdev *netdev = user_data; + const uint8_t *ptr = body; + const uint8_t *oci; + + if (!netdev->connected) + return; if (body_len < 4) { l_debug("SA Query frame too short"); return; } + ptr += 2; + l_debug("SA Query src="MAC" dest="MAC" bssid="MAC" transaction=%u", MAC_STR(hdr->address_2), MAC_STR(hdr->address_1), - MAC_STR(hdr->address_3), l_get_u16(body + 2)); + MAC_STR(hdr->address_3), l_get_u16(ptr)); if (!netdev->sa_query_timeout) { l_debug("no SA Query request sent"); @@ -4554,50 +5066,44 @@ static void netdev_sa_query_resp_frame_event(const struct mmpdu_header *hdr, return; } - if (memcmp(body + 2, &netdev->sa_query_id, 2)) { + if (memcmp(ptr, &netdev->sa_query_id, 2)) { l_debug("SA Query transaction ID's did not match"); return; } - l_info("SA Query response from connected BSS received, " - "keeping the connection active"); - - l_timeout_remove(netdev->sa_query_timeout); - netdev->sa_query_timeout = NULL; -} + if (!(netdev->handshake->supplicant_ocvc && + netdev->handshake->authenticator_ocvc)) + goto keep_alive; -static void netdev_sa_query_req_cb(struct l_genl_msg *msg, void *user_data) -{ - struct netdev *netdev = user_data; - int err = l_genl_msg_get_error(msg); - const char *ext_error; + ptr += 2; + body_len -= 4; - if (err >= 0) + /* + * IEEE 802.11 Section 11.13 + * + * "When a non-AP or non-PCP STA receives the SA Query Response frame + * from a STA that indicated OCVC capability, it shall ensure that OCI + * element is present in the response and the channel information in the + * OCI element matches current operating channel parameters + * (see 12.2.9). Otherwise, the receiving STA shall deem the response + * as invalid and discard it" + */ + if (ie_parse_oci(ptr, body_len, &oci) < 0) { + l_debug("Invalid OCI element"); return; + } - ext_error = l_genl_msg_get_extended_error(msg); - l_debug("error sending SA Query request: %s", - ext_error ? ext_error : strerror(-err)); - - l_timeout_remove(netdev->sa_query_timeout); - netdev->sa_query_timeout = NULL; -} - -static void netdev_sa_query_timeout(struct l_timeout *timeout, - void *user_data) -{ - struct netdev *netdev = user_data; - struct l_genl_msg *msg; + if (oci_verify(oci, netdev->handshake->chandef) < 0) { + l_debug("Could not verify OCI element"); + return; + } - l_info("SA Query timed out, connection is invalid. Disconnecting..."); +keep_alive: + l_info("SA Query response from connected BSS received, " + "keeping the connection active"); l_timeout_remove(netdev->sa_query_timeout); netdev->sa_query_timeout = NULL; - - msg = netdev_build_cmd_disconnect(netdev, - MMPDU_REASON_CODE_PREV_AUTH_NOT_VALID); - netdev->disconnect_cmd_id = l_genl_family_send(nl80211, msg, - netdev_disconnect_cb, netdev, NULL); } static void netdev_unprot_disconnect_event(struct l_genl_msg *msg, @@ -4608,15 +5114,16 @@ static void netdev_unprot_disconnect_event(struct l_genl_msg *msg, uint16_t type; uint16_t len; const void *data; - uint8_t action_frame[4]; uint8_t reason_code; if (!netdev->connected) return; /* ignore excessive disassociate requests */ - if (netdev->sa_query_timeout) + if (netdev->sa_query_timeout) { + l_debug("SA Query already in progress, ignoring"); return; + } if (!l_genl_attr_init(&attr, msg)) return; @@ -4652,23 +5159,7 @@ static void netdev_unprot_disconnect_event(struct l_genl_msg *msg, return; } - action_frame[0] = 0x08; /* Category: SA Query */ - action_frame[1] = 0x00; /* SA Query Action: Request */ - - /* Transaction ID */ - l_getrandom(action_frame + 2, 2); - - if (!netdev_send_action_frame(netdev, netdev->handshake->aa, - action_frame, sizeof(action_frame), - netdev->frequency, - netdev_sa_query_req_cb, netdev)) { - l_error("error sending SA Query action frame"); - return; - } - - netdev->sa_query_id = l_get_u16(action_frame + 2); - netdev->sa_query_timeout = l_timeout_create(3, - netdev_sa_query_timeout, netdev, NULL); + netdev_send_sa_query_request(netdev); } static void netdev_station_event(struct l_genl_msg *msg, @@ -4866,42 +5357,54 @@ failed: } -static void netdev_channel_switch_event(struct l_genl_msg *msg, - struct netdev *netdev) +static void netdev_send_sa_query_delay(struct l_timeout *timeout, + void *user_data) { - struct l_genl_attr attr; - uint16_t type, len; - const void *data; - uint32_t *freq = NULL; + struct netdev *netdev = user_data; - l_debug(""); + netdev_send_sa_query_request(netdev); - if (!l_genl_attr_init(&attr, msg)) - return; + l_timeout_remove(netdev->sa_query_delay); + netdev->sa_query_delay = NULL; +} - while (l_genl_attr_next(&attr, &type, &len, &data)) { - switch (type) { - case NL80211_ATTR_WIPHY_FREQ: - if (len != 4) - continue; +static void netdev_channel_switch_event(struct l_genl_msg *msg, + struct netdev *netdev) +{ + _auto_(l_free) struct band_chandef *chandef = + l_new(struct band_chandef, 1); - freq = (uint32_t *) data; - break; - } + if (nl80211_parse_chandef(msg, chandef) < 0) { + l_debug("Couldn't parse operating channel info."); + return; } - if (!freq) - return; + netdev->frequency = chandef->frequency; + + l_debug("Channel switch event, frequency: %u", netdev->frequency); - l_debug("Channel switch event, frequency: %u", *freq); + handshake_state_set_chandef(netdev->handshake, l_steal_ptr(chandef)); - netdev->frequency = *freq; + /* + * IEEE 802.11-2020 11.9.3.2 + * "If the STA chooses to perform the specified switch and + * dot11RSNAOperatingChannelValidationActivated is true and the AP has + * indicated OCVC capability, after switching to the new channel the STA + * shall wait a random delay uniformly-distributed in the range between + * zero and 5000us, and then initiate the SA query procedure" + */ + if (netdev->handshake->supplicant_ocvc && + netdev->handshake->authenticator_ocvc) + netdev->sa_query_delay = l_timeout_create_ms( + l_getrandom_uint32() % 5, + netdev_send_sa_query_delay, + netdev, NULL); if (!netdev->event_filter) return; - netdev->event_filter(netdev, NETDEV_EVENT_CHANNEL_SWITCHED, freq, - netdev->user_data); + netdev->event_filter(netdev, NETDEV_EVENT_CHANNEL_SWITCHED, + &netdev->frequency, netdev->user_data); } static void netdev_mlme_notify(struct l_genl_msg *msg, void *user_data) @@ -5043,107 +5546,6 @@ static void netdev_control_port_frame_event(struct l_genl_msg *msg, frame, frame_len, unencrypted); } -static struct l_genl_msg *netdev_build_control_port_frame(struct netdev *netdev, - const uint8_t *to, - uint16_t proto, - bool unencrypted, - const void *body, - size_t body_len) -{ - struct l_genl_msg *msg; - - msg = l_genl_msg_new_sized(NL80211_CMD_CONTROL_PORT_FRAME, - 128 + body_len); - - l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &netdev->index); - l_genl_msg_append_attr(msg, NL80211_ATTR_FRAME, body_len, body); - l_genl_msg_append_attr(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, 2, - &proto); - l_genl_msg_append_attr(msg, NL80211_ATTR_MAC, ETH_ALEN, to); - - if (unencrypted) - l_genl_msg_append_attr(msg, - NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT, 0, NULL); - - return msg; -} - -static void netdev_control_port_frame_cb(struct l_genl_msg *msg, - void *user_data) -{ - int err = l_genl_msg_get_error(msg); - const char *ext_error; - - if (err >= 0) - return; - - ext_error = l_genl_msg_get_extended_error(msg); - l_error("CMD_CONTROL_PORT failed: %s", - ext_error ? ext_error : strerror(-err)); -} - -static int netdev_control_port_write_pae(struct netdev *netdev, - const uint8_t *dest, - uint16_t proto, - const struct eapol_frame *ef, - bool noencrypt) -{ - int fd = l_io_get_fd(netdev->pae_io); - struct sockaddr_ll sll; - size_t frame_size = sizeof(struct eapol_header) + - L_BE16_TO_CPU(ef->header.packet_len); - ssize_t r; - - memset(&sll, 0, sizeof(sll)); - sll.sll_family = AF_PACKET; - sll.sll_ifindex = netdev->index; - sll.sll_protocol = htons(proto); - sll.sll_halen = ETH_ALEN; - memcpy(sll.sll_addr, dest, ETH_ALEN); - - r = sendto(fd, ef, frame_size, 0, - (struct sockaddr *) &sll, sizeof(sll)); - if (r < 0) - l_error("EAPoL write socket: %s", strerror(errno)); - - return r; -} - -static int netdev_control_port_frame(uint32_t ifindex, - const uint8_t *dest, uint16_t proto, - const struct eapol_frame *ef, - bool noencrypt, - void *user_data) -{ - struct l_genl_msg *msg; - struct netdev *netdev; - size_t frame_size; - - netdev = netdev_find(ifindex); - if (!netdev) - return -ENOENT; - - frame_size = sizeof(struct eapol_header) + - L_BE16_TO_CPU(ef->header.packet_len); - - if (!netdev->pae_over_nl80211) - return netdev_control_port_write_pae(netdev, dest, proto, - ef, noencrypt); - - msg = netdev_build_control_port_frame(netdev, dest, proto, noencrypt, - ef, frame_size); - if (!msg) - return -ENOMEM; - - if (!l_genl_family_send(nl80211, msg, netdev_control_port_frame_cb, - netdev, NULL)) { - l_genl_msg_unref(msg); - return -EINVAL; - } - - return 0; -} - static void netdev_unicast_notify(struct l_genl_msg *msg, void *user_data) { struct netdev *netdev = NULL; @@ -5156,7 +5558,7 @@ static void netdev_unicast_notify(struct l_genl_msg *msg, void *user_data) if (!cmd) return; - l_debug("Unicast notification %u", cmd); + l_debug("Unicast notification %s(%u)", nl80211cmd_to_string(cmd), cmd); if (!l_genl_attr_init(&attr, msg)) return; @@ -6016,9 +6418,7 @@ struct netdev *netdev_create_from_genl(struct l_genl_msg *msg, return NULL; } - if (!wiphy_has_ext_feature(wiphy, - NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211) || - !pae_over_nl80211) { + if (!wiphy_control_port_enabled(wiphy)) { pae_io = pae_open(ifindex); if (!pae_io) { l_error("Unable to open PAE interface"); @@ -6155,10 +6555,6 @@ static int netdev_init(void) &LOW_SIGNAL_THRESHOLD_5GHZ)) LOW_SIGNAL_THRESHOLD_5GHZ = -76; - if (!l_settings_get_bool(settings, "General", "ControlPortOverNL80211", - &pae_over_nl80211)) - pae_over_nl80211 = true; - rand_addr_str = l_settings_get_value(settings, "General", "AddressRandomization"); if (rand_addr_str && !strcmp(rand_addr_str, "network")) @@ -6170,6 +6566,7 @@ static int netdev_init(void) __handshake_set_install_tk_func(netdev_set_tk); __handshake_set_install_gtk_func(netdev_set_gtk); __handshake_set_install_igtk_func(netdev_set_igtk); + __handshake_set_install_ext_tk_func(netdev_set_ext_tk); __eapol_set_rekey_offload_func(netdev_set_rekey_offload); __eapol_set_tx_packet_func(netdev_control_port_frame); diff --git a/src/network.c b/src/network.c @@ -59,7 +59,7 @@ #define SAE_PT_SETTING "SAE-PT-Group%u" static uint32_t known_networks_watch; -static uint32_t anqp_watch; +static uint32_t event_watch; struct network { char ssid[33]; @@ -83,11 +83,13 @@ struct network { 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 */ + bool owe_hidden_pending:1; uint8_t transition_disable; /* Temporary cache until info is set */ bool have_transition_disable:1; int rank; /* Holds DBus Connect() message if it comes in before ANQP finishes */ struct l_dbus_message *connect_after_anqp; + struct l_dbus_message *connect_after_owe_hidden; }; static bool network_settings_load(struct network *network) @@ -390,6 +392,11 @@ struct l_settings *network_get_settings(const struct network *network) return network->settings; } +struct station *network_get_station(const struct network *network) +{ + return network->station; +} + static bool network_set_8021x_secrets(struct network *network) { const struct l_queue_entry *entry; @@ -465,7 +472,7 @@ static int network_set_handshake_secrets_psk(struct network *network, return 0; } -int network_handshake_setup(struct network *network, +int network_handshake_setup(struct network *network, struct scan_bss *bss, struct handshake_state *hs) { struct station *station = network->station; @@ -492,8 +499,7 @@ int network_handshake_setup(struct network *network, return -ENOTSUP; } - handshake_state_set_ssid(hs, (void *) network->ssid, - strlen(network->ssid)); + handshake_state_set_ssid(hs, bss->ssid, bss->ssid_len); if (settings && l_settings_get_uint(settings, "EAPoL", "ProtocolVersion", @@ -1131,6 +1137,17 @@ struct scan_bss *network_bss_select(struct network *network, if (!candidate) candidate = bss; + /* OWE Transition BSS */ + if (bss->owe_trans) { + /* Don't want to connect to the Open BSS if possible */ + if (!bss->rsne) + continue; + + /* Candidate is not OWE, set this as new candidate */ + if (!(candidate->owe_trans && candidate->rsne)) + candidate = bss; + } + /* check if temporarily blacklisted */ if (l_queue_find(network->blacklist, match_bss, bss)) continue; @@ -1515,6 +1532,17 @@ struct l_dbus_message *__network_connect(struct network *network, case SECURITY_PSK: return network_connect_psk(network, bss, message); case SECURITY_NONE: + if (network->connect_after_owe_hidden) + return dbus_error_busy(message); + + /* Save message and connect after OWE hidden scan is done */ + if (network->owe_hidden_pending) { + network->connect_after_owe_hidden = + l_dbus_message_ref(message); + l_debug("Pending OWE hidden scan, delaying connect"); + return NULL; + } + station_connect_network(station, network, bss, message); return NULL; case SECURITY_8021X: @@ -1724,6 +1752,14 @@ static void network_unregister(struct network *network, int reason) struct l_dbus *dbus = dbus_get_bus(); #endif + if (network->connect_after_anqp) + dbus_pending_reply(&network->connect_after_anqp, + dbus_error_aborted(network->connect_after_anqp)); + + if (network->connect_after_owe_hidden) + dbus_pending_reply(&network->connect_after_owe_hidden, + dbus_error_aborted(network->connect_after_owe_hidden)); + agent_request_cancel(network->agent_request, reason); network_settings_close(network); @@ -1913,13 +1949,20 @@ static void known_networks_changed(enum known_networks_event event, } } -static void anqp_watch_changed(enum station_anqp_state state, +static void event_watch_changed(enum station_event state, struct network *network, void *user_data) { - network->anqp_pending = state == STATION_ANQP_STARTED; + struct l_dbus_message *reply; - if (state == STATION_ANQP_FINISHED && network->connect_after_anqp) { - struct l_dbus_message *reply; + switch (state) { + case STATION_EVENT_ANQP_STARTED: + network->anqp_pending = true; + break; + case STATION_EVENT_ANQP_FINISHED: + network->anqp_pending = false; + + if (!network->connect_after_anqp) + return; l_debug("ANQP complete, resuming connect to %s", network->ssid); @@ -1943,6 +1986,25 @@ static void anqp_watch_changed(enum station_anqp_state state, l_dbus_message_unref(network->connect_after_anqp); #endif network->connect_after_anqp = NULL; + + break; + case STATION_EVENT_OWE_HIDDEN_STARTED: + network->owe_hidden_pending = true; + break; + case STATION_EVENT_OWE_HIDDEN_FINISHED: + network->owe_hidden_pending = false; + + if (!network->connect_after_owe_hidden) + return; + + station_connect_network(network->station, network, + network_bss_select(network, true), + network->connect_after_owe_hidden); + + l_dbus_message_unref(network->connect_after_owe_hidden); + network->connect_after_owe_hidden = NULL; + + break; } } @@ -1981,7 +2043,7 @@ 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); + event_watch = station_add_event_watch(event_watch_changed, NULL, NULL); return 0; } @@ -1991,8 +2053,8 @@ 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; + station_remove_event_watch(event_watch); + event_watch = 0; #ifdef HAVE_DBUS l_dbus_unregister_interface(dbus_get_bus(), IWD_NETWORK_INTERFACE); diff --git a/src/network.h b/src/network.h @@ -44,13 +44,14 @@ enum security network_get_security(const struct network *network); bool network_set_passphrase(struct network *network, const char *passphrase); int network_get_signal_strength(const struct network *network); struct l_settings *network_get_settings(const struct network *network); +struct station *network_get_station(const struct network *network); bool network_set_psk(struct network *network, const uint8_t *psk); int network_set_transition_disable(struct network *network, const uint8_t *td, size_t len); -int network_handshake_setup(struct network *network, +int network_handshake_setup(struct network *network, struct scan_bss *bss, struct handshake_state *hs); void network_sync_settings(struct network *network); diff --git a/src/nl80211cmd.c b/src/nl80211cmd.c @@ -142,7 +142,8 @@ static const struct { { NL80211_CMD_GET_MPP, "Get Mesh Proxy Path" }, { NL80211_CMD_JOIN_OCB, "Join OCB Network" }, { NL80211_CMD_LEAVE_OCB, "Leave OCB Network" }, - { NL80211_CMD_CH_SWITCH_STARTED_NOTIFY, "Channel Switch Notify" }, + { NL80211_CMD_CH_SWITCH_STARTED_NOTIFY, + "Channel Switch Started Notify"}, { NL80211_CMD_TDLS_CHANNEL_SWITCH, "TDLS Channel Switch" }, { NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH, "Cancel TLDS Channel Switch" }, diff --git a/src/nl80211util.c b/src/nl80211util.c @@ -31,6 +31,7 @@ #include "linux/nl80211.h" #include "src/nl80211util.h" +#include "src/band.h" typedef bool (*attr_handler)(const void *data, uint16_t len, void *o); @@ -138,6 +139,11 @@ static attr_handler handler_for_type(enum nl80211_attrs type) case NL80211_ATTR_ACK: return extract_flag; case NL80211_ATTR_WIPHY_FREQ: + case NL80211_ATTR_WIPHY_FREQ_OFFSET: + case NL80211_ATTR_WIPHY_CHANNEL_TYPE: + case NL80211_ATTR_CHANNEL_WIDTH: + case NL80211_ATTR_CENTER_FREQ1: + case NL80211_ATTR_CENTER_FREQ2: return extract_uint32; default: break; @@ -241,6 +247,7 @@ struct l_genl_msg *nl80211_build_new_key_group(uint32_t ifindex, uint32_t cipher size_t ctr_len, const uint8_t *addr) { struct l_genl_msg *msg; + uint32_t type = NL80211_KEYTYPE_GROUP; msg = l_genl_msg_new_sized(NL80211_CMD_NEW_KEY, 512); @@ -257,16 +264,11 @@ struct l_genl_msg *nl80211_build_new_key_group(uint32_t ifindex, uint32_t cipher if (ctr) l_genl_msg_append_attr(msg, NL80211_KEY_SEQ, ctr_len, ctr); - if (addr) { - uint32_t type = NL80211_KEYTYPE_GROUP; - - l_genl_msg_append_attr(msg, NL80211_KEY_TYPE, 4, &type); - l_genl_msg_enter_nested(msg, NL80211_KEY_DEFAULT_TYPES); - l_genl_msg_append_attr(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST, + l_genl_msg_append_attr(msg, NL80211_KEY_TYPE, 4, &type); + l_genl_msg_enter_nested(msg, NL80211_KEY_DEFAULT_TYPES); + l_genl_msg_append_attr(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST, 0, NULL); - l_genl_msg_leave_nested(msg); - } - + l_genl_msg_leave_nested(msg); l_genl_msg_leave_nested(msg); return msg; @@ -430,3 +432,27 @@ struct l_genl_msg *nl80211_build_cmd_frame(uint32_t ifindex, return msg; } + +int nl80211_parse_chandef(struct l_genl_msg *msg, struct band_chandef *out) +{ + struct band_chandef t; + + if (nl80211_parse_attrs(msg, + NL80211_ATTR_WIPHY_FREQ, &t.frequency, + NL80211_ATTR_CHANNEL_WIDTH, &t.channel_width, + NL80211_ATTR_CENTER_FREQ1, &t.center1_frequency, + NL80211_ATTR_UNSPEC) < 0) + return -ENOENT; + + t.center2_frequency = 0; + + /* Try to parse CENTER_FREQ2, but it is given only for 80P80 */ + if (t.channel_width == NL80211_CHAN_WIDTH_80P80 && + nl80211_parse_attrs(msg, + NL80211_ATTR_CENTER_FREQ2, &t.center2_frequency, + NL80211_ATTR_UNSPEC) < 0) + return -ENOENT; + + memcpy(out, &t, sizeof(t)); + return 0; +} diff --git a/src/nl80211util.h b/src/nl80211util.h @@ -22,6 +22,8 @@ #include <ell/ell.h> +struct band_chandef; + int nl80211_parse_attrs(struct l_genl_msg *msg, int tag, ...); struct l_genl_msg *nl80211_build_new_key_group(uint32_t ifindex, @@ -51,3 +53,5 @@ struct l_genl_msg *nl80211_build_cmd_frame(uint32_t ifindex, uint32_t freq, struct iovec *iov, size_t iov_len); + +int nl80211_parse_chandef(struct l_genl_msg *msg, struct band_chandef *out); diff --git a/src/p2p.c b/src/p2p.c @@ -1171,7 +1171,6 @@ static size_t p2p_group_write_wfd_ie(struct p2p_device *dev, return 0; } - static size_t p2p_group_get_ies_len(enum mpdu_management_subtype type, const struct mmpdu_header *client_frame, size_t client_frame_len, @@ -1333,8 +1332,7 @@ static void p2p_start_client_netconfig(struct p2p_device *dev) settings = dev->conn_netconfig_settings ?: p2p_dhcp_settings; - if (!netconfig_load_settings(dev->conn_netconfig, settings, - dev->conn_addr) || + if (!netconfig_load_settings(dev->conn_netconfig, settings) || !netconfig_configure(dev->conn_netconfig, p2p_netconfig_event_handler, dev)) { @@ -1984,7 +1982,8 @@ static void p2p_provision_scan_start(struct p2p_device *dev) params.flush = true; params.no_cck_rates = true; - params.ssid = dev->go_group_id.ssid; + params.ssid = (const uint8_t *)dev->go_group_id.ssid; + params.ssid_len = strlen(dev->go_group_id.ssid); params.extra_ie = p2p_build_scan_ies(dev, buf, sizeof(buf), &params.extra_ie_size); L_WARN_ON(!params.extra_ie); @@ -3787,7 +3786,8 @@ static bool p2p_device_scan_start(struct p2p_device *dev) L_WARN_ON(!params.extra_ie); params.flush = true; /* P2P Wildcard SSID because we don't need legacy networks to reply */ - params.ssid = "DIRECT-"; + params.ssid = (const uint8_t *)"DIRECT-"; + params.ssid_len = strlen("DIRECT-"); /* * Must send probe requests at 6Mb/s, OFDM only. The no-CCK rates * flag forces the drivers to do exactly this for 2.4GHz frames. @@ -4718,6 +4718,18 @@ static bool p2p_peer_get_name(struct l_dbus *dbus, return true; } +static bool p2p_peer_get_addr(struct l_dbus *dbus, + struct l_dbus_message *message, + struct l_dbus_message_builder *builder, + void *user_data) +{ + struct p2p_peer *peer = user_data; + const char *addr = util_address_to_string(peer->device_addr); + + l_dbus_message_builder_append_basic(builder, 's', addr); + return true; +} + static bool p2p_peer_get_device(struct l_dbus *dbus, struct l_dbus_message *message, struct l_dbus_message_builder *builder, @@ -4835,6 +4847,8 @@ static void p2p_peer_interface_setup(struct l_dbus_interface *interface) { l_dbus_interface_property(interface, "Name", 0, "s", p2p_peer_get_name, NULL); + l_dbus_interface_property(interface, "Address", 0, "s", + p2p_peer_get_addr, NULL); l_dbus_interface_property(interface, "Device", 0, "o", p2p_peer_get_device, NULL); l_dbus_interface_property(interface, "DeviceCategory", 0, "s", diff --git a/src/resolve.c b/src/resolve.c @@ -35,6 +35,7 @@ #include "src/iwd.h" #include "src/module.h" #include "src/dbus.h" +#include "src/netconfig.h" #include "src/resolve.h" struct resolve_ops { @@ -582,18 +583,9 @@ static const struct { static int resolve_init(void) { const char *method_name; - bool enabled; uint8_t i; - if (!l_settings_get_bool(iwd_get_config(), "General", - "EnableNetworkConfiguration", - &enabled)) { - if (!l_settings_get_bool(iwd_get_config(), "General", - "enable_network_config", &enabled)) - enabled = false; - } - - if (!enabled) + if (!netconfig_enabled()) return 0; method_name = l_settings_get_value(iwd_get_config(), "Network", diff --git a/src/rrm.c b/src/rrm.c @@ -570,7 +570,7 @@ static void rrm_handle_beacon_request(struct rrm_state *rrm, break; case RRM_BEACON_REQ_SUBELEM_ID_REPORTING_DETAIL: - if (length != 1) { + if (length < 1) { l_error("Invalid length in reporting detail"); goto reject_refused; } @@ -578,6 +578,11 @@ static void rrm_handle_beacon_request(struct rrm_state *rrm, detail = l_get_u8(data); break; case RRM_BEACON_REQ_SUBELEM_ID_BEACON_REPORTING: + if (length < 2) { + l_error("Invalid length in Beacon Reporting"); + goto reject_refused; + } + /* * 802.11-2016 9.4.2.21.7 * @@ -585,9 +590,19 @@ static void rrm_handle_beacon_request(struct rrm_state *rrm, * present in a Beacon request for repeated * measurements; otherwise it is not present" * - * Since repeated measurements are not supported we can - * reject this request now. + * However, some implementations send an all-zero + * Beacon Reporting subelement. Reporting Condition + * of zero is 'default, used when the Beacon Reporting + * subelement is not included in a Beacon request).' + * Treat such elements as if they're not present. + * + * Otherwise, since repeated measurements are not + * supported we can reject this request now. */ + if (l_get_u8(data) != 0) + goto reject_incapable; + + break; case RRM_BEACON_REQ_SUBELEM_ID_AP_CHAN_REPORT: /* * Only supporting single channel requests diff --git a/src/scan.c b/src/scan.c @@ -49,6 +49,7 @@ #include "src/util.h" #include "src/p2putil.h" #include "src/mpdu.h" +#include "src/band.h" #include "src/scan.h" /* User configurable options */ @@ -542,7 +543,7 @@ static void scan_cmds_add(struct l_queue *cmds, struct scan_context *sc, if (params->ssid) { /* direct probe request scan */ l_genl_msg_append_attr(cmd, NL80211_ATTR_SSID, - strlen(params->ssid), params->ssid); + params->ssid_len, params->ssid); l_genl_msg_leave_nested(cmd); l_queue_push_tail(cmds, cmd); @@ -583,6 +584,27 @@ static const struct wiphy_radio_work_item_ops work_ops = { .destroy = scan_request_free, }; +static struct scan_request *scan_request_new(struct scan_context *sc, + bool passive, + scan_trigger_func_t trigger, + scan_notify_func_t notify, + void *userdata, + scan_destroy_func_t destroy) +{ + struct scan_request *sr; + + sr = l_new(struct scan_request, 1); + sr->sc = sc; + sr->trigger = trigger; + sr->callback = notify; + sr->userdata = userdata; + sr->destroy = destroy; + sr->passive = passive; + sr->cmds = l_queue_new(); + + return sr; +} + static uint32_t scan_common(uint64_t wdev_id, bool passive, const struct scan_parameters *params, scan_trigger_func_t trigger, @@ -597,14 +619,7 @@ static uint32_t scan_common(uint64_t wdev_id, bool passive, if (!sc) return 0; - sr = l_new(struct scan_request, 1); - sr->sc = sc; - sr->trigger = trigger; - sr->callback = notify; - sr->userdata = userdata; - sr->destroy = destroy; - sr->passive = passive; - sr->cmds = l_queue_new(); + sr = scan_request_new(sc, passive, trigger, notify, userdata, destroy); scan_cmds_add(sr->cmds, sc, passive, params); @@ -656,6 +671,133 @@ uint32_t scan_active_full(uint64_t wdev_id, trigger, notify, userdata, destroy); } +static void scan_add_owe_freq(struct scan_freq_set *freqs, + const struct scan_bss *bss) +{ + int freq; + + if (bss->owe_trans->oper_class) + freq = oci_to_frequency(bss->owe_trans->oper_class, + bss->owe_trans->channel); + else + freq = bss->frequency; + + L_WARN_ON(freq < 0); + + scan_freq_set_add(freqs, freq); +} + +static void add_owe_scan_cmd(struct scan_context *sc, struct scan_request *sr, + bool ignore_flush, + struct scan_freq_set *freqs, + const struct scan_bss *bss) +{ + struct l_genl_msg *cmd; + struct scan_parameters params = {}; + struct scan_freq_set *tmp; + + if (!freqs) { + tmp = scan_freq_set_new(); + + scan_add_owe_freq(tmp, bss); + + params.freqs = tmp; + } else + params.freqs = freqs; + + params.ssid = bss->owe_trans->ssid; + params.ssid_len = bss->owe_trans->ssid_len; + params.flush = true; + + cmd = scan_build_cmd(sc, ignore_flush, false, &params); + + l_genl_msg_enter_nested(cmd, NL80211_ATTR_SCAN_SSIDS); + l_genl_msg_append_attr(cmd, 0, params.ssid_len, params.ssid); + l_genl_msg_leave_nested(cmd); + + l_queue_push_tail(sr->cmds, cmd); + + if (!freqs) + scan_freq_set_free(tmp); +} + +uint32_t scan_owe_hidden(uint64_t wdev_id, struct l_queue *list, + scan_trigger_func_t trigger, scan_notify_func_t notify, + void *userdata, scan_destroy_func_t destroy) +{ + struct scan_context *sc; + struct scan_request *sr; + struct scan_freq_set *freqs; + const struct l_queue_entry *entry; + const uint8_t *ssid = NULL; + size_t ssid_len; + bool same_ssid = true; + struct scan_bss *bss; + bool ignore_flush = false; + + sc = l_queue_find(scan_contexts, scan_context_match, &wdev_id); + + if (!sc) + return 0; + + sr = scan_request_new(sc, false, trigger, notify, userdata, destroy); + + freqs = scan_freq_set_new(); + + /* + * Start building up a frequency list if all SSIDs are the same. This + * is hopefully the common case and will allow a single scan command. + */ + for (entry = l_queue_get_entries(list); entry; entry = entry->next) { + bss = entry->data; + + scan_add_owe_freq(freqs, bss); + + /* First */ + if (!ssid) { + ssid = bss->owe_trans->ssid; + ssid_len = bss->owe_trans->ssid_len; + continue; + } + + if (ssid_len == bss->owe_trans->ssid_len && + !memcmp(ssid, bss->owe_trans->ssid, + bss->owe_trans->ssid_len)) + continue; + + same_ssid = false; + break; + } + + if (same_ssid) { + bss = l_queue_peek_head(list); + + add_owe_scan_cmd(sc, sr, ignore_flush, freqs, bss); + + scan_freq_set_free(freqs); + + goto done; + } + + scan_freq_set_free(freqs); + + /* SSIDs differed, use separate scan commands. */ + for (entry = l_queue_get_entries(list); entry; entry = entry->next) { + bss = entry->data; + + add_owe_scan_cmd(sc, sr, ignore_flush, NULL, bss); + + /* Ignore flush on all subsequent commands */ + if (!ignore_flush) + ignore_flush = true; + } + +done: + l_queue_push_tail(sc->requests, sr); + + return wiphy_radio_work_insert(sc->wiphy, &sr->work, 2, &work_ops); +} + bool scan_cancel(uint64_t wdev_id, uint32_t id) { struct scan_context *sc; @@ -936,6 +1078,22 @@ static void scan_parse_vendor_specific(struct scan_bss *bss, const void *data, return; } + if (is_ie_wfa_ie(data, len, IE_WFA_OI_OWE_TRANSITION)) { + _auto_(l_free) struct ie_owe_transition_info *owe_trans = + l_new(struct ie_owe_transition_info, 1); + + if (ie_parse_owe_transition(data - 2, len + 2, owe_trans) < 0) + return; + + if (owe_trans->oper_class && + oci_to_frequency(owe_trans->oper_class, + owe_trans->channel) < 0) + return; + + bss->owe_trans = l_steal_ptr(owe_trans); + return; + } + if (!ie_parse_network_cost(data, len, &cost_level, &cost_flags)) { bss->cost_level = cost_level; bss->cost_flags = cost_flags; @@ -1381,6 +1539,7 @@ void scan_bss_free(struct scan_bss *bss) l_free(bss->osen); l_free(bss->rc_ie); l_free(bss->wfd); + l_free(bss->owe_trans); switch (bss->source_frame) { case SCAN_BSS_PROBE_RESP: diff --git a/src/scan.h b/src/scan.h @@ -22,6 +22,7 @@ struct scan_freq_set; struct ie_rsn_info; +struct ie_owe_transition_info; struct p2p_probe_resp; struct p2p_probe_req; struct p2p_beacon; @@ -62,6 +63,7 @@ struct scan_bss { struct p2p_probe_req *p2p_probe_req_info; struct p2p_beacon *p2p_beacon_info; }; + struct ie_owe_transition_info *owe_trans; uint8_t mde[3]; uint8_t ssid[32]; uint8_t ssid_len; @@ -97,7 +99,8 @@ struct scan_parameters { bool randomize_mac_addr_hint : 1; bool no_cck_rates : 1; bool duration_mandatory : 1; - const char *ssid; /* Used for direct probe request */ + const uint8_t *ssid; /* Used for direct probe request */ + size_t ssid_len; const uint8_t *source_mac; }; @@ -143,6 +146,9 @@ uint32_t scan_active_full(uint64_t wdev_id, const struct scan_parameters *params, scan_trigger_func_t trigger, scan_notify_func_t notify, void *userdata, scan_destroy_func_t destroy); +uint32_t scan_owe_hidden(uint64_t wdev_id, struct l_queue *list, + scan_trigger_func_t trigger, scan_notify_func_t notify, + void *userdata, scan_destroy_func_t destroy); bool scan_cancel(uint64_t wdev_id, uint32_t id); void scan_periodic_start(uint64_t wdev_id, scan_trigger_func_t trigger, diff --git a/src/station.c b/src/station.c @@ -63,8 +63,7 @@ static uint32_t netdev_watch; static uint32_t mfp_setting; static uint32_t roam_retry_interval; static bool anqp_disabled; -static bool netconfig_enabled; -static struct watchlist anqp_watches; +static struct watchlist event_watches; struct station { enum station_state state; @@ -87,6 +86,7 @@ struct station { uint32_t dbus_scan_id; uint32_t quick_scan_id; uint32_t hidden_network_scan_id; + struct l_queue *owe_hidden_scan_ids; /* Roaming related members */ struct timespec roam_min_time; @@ -114,6 +114,7 @@ struct station { bool ap_directed_roaming : 1; bool scanning : 1; bool autoconnect : 1; + bool autoconnect_can_start : 1; }; struct anqp_entry { @@ -189,11 +190,22 @@ static void station_property_set_scanning(struct station *station, static void station_enter_state(struct station *station, enum station_state state); +static void network_add_foreach(struct network *network, void *user_data) +{ + struct station *station = user_data; + + l_queue_insert(station->autoconnect_list, network, + network_rank_compare, NULL); +} + static int station_autoconnect_next(struct station *station) { struct network *network; int r; + if (!station->autoconnect_list) + return -ENOENT; + while ((network = l_queue_pop_head(station->autoconnect_list))) { const char *ssid = network_get_ssid(network); struct scan_bss *bss = network_bss_select(network, false); @@ -231,6 +243,31 @@ static int station_autoconnect_next(struct station *station) return -ENOENT; } +static void station_autoconnect_start(struct station *station) +{ + l_debug(""); + + if (!station->autoconnect_can_start) + return; + + if (!station_is_autoconnecting(station)) + return; + + if (!l_queue_isempty(station->anqp_pending)) + return; + + if (!l_queue_isempty(station->owe_hidden_scan_ids)) + return; + + if (L_WARN_ON(station->autoconnect_list)) + l_queue_destroy(station->autoconnect_list, NULL); + + station->autoconnect_list = l_queue_new(); + station_network_foreach(station, network_add_foreach, station); + station_autoconnect_next(station); + station->autoconnect_can_start = false; +} + static void bss_free(void *data) { struct scan_bss *bss = data; @@ -363,6 +400,43 @@ static struct network *station_add_seen_bss(struct station *station, if (station_parse_bss_security(station, bss, &security) < 0) return NULL; + /* Hidden OWE transition network */ + if (security == SECURITY_NONE && bss->rsne && bss->owe_trans) { + struct ie_owe_transition_info *info = bss->owe_trans; + /* + * WiFi Alliance OWE Specification v1.1 - Section 2.2.1: + * + * "2. An OWE AP shall use two different SSIDs, one for OWE + * and one for Open" + * + * "4. The OWE BSS shall include the OWE Transition Mode element + * in all Beacon and Probe Response frames to encapsulate + * the BSSID and SSID of the Open BSS" + * + * Meaning the hidden SSID should not match the SSID in the + * hidden network's OWE IE. Might as well restrict BSSID as well + * to be safe. + * + * In addition this SSID must be a valid utf8 string otherwise + * we could not look up the network. Note that this is not true + * for the open BSS IE, it can be non-utf8. + */ + if (!util_ssid_is_utf8(info->ssid_len, info->ssid)) + return NULL; + + if (!memcmp(info->ssid, bss->ssid, bss->ssid_len)) + return NULL; + + if (!memcmp(info->bssid, bss->addr, 6)) + return NULL; + + memcpy(ssid, info->ssid, info->ssid_len); + ssid[info->ssid_len] = '\0'; + + l_debug("Found hidden OWE network, using %s for network lookup", + ssid); + } + path = iwd_network_get_path(station, ssid, security); network = l_hashmap_lookup(station->networks, path); @@ -458,14 +532,6 @@ static bool match_nai_realms(const struct network_info *info, void *user_data) return true; } -static void network_add_foreach(struct network *network, void *user_data) -{ - struct station *station = user_data; - - l_queue_insert(station->autoconnect_list, network, - network_rank_compare, NULL); -} - static bool match_pending(const void *a, const void *b) { const struct anqp_entry *entry = a; @@ -487,8 +553,8 @@ 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); + WATCHLIST_NOTIFY(&event_watches, station_event_watch_func_t, + STATION_EVENT_ANQP_FINISHED, e->network); remove_anqp(e); @@ -554,13 +620,7 @@ request_done: /* 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, NULL); - station->autoconnect_list = l_queue_new(); - - if (station_is_autoconnecting(station)) { - station_network_foreach(station, network_add_foreach, station); - station_autoconnect_next(station); - } + station_autoconnect_start(station); } static bool station_start_anqp(struct station *station, struct network *network, @@ -622,11 +682,175 @@ 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); + WATCHLIST_NOTIFY(&event_watches, station_event_watch_func_t, + STATION_EVENT_ANQP_STARTED, network); return true; } +static bool network_has_open_pair(struct network *network, struct scan_bss *owe) +{ + const struct l_queue_entry *entry; + struct ie_owe_transition_info *owe_info = owe->owe_trans; + + for (entry = network_bss_list_get_entries(network); entry; + entry = entry->next) { + struct scan_bss *open = entry->data; + struct ie_owe_transition_info *open_info = open->owe_trans; + + /* + * Check if this is an Open/Hidden pair: + * + * Open SSID equals the SSID in OWE IE + * Open BSSID equals the BSSID in OWE IE + * + * OWE SSID equals the SSID in Open IE + * OWE BSSID equals the BSSID in Open IE + */ + if (open->ssid_len == owe_info->ssid_len && + open_info->ssid_len == owe->ssid_len && + !memcmp(open->ssid, owe_info->ssid, + open->ssid_len) && + !memcmp(open_info->ssid, owe->ssid, + owe->ssid_len) && + !memcmp(open->addr, owe_info->bssid, 6) && + !memcmp(open_info->bssid, owe->addr, 6)) + return true; + } + + return false; +} + +static bool station_owe_transition_results(int err, struct l_queue *bss_list, + const struct scan_freq_set *freqs, + void *userdata) +{ + struct network *network = userdata; + struct station *station = network_get_station(network); + struct scan_bss *bss; + + station_property_set_scanning(station, false); + + if (err) + goto done; + + while ((bss = l_queue_pop_head(bss_list))) { + /* + * Don't handle the open BSS, hidden BSS, BSS with no OWE + * Transition IE, or an IE with a non-utf8 SSID + */ + if (!bss->rsne || !bss->owe_trans || + util_ssid_is_hidden(bss->ssid_len, bss->ssid) || + !util_ssid_is_utf8(bss->owe_trans->ssid_len, + bss->owe_trans->ssid)) + goto free; + + /* Check if we have an open BSS that matches */ + if (!network_has_open_pair(network, bss)) + goto free; + + l_debug("Adding OWE transition network "MAC" to %s", + MAC_STR(bss->addr), network_get_ssid(network)); + + l_queue_push_tail(station->bss_list, bss); + network_bss_add(network, bss); + + continue; + +free: + scan_bss_free(bss); + } + + l_queue_destroy(bss_list, NULL); + +done: + l_queue_pop_head(station->owe_hidden_scan_ids); + + WATCHLIST_NOTIFY(&event_watches, station_event_watch_func_t, + STATION_EVENT_OWE_HIDDEN_FINISHED, network); + + station_autoconnect_start(station); + + return err == 0; +} + +static void station_owe_transition_triggered(int err, void *user_data) +{ + struct network *network = user_data; + struct station *station = network_get_station(network); + + if (err < 0) { + l_debug("OWE transition scan trigger failed: %i", err); + + l_queue_pop_head(station->owe_hidden_scan_ids); + + WATCHLIST_NOTIFY(&event_watches, station_event_watch_func_t, + STATION_EVENT_OWE_HIDDEN_FINISHED, network); + + return; + } + + l_debug("OWE transition scan triggered"); + + station_property_set_scanning(station, true); +} + +static void foreach_add_owe_scan(struct network *network, void *data) +{ + struct station *station = data; + const struct l_queue_entry *entry; + struct l_queue *list = NULL; + uint32_t id; + + if (network_get_security(network) != SECURITY_NONE) + return; + + for (entry = network_bss_list_get_entries(network); entry; + entry = entry->next) { + struct scan_bss *open = entry->data; + + if (!open->owe_trans) + continue; + + /* only want the open networks with WFA OWE IE */ + if (open->rsne) + continue; + + /* BSS already in network object */ + if (network_bss_find_by_addr(network, open->owe_trans->bssid)) + continue; + + if (!list) + list = l_queue_new(); + + l_queue_push_tail(list, open); + } + + if (!list) + return; + + id = scan_owe_hidden(netdev_get_wdev_id(station->netdev), list, + station_owe_transition_triggered, + station_owe_transition_results, network, NULL); + + l_queue_destroy(list, NULL); + + if (!id) + return; + + if (!station->owe_hidden_scan_ids) + station->owe_hidden_scan_ids = l_queue_new(); + + l_queue_push_tail(station->owe_hidden_scan_ids, L_UINT_TO_PTR(id)); + + WATCHLIST_NOTIFY(&event_watches, station_event_watch_func_t, + STATION_EVENT_OWE_HIDDEN_STARTED, network); +} + +static void station_process_owe_transition_networks(struct station *station) +{ + station_network_foreach(station, foreach_add_owe_scan, station); +} + static bool bss_free_if_ssid_not_utf8(void *data, void *user_data) { struct scan_bss *bss = data; @@ -650,11 +874,10 @@ static bool bss_free_if_ssid_not_utf8(void *data, void *user_data) void station_set_scan_results(struct station *station, struct l_queue *new_bss_list, const struct scan_freq_set *freqs, - bool add_to_autoconnect) + bool trigger_autoconnect) { const struct l_queue_entry *bss_entry; struct network *network; - bool wait_for_anqp = false; l_queue_foreach_remove(new_bss_list, bss_free_if_ssid_not_utf8, NULL); @@ -664,7 +887,7 @@ void station_set_scan_results(struct station *station, l_queue_clear(station->hidden_bss_list_sorted, NULL); l_queue_destroy(station->autoconnect_list, NULL); - station->autoconnect_list = l_queue_new(); + station->autoconnect_list = NULL; station_bss_list_remove_expired_bsses(station, freqs); @@ -701,18 +924,19 @@ void station_set_scan_results(struct station *station, if (!network) continue; - if (station_start_anqp(station, network, bss)) - wait_for_anqp = true; + /* Cached BSS entry, this should have been processed already */ + if (!scan_freq_set_contains(freqs, bss->frequency)) + continue; + + station_start_anqp(station, network, bss); } station->bss_list = new_bss_list; l_hashmap_foreach_remove(station->networks, process_network, station); - if (!wait_for_anqp && add_to_autoconnect) { - station_network_foreach(station, network_add_foreach, station); - station_autoconnect_next(station); - } + station->autoconnect_can_start = trigger_autoconnect; + station_autoconnect_start(station); } static void station_reconnect(struct station *station); @@ -772,14 +996,15 @@ static int station_build_handshake_rsn(struct handshake_state *hs, struct network *network, struct scan_bss *bss) { + const struct l_settings *settings = iwd_get_config(); enum security security = network_get_security(network); bool add_mde = false; struct erp_cache_entry *erp_cache = NULL; - struct ie_rsn_info bss_info; uint8_t rsne_buf[256]; struct ie_rsn_info info; uint8_t *ap_ie; + bool disable_ocv; memset(&info, 0, sizeof(info)); @@ -857,6 +1082,34 @@ static int station_build_handshake_rsn(struct handshake_state *hs, goto not_supported; build_ie: + if (!l_settings_get_bool(settings, "General", "DisableOCV", + &disable_ocv)) + disable_ocv = false; + + /* + * Obviously do not enable OCV if explicitly disabled or no AP support. + * + * Not obviously hostapd rejects OCV support if MFPC is not enabled. + * This is not really specified by the spec, but we have to work around + * this limitation. + * + * Another limitation is full mac cards. With limited testing it was + * seen that they do not include the OCI in the 4-way handshake yet + * still advertise the capability. Because of this OCV is disabled if + * any offload features are detected (since IWD prefers to use offload). + */ + info.ocvc = !disable_ocv && bss_info.ocvc && info.mfpc && + !wiphy_can_offload(wiphy);; + + /* + * IEEE 802.11-2020 9.4.2.24.4 states extended key IDs can only be used + * with CCMP/GCMP cipher suites. We also only enable support if the AP + * also indicates support. + */ + if (wiphy_supports_ext_key_id(wiphy) && bss_info.extended_key_id && + info.pairwise_ciphers == IE_RSN_CIPHER_SUITE_CCMP) + info.extended_key_id = true; + /* RSN takes priority */ if (bss->rsne) { ap_ie = bss->rsne; @@ -937,7 +1190,7 @@ static struct handshake_state *station_handshake_setup(struct station *station, handshake_state_set_authenticator_rsnxe(hs, bss->rsnxe); - if (network_handshake_setup(network, hs) < 0) + if (network_handshake_setup(network, bss, hs) < 0) goto not_supported; vendor_ies = network_info_get_extra_ies(info, bss, &iov_elems); @@ -965,15 +1218,15 @@ static bool new_scan_results(int err, struct l_queue *bss_list, void *userdata) { struct station *station = userdata; - bool autoconnect; station_property_set_scanning(station, false); if (err) return false; - autoconnect = station_is_autoconnecting(station); - station_set_scan_results(station, bss_list, freqs, autoconnect); + station_set_scan_results(station, bss_list, freqs, true); + + station_process_owe_transition_networks(station); return true; } @@ -1037,15 +1290,15 @@ static bool station_quick_scan_results(int err, struct l_queue *bss_list, void *userdata) { struct station *station = userdata; - bool autoconnect; station_property_set_scanning(station, false); if (err) goto done; - autoconnect = station_is_autoconnecting(station); - station_set_scan_results(station, bss_list, freqs, autoconnect); + station_set_scan_results(station, bss_list, freqs, true); + + station_process_owe_transition_networks(station); done: if (station->state == STATION_STATE_AUTOCONNECT_QUICK) @@ -1226,16 +1479,16 @@ 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, +uint32_t station_add_event_watch(station_event_watch_func_t func, void *user_data, station_destroy_func_t destroy) { - return watchlist_add(&anqp_watches, func, user_data, destroy); + return watchlist_add(&event_watches, func, user_data, destroy); } -void station_remove_anqp_watch(uint32_t id) +void station_remove_event_watch(uint32_t id) { - watchlist_remove(&anqp_watches, id); + watchlist_remove(&event_watches, id); } bool station_set_autoconnect(struct station *station, bool autoconnect) @@ -1497,6 +1750,9 @@ static void station_early_neighbor_report_cb(struct netdev *netdev, int err, { struct station *station = user_data; + if (err == -ENODEV) + return; + l_debug("ifindex: %u, error: %d(%s)", netdev_get_ifindex(station->netdev), err, err < 0 ? strerror(-err) : ""); @@ -1508,6 +1764,77 @@ static void station_early_neighbor_report_cb(struct netdev *netdev, int err, &station->roam_freqs); } +static bool station_can_fast_transition(struct handshake_state *hs, + struct scan_bss *bss) +{ + uint16_t mdid; + + if (!hs->mde) + return false; + + if (ie_parse_mobility_domain_from_data(hs->mde, hs->mde[1] + 2, + &mdid, NULL, NULL) < 0) + return false; + + if (!(bss->mde_present && l_get_le16(bss->mde) == mdid)) + return false; + + if (hs->supplicant_ie != NULL) { + struct ie_rsn_info rsn_info; + + if (!IE_AKM_IS_FT(hs->akm_suite)) + return false; + + if (scan_bss_get_rsn_info(bss, &rsn_info) < 0) + return false; + + if (!IE_AKM_IS_FT(rsn_info.akm_suites)) + return false; + } + + return true; +} + +static void station_ft_ds_action_start(struct station *station) +{ + struct handshake_state *hs = netdev_get_handshake(station->netdev); + uint16_t mdid; + const struct l_queue_entry *entry; + struct scan_bss *bss; + struct ie_rsn_info rsn_info; + + if (!station_can_fast_transition(hs, station->connected_bss) || + !(hs->mde[4] & 1)) + return; + + if (ie_parse_mobility_domain_from_data(hs->mde, hs->mde[1] + 2, + &mdid, NULL, NULL) < 0) + return; + + for (entry = network_bss_list_get_entries(station->connected_network); + entry; entry = entry->next) { + bss = entry->data; + + if (bss == station->connected_bss) + continue; + + if (mdid != l_get_le16(bss->mde)) + continue; + + if (scan_bss_get_rsn_info(bss, &rsn_info) < 0) + continue; + + if (!IE_AKM_IS_FT(rsn_info.akm_suites)) + continue; + + /* + * Fire and forget. Netdev will maintain a cache of responses and + * when the time comes these can be referenced for a roam + */ + netdev_fast_transition_over_ds_action(station->netdev, bss); + } +} + static void station_roamed(struct station *station) { station->roam_scan_full = false; @@ -1533,6 +1860,8 @@ static void station_roamed(struct station *station) l_warn("Could not request neighbor report"); } + station_ft_ds_action_start(station); + station_enter_state(station, STATION_STATE_CONNECTED); } @@ -1736,37 +2065,6 @@ static void station_preauthenticate_cb(struct netdev *netdev, station_transition_reassociate(station, bss, new_hs); } -static bool station_can_fast_transition(struct handshake_state *hs, - struct scan_bss *bss) -{ - uint16_t mdid; - - if (!hs->mde) - return false; - - if (ie_parse_mobility_domain_from_data(hs->mde, hs->mde[1] + 2, - &mdid, NULL, NULL) < 0) - return false; - - if (!(bss->mde_present && l_get_le16(bss->mde) == mdid)) - return false; - - if (hs->supplicant_ie != NULL) { - struct ie_rsn_info rsn_info; - - if (!IE_AKM_IS_FT(hs->akm_suite)) - return false; - - if (scan_bss_get_rsn_info(bss, &rsn_info) < 0) - return false; - - if (!IE_AKM_IS_FT(rsn_info.akm_suites)) - return false; - } - - return true; -} - static void station_transition_start(struct station *station, struct scan_bss *bss) { @@ -1801,8 +2099,7 @@ static void station_transition_start(struct station *station, vendor_ies = network_info_get_extra_ies(info, bss, &iov_elems); handshake_state_set_vendor_ies(hs, vendor_ies, iov_elems); - /* FT-over-DS can be better suited for these situations */ - if ((hs->mde[4] & 1) && station->signal_low) { + if ((hs->mde[4] & 1)) { ret = netdev_fast_transition_over_ds(station->netdev, bss, station->connected_bss, station_fast_transition_cb); @@ -2023,9 +2320,12 @@ static int station_roam_scan(struct station *station, l_debug("ifindex: %u", netdev_get_ifindex(station->netdev)); - if (station->connected_network) + if (station->connected_network) { + const char *ssid = network_get_ssid(station->connected_network); /* Use direct probe request */ - params.ssid = network_get_ssid(station->connected_network); + params.ssid = (const uint8_t *)ssid; + params.ssid_len = strlen(ssid); + } if (!freq_set) station->roam_scan_full = true; @@ -2066,6 +2366,9 @@ static void station_neighbor_report_cb(struct netdev *netdev, int err, struct scan_freq_set *freq_set; int r; + if (err == -ENODEV) + return; + l_debug("ifindex: %u, error: %d(%s)", netdev_get_ifindex(station->netdev), err, err < 0 ? strerror(-err) : ""); @@ -2398,36 +2701,6 @@ static bool station_retry_with_status(struct station *station, return station_try_next_bss(station); } -static void station_ft_ds_action_start(struct station *station, uint16_t mdid) -{ - const struct l_queue_entry *entry; - struct scan_bss *bss; - struct ie_rsn_info rsn_info; - - for (entry = network_bss_list_get_entries(station->connected_network); - entry; entry = entry->next) { - bss = entry->data; - - if (bss == station->connected_bss) - continue; - - if (mdid != l_get_le16(bss->mde)) - continue; - - if (scan_bss_get_rsn_info(bss, &rsn_info) < 0) - continue; - - if (!IE_AKM_IS_FT(rsn_info.akm_suites)) - continue; - - /* - * Fire and forget. Netdev will maintain a cache of responses and - * when the time comes these can be referenced for a roam - */ - netdev_fast_transition_over_ds_action(station->netdev, bss); - } -} - static void station_connect_ok(struct station *station) { struct handshake_state *hs = netdev_get_handshake(station->netdev); @@ -2451,20 +2724,7 @@ static void station_connect_ok(struct station *station) l_warn("Could not request neighbor report"); } - /* - * If this network supports FT-over-DS send initial action frames now - * to prepare for future roams. - */ - if (station_can_fast_transition(hs, station->connected_bss) && - (hs->mde[4] & 1)) { - uint16_t mdid; - - if (ie_parse_mobility_domain_from_data(hs->mde, hs->mde[1] + 2, - &mdid, NULL, NULL) < 0) - return; - - station_ft_ds_action_start(station, mdid); - } + station_ft_ds_action_start(station); network_connected(station->connected_network); @@ -2649,8 +2909,7 @@ int __station_connect_network(struct station *station, struct network *network, if (station->netconfig && !netconfig_load_settings( station->netconfig, - network_get_settings(network), - netdev_get_address(station->netdev))) + network_get_settings(network))) return -EINVAL; hs = station_handshake_setup(station, network, bss); @@ -2835,6 +3094,9 @@ static bool station_hidden_network_scan_results(int err, memcmp(bss->ssid, ssid, ssid_len)) goto next; + if (bss->owe_trans) + goto next; + /* * Override time_stamp so that this entry is removed on * the next scan @@ -2928,6 +3190,10 @@ static struct l_dbus_message *station_dbus_connect_hidden_network( l_queue_get_entries(station->hidden_bss_list_sorted); struct scan_bss *target = network_bss_select(network, true); + /* Treat OWE transition networks special */ + if (target->owe_trans) + goto not_hidden; + for (; entry; entry = entry->next) { struct scan_bss *bss = entry->data; @@ -2939,10 +3205,12 @@ static struct l_dbus_message *station_dbus_connect_hidden_network( message); } +not_hidden: return dbus_error_not_hidden(message); } - params.ssid = ssid; + params.ssid = (const uint8_t *)ssid; + params.ssid_len = strlen(ssid); /* HW cannot randomize our MAC if connected */ if (!station->connected_bss) @@ -3149,10 +3417,18 @@ static struct l_dbus_message *station_dbus_get_hidden_access_points( return reply; } -static void station_dbus_scan_done(struct station *station) +static void station_dbus_scan_done(struct station *station, + bool try_autoconnect) { station->dbus_scan_id = 0; station_property_set_scanning(station, false); + + station_process_owe_transition_networks(station); + + if (try_autoconnect) { + station->autoconnect_can_start = true; + station_autoconnect_start(station); + } } static void station_dbus_scan_triggered(int err, void *user_data) @@ -3169,7 +3445,7 @@ static void station_dbus_scan_triggered(int err, void *user_data) dbus_pending_reply(&station->scan_pending, reply); } - station_dbus_scan_done(station); + station_dbus_scan_done(station, true); return; } @@ -3194,23 +3470,21 @@ static bool station_dbus_scan_results(int err, struct l_queue *bss_list, { struct station *station = userdata; unsigned int next_idx = station->dbus_scan_subset_idx + 1; - bool autoconnect; bool last_subset; if (err) { - station_dbus_scan_done(station); + station_dbus_scan_done(station, true); return false; } - autoconnect = station_is_autoconnecting(station); - station_set_scan_results(station, bss_list, freqs, autoconnect); - last_subset = next_idx >= L_ARRAY_SIZE(station->scan_freqs_order) || station->scan_freqs_order[next_idx] == NULL; station->dbus_scan_subset_idx = next_idx; + station_set_scan_results(station, bss_list, freqs, false); + if (last_subset || !station_dbus_scan_subset(station)) - station_dbus_scan_done(station); + station_dbus_scan_done(station, true); return true; } @@ -3629,7 +3903,7 @@ static struct station *station_create(struct netdev *netdev) IWD_STATION_INTERFACE, station); #endif - if (netconfig_enabled) + if (netconfig_enabled()) station->netconfig = netconfig_new(netdev_get_ifindex(netdev)); station->anqp_pending = l_queue_new(); @@ -3707,6 +3981,16 @@ static void station_free(struct station *station) scan_cancel(netdev_get_wdev_id(station->netdev), station->hidden_network_scan_id); + if (station->owe_hidden_scan_ids) { + void *ptr; + + while ((ptr = l_queue_pop_head(station->owe_hidden_scan_ids))) + scan_cancel(netdev_get_wdev_id(station->netdev), + L_PTR_TO_UINT(ptr)); + + l_queue_destroy(station->owe_hidden_scan_ids, NULL); + } + station_roam_state_clear(station); l_queue_destroy(station->networks_sorted, NULL); @@ -3856,6 +4140,9 @@ static struct l_dbus_message *station_force_roam(struct l_dbus *dbus, if (mac_len != 6) return dbus_error_invalid_args(message); + if (!station->connected_network) + return dbus_error_not_connected(message); + target = network_bss_find_by_addr(station->connected_network, mac); if (!target || target == station->connected_bss) return dbus_error_invalid_args(message); @@ -3954,7 +4241,7 @@ static void station_debug_scan_triggered(int err, void *user_data) dbus_pending_reply(&station->scan_pending, reply); } - station_dbus_scan_done(station); + station_dbus_scan_done(station, false); return; } @@ -3975,17 +4262,14 @@ static bool station_debug_scan_results(int err, struct l_queue *bss_list, void *userdata) { struct station *station = userdata; - bool autoconnect; if (err) { - station_dbus_scan_done(station); + station_dbus_scan_done(station, false); return false; } - autoconnect = station_is_autoconnecting(station); - station_set_scan_results(station, bss_list, freqs, autoconnect); - - station_dbus_scan_done(station); + station_set_scan_results(station, bss_list, freqs, false); + station_dbus_scan_done(station, false); return true; } @@ -4208,15 +4492,10 @@ static int station_init(void) &anqp_disabled)) anqp_disabled = true; - if (!l_settings_get_bool(iwd_get_config(), "General", - "EnableNetworkConfiguration", - &netconfig_enabled)) - netconfig_enabled = false; - - if (!netconfig_enabled) + if (!netconfig_enabled()) l_info("station: Network configuration is disabled."); - watchlist_init(&anqp_watches, NULL); + watchlist_init(&event_watches, NULL); return 0; } @@ -4234,7 +4513,7 @@ static void station_exit(void) netdev_watch_remove(netdev_watch); l_queue_destroy(station_list, NULL); station_list = NULL; - watchlist_destroy(&anqp_watches); + watchlist_destroy(&event_watches); } IWD_MODULE(station, station_init, station_exit) diff --git a/src/station.h b/src/station.h @@ -45,14 +45,16 @@ enum station_state { STATION_STATE_ROAMING }; -enum station_anqp_state { - STATION_ANQP_STARTED, - STATION_ANQP_FINISHED, +enum station_event { + STATION_EVENT_ANQP_STARTED, + STATION_EVENT_ANQP_FINISHED, + STATION_EVENT_OWE_HIDDEN_STARTED, + STATION_EVENT_OWE_HIDDEN_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, +typedef void (*station_event_watch_func_t)(enum station_event, struct network *network, void *user_data); typedef void (*station_destroy_func_t)(void *userdata); @@ -77,10 +79,10 @@ 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, +uint32_t station_add_event_watch(station_event_watch_func_t func, void *user_data, station_destroy_func_t destroy); -void station_remove_anqp_watch(uint32_t id); +void station_remove_event_watch(uint32_t id); bool station_set_autoconnect(struct station *station, bool autoconnect); diff --git a/src/util.h b/src/util.h @@ -29,7 +29,7 @@ #define align_len(len, boundary) (((len)+(boundary)-1) & ~((boundary)-1)) #define MAC "%02x:%02x:%02x:%02x:%02x:%02x" -#define MAC_STR(a) a[0], a[1], a[2], a[3], a[4], a[5] +#define MAC_STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5] const char *util_ssid_to_utf8(size_t len, const uint8_t *ssid); bool util_ssid_is_utf8(size_t len, const uint8_t *ssid); diff --git a/src/wiphy.c b/src/wiphy.c @@ -67,6 +67,31 @@ static int mac_randomize_bytes = 6; static char regdom_country[2]; static uint32_t work_ids; +enum driver_flag { + DEFAULT_IF = 0x1, + FORCE_PAE = 0x2, +}; + +struct driver_info { + const char *prefix; + unsigned int flags; +}; + +/* + * The out-of-tree rtl88x2bu crashes the kernel hard if default interface is + * destroyed. It seems many other drivers are built from the same source code + * so we set the DEFAULT_IF flag for all of them. Unfortunately there are + * in-tree drivers that also match these names and may be fine. + */ +static const struct driver_info driver_infos[] = { + { "rtl81*", DEFAULT_IF }, + { "rtl87*", DEFAULT_IF }, + { "rtl88*", DEFAULT_IF }, + { "rtw_*", DEFAULT_IF }, + { "brcmfmac", DEFAULT_IF }, + { "bcmsdh_sdmmc", DEFAULT_IF }, +}; + struct wiphy { uint32_t id; char name[20]; @@ -84,6 +109,7 @@ struct wiphy { char *model_str; char *vendor_str; char *driver_str; + const struct driver_info *driver_info; struct watchlist state_watches; uint8_t extended_capabilities[EXT_CAP_LEN + 2]; /* max bitmap size + IE header */ uint8_t *iftype_extended_capabilities[NUM_NL80211_IFTYPES]; @@ -433,6 +459,21 @@ bool wiphy_can_transition_disable(struct wiphy *wiphy) return true; } +/* Catch all for the offload features */ +bool wiphy_can_offload(struct wiphy *wiphy) +{ + return wiphy_has_ext_feature(wiphy, + NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_PSK) || + wiphy_has_ext_feature(wiphy, + NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X) || + wiphy_has_ext_feature(wiphy, NL80211_EXT_FEATURE_SAE_OFFLOAD); +} + +bool wiphy_supports_ext_key_id(struct wiphy *wiphy) +{ + return wiphy_has_ext_feature(wiphy, NL80211_EXT_FEATURE_EXT_KEY_ID); +} + bool wiphy_supports_cmds_auth_assoc(struct wiphy *wiphy) { return wiphy->support_cmds_auth_assoc; @@ -512,6 +553,41 @@ const char *wiphy_get_name(struct wiphy *wiphy) return wiphy->name; } +bool wiphy_uses_default_if(struct wiphy *wiphy) +{ + if (!wiphy_get_driver(wiphy)) + return true; + + if (wiphy->driver_info && + wiphy->driver_info->flags & DEFAULT_IF) + return true; + + return false; +} + +bool wiphy_control_port_enabled(struct wiphy *wiphy) +{ + const struct l_settings *settings = iwd_get_config(); + bool enabled; + + if (wiphy->driver_info && + wiphy->driver_info->flags & FORCE_PAE) { + l_info("Not using Control Port due to driver quirks: %s", + wiphy_get_driver(wiphy)); + return false; + } + + if (!wiphy_has_ext_feature(wiphy, + NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211)) + return false; + + if (!l_settings_get_bool(settings, "General", + "ControlPortOverNL80211", &enabled)) + enabled = true; + + return enabled; +} + const uint8_t *wiphy_get_permanent_address(struct wiphy *wiphy) { return wiphy->permanent_addr; @@ -1340,6 +1416,7 @@ static bool wiphy_get_driver_name(struct wiphy *wiphy) L_AUTO_FREE_VAR(char *, driver_link) = NULL; char driver_path[256]; ssize_t len; + unsigned int i; driver_link = l_strdup_printf("/sys/class/ieee80211/%s/device/driver", wiphy->name); @@ -1352,6 +1429,11 @@ static bool wiphy_get_driver_name(struct wiphy *wiphy) driver_path[len] = '\0'; wiphy->driver_str = l_strdup(basename(driver_path)); + + for (i = 0; i < L_ARRAY_SIZE(driver_infos); i++) + if (!fnmatch(driver_infos[i].prefix, wiphy->driver_str, 0)) + wiphy->driver_info = &driver_infos[i]; + return true; } diff --git a/src/wiphy.h b/src/wiphy.h @@ -83,9 +83,11 @@ uint32_t wiphy_get_supported_bands(struct wiphy *wiphy); const struct scan_freq_set *wiphy_get_supported_freqs( const struct wiphy *wiphy); bool wiphy_can_transition_disable(struct wiphy *wiphy); +bool wiphy_can_offload(struct wiphy *wiphy); bool wiphy_supports_cmds_auth_assoc(struct wiphy *wiphy); bool wiphy_can_randomize_mac_addr(struct wiphy *wiphy); bool wiphy_rrm_capable(struct wiphy *wiphy); +bool wiphy_supports_ext_key_id(struct wiphy *wiphy); bool wiphy_has_feature(struct wiphy *wiphy, uint32_t feature); bool wiphy_has_ext_feature(struct wiphy *wiphy, uint32_t feature); uint8_t wiphy_get_max_num_ssids_per_scan(struct wiphy *wiphy); @@ -100,6 +102,8 @@ bool wiphy_supports_qos_set_map(struct wiphy *wiphy); bool wiphy_supports_firmware_roam(struct wiphy *wiphy); const char *wiphy_get_driver(struct wiphy *wiphy); const char *wiphy_get_name(struct wiphy *wiphy); +bool wiphy_uses_default_if(struct wiphy *wiphy); +bool wiphy_control_port_enabled(struct wiphy *wiphy); const uint8_t *wiphy_get_permanent_address(struct wiphy *wiphy); const uint8_t *wiphy_get_extended_capabilities(struct wiphy *wiphy, uint32_t iftype); diff --git a/test/force-roam b/test/force-roam @@ -3,14 +3,33 @@ import sys import dbus -if (len(sys.argv) != 3): - print("Usage: %s <device> <mac>" % (sys.argv[0])) +if len(sys.argv) < 2 or len(sys.argv) > 3: + print("Usage: %s [device] <mac>" % (sys.argv[0])) sys.exit(1) bus = dbus.SystemBus() -device = dbus.Interface(bus.get_object("net.connman.iwd", sys.argv[1]), +device = None + +if len(sys.argv) == 2: + manager = dbus.Interface(bus.get_object("net.connman.iwd", "/"), + "org.freedesktop.DBus.ObjectManager") + objects = manager.GetManagedObjects() + for path in objects: + for interface in objects[path]: + if interface == "net.connman.iwd.StationDebug": + device = dbus.Interface(bus.get_object("net.connman.iwd", path), "net.connman.iwd.StationDebug") + break + + if not device: + print("StationDebug interface not found") + exit() -mac = sys.argv[2].replace(':', '') + mac = sys.argv[1] +else: + device = dbus.Interface(bus.get_object("net.connman.iwd", sys.argv[1]), + "net.connman.iwd.StationDebug") + mac = sys.argv[2] +mac = mac.replace(':', '') device.Roam(dbus.ByteArray.fromhex(mac)) diff --git a/tools/test-runner b/tools/test-runner @@ -853,7 +853,11 @@ class TestContext(Namespace): def discover_radios(self): phys = [] - iw = pyroute2.iwutil.IW() + + try: + iw = pyroute2.iwutil.IW() + except: + iw = pyroute2.IW() attrs = [phy['attrs'] for phy in iw.list_wiphy()] @@ -927,12 +931,32 @@ class TestContext(Namespace): return frequencies def start_wpas_interfaces(self): + if 'WPA_SUPPLICANT' not in self.hw_config: return settings = self.hw_config['WPA_SUPPLICANT'] - wpas_radios = [rad for rad in self.radios if rad.name in settings] - self.wpas_interfaces = [rad.create_interface(settings[rad.name], 'wpas') for rad in wpas_radios] + + if self.args.hw: + nradios = len(settings.items()) + + wpas_radios = self.radios[:nradios] + self.wpas_interfaces = [] + + # + # Physical radios most likely will use a different name + # than 'rad#' but the config file is referenced by these + # 'rad#' names. Iterate through both the settings and + # physical radios to create interfaces associated with + # each config file. + # + for vrad, hwrad in zip(settings.items(), wpas_radios): + self.wpas_interfaces.append(hwrad.create_interface(vrad[1], 'wpas')) + + else: + wpas_radios = [rad for rad in self.radios if rad.name in settings] + self.wpas_interfaces = [rad.create_interface('wpa_supplicant.conf', 'wpas') \ + for rad in wpas_radios] def start_ofono(self): sim_keys = self.hw_config['SETUP'].get('sim_keys', None) @@ -1416,6 +1440,7 @@ def run_auto_tests(ctx, args): # a set of tests was passed in just start out in the first. # os.chdir(tests[0]) + os.environ['DBUS_SYSTEM_BUS_ADDRESS'] = ctx.dbus_address os.system('/bin/bash') exit() @@ -1628,6 +1653,7 @@ class Main: def start(self): usb_adapters = None + pci_adapters = None qemu_table = [ 'qemu-system-x86_64', @@ -1670,7 +1696,7 @@ class Main: if self.args.hw: hw_conf = ConfigParser() hw_conf.read(self.args.hw) - # TODO: Parse PCI adapters + if hw_conf.has_section('USBAdapters'): # The actual key name of the adapter # doesn't matter since all we need is the @@ -1678,6 +1704,9 @@ class Main: # anyways once in the VM. usb_adapters = [v for v in hw_conf['USBAdapters'].values()] + if hw_conf.has_section('PCIAdapters'): + pci_adapters = [v for v in hw_conf['PCIAdapters'].values()] + # # Additional arguments not provided to test-runner which are # needed once booted into the kernel. @@ -1777,7 +1806,7 @@ class Main: '-display', 'none', '-m', '%dM' % ram, '-nographic', '-vga', 'none', '-no-acpi', '-no-hpet', '-no-reboot', '-fsdev', - 'local,id=fsdev-root,path=/,readonly,security_model=none,multidevs=remap', + 'local,id=fsdev-root,path=/,readonly=on,security_model=none,multidevs=remap', '-device', 'virtio-9p-pci,fsdev=fsdev-root,mount_tag=/dev/root', '-chardev', 'stdio,id=chardev-serial0,signal=off', @@ -1807,6 +1836,11 @@ class Main: '-device', 'usb-host,hostbus=%s,hostaddr=%s' % \ (bus, addr)]) + if pci_adapters: + qemu_cmdline.extend(['-enable-kvm']) + for addr in pci_adapters: + qemu_cmdline.extend(['-device', 'vfio-pci,host=%s' % addr]) + if self.args.log: # # Creates a virtfs device that can be mounted. This mount diff --git a/unit/test-band.c b/unit/test-band.c @@ -282,6 +282,96 @@ static void band_test_vht_1(const void *data) band_free(band); } +struct oci2freq_data { + unsigned int op; + unsigned int chan; + int expected_freq; +}; + +static const struct oci2freq_data oci2freq_data_1 = { 129, 100, 5500 }; +static const struct oci2freq_data oci2freq_data_2 = { 129, 108, 5540 }; +static const struct oci2freq_data oci2freq_data_3 = { 129, 106, -EINVAL }; +static const struct oci2freq_data oci2freq_data_4 = { 81, 1, 2412 }; +static const struct oci2freq_data oci2freq_data_5 = { 82, 1, -EINVAL }; +static const struct oci2freq_data oci2freq_data_6 = { 82, 14, 2484 }; +static const struct oci2freq_data oci2freq_data_7 = { 88, 0, -ENOENT }; +static const struct oci2freq_data oci2freq_data_8 = { 128, 161, 5805 }; + +static void test_oci2freq(const void *data) +{ + const struct oci2freq_data *test = data; + int r; + + r = oci_to_frequency(test->op, test->chan); + assert(r == test->expected_freq); +} + +static const struct band_chandef cd_1 = { + .frequency = 5540, + .channel_width = BAND_CHANDEF_WIDTH_160, + .center1_frequency = 5570, +}; + +static const struct band_chandef cd_2 = { + .frequency = 5180, + .channel_width = BAND_CHANDEF_WIDTH_80P80, + .center1_frequency = 5210, + .center2_frequency = 5775, +}; + +static const struct band_chandef cd_3 = { + .frequency = 2437, + .channel_width = BAND_CHANDEF_WIDTH_20NOHT, + .center1_frequency = 2437, +}; + +static const struct band_chandef cd_4 = { + .frequency = 2437, + .channel_width = BAND_CHANDEF_WIDTH_40, + .center1_frequency = 2427, +}; + +struct oci_data { + const struct band_chandef *cd; + uint8_t oci[3]; + int expected_verify_error; +}; + +static const struct oci_data oci_data_1 = { &cd_1, { 129, 108, 0 } }; +static const struct oci_data oci_data_2 = { &cd_2, { 130, 36, 155 } }; +static const struct oci_data oci_data_3 = { &cd_3, { 81, 6, 0 } }; +static const struct oci_data oci_data_4 = { &cd_4, { 84, 6, 0 } }; + +static const struct oci_data oci_err_1 = { &cd_1, { 129, 36, 0 }, -EPERM }; +static const struct oci_data oci_err_2 = { &cd_1, { 121, 108, 0 }, -EPERM }; +static const struct oci_data oci_err_3 = { &cd_1, { 130, 36, 155 }, -EPERM }; +static const struct oci_data oci_err_4 = { &cd_3, { 81, 5 }, -EPERM }; +static const struct oci_data oci_err_5 = { &cd_3, { 80, 1 }, -ENOENT }; +static const struct oci_data oci_err_6 = { &cd_3, { 81, 15 }, -EINVAL }; +static const struct oci_data oci_err_7 = { &cd_4, { 84, 5 }, -EPERM }; +static const struct oci_data oci_err_8 = { &cd_4, { 83, 6 }, -EPERM }; + +static void test_oci_verify(const void *data) +{ + const struct oci_data *test = data; + int r; + + r = oci_verify(test->oci, test->cd); + assert(r == test->expected_verify_error); +} + +static void test_oci_from_chandef(const void *data) +{ + const struct oci_data *test = data; + uint8_t oci[3]; + int r; + + r = oci_from_chandef(test->cd, oci); + assert(!r); + + assert(!memcmp(oci, test->oci, sizeof(oci))); +} + int main(int argc, char *argv[]) { l_test_init(&argc, &argv); @@ -292,5 +382,33 @@ int main(int argc, char *argv[]) l_test_add("/band/VHT/test1", band_test_vht_1, NULL); + l_test_add("/band/oci2freq 1", test_oci2freq, &oci2freq_data_1); + l_test_add("/band/oci2freq 2", test_oci2freq, &oci2freq_data_2); + l_test_add("/band/oci2freq 3", test_oci2freq, &oci2freq_data_3); + l_test_add("/band/oci2freq 4", test_oci2freq, &oci2freq_data_4); + l_test_add("/band/oci2freq 5", test_oci2freq, &oci2freq_data_5); + l_test_add("/band/oci2freq 6", test_oci2freq, &oci2freq_data_6); + l_test_add("/band/oci2freq 7", test_oci2freq, &oci2freq_data_7); + l_test_add("/band/oci2freq 8", test_oci2freq, &oci2freq_data_8); + + l_test_add("/band/oci/verify 1", test_oci_verify, &oci_data_1); + l_test_add("/band/oci/verify 2", test_oci_verify, &oci_data_2); + l_test_add("/band/oci/verify 3", test_oci_verify, &oci_data_3); + l_test_add("/band/oci/verify 4", test_oci_verify, &oci_data_4); + + l_test_add("/band/oci/noverify 1", test_oci_verify, &oci_err_1); + l_test_add("/band/oci/noverify 2", test_oci_verify, &oci_err_2); + l_test_add("/band/oci/noverify 3", test_oci_verify, &oci_err_3); + l_test_add("/band/oci/noverify 4", test_oci_verify, &oci_err_4); + l_test_add("/band/oci/noverify 5", test_oci_verify, &oci_err_5); + l_test_add("/band/oci/noverify 6", test_oci_verify, &oci_err_6); + l_test_add("/band/oci/noverify 7", test_oci_verify, &oci_err_7); + l_test_add("/band/oci/noverify 8", test_oci_verify, &oci_err_8); + + l_test_add("/band/oci/chandef 1", test_oci_from_chandef, &oci_data_1); + l_test_add("/band/oci/chandef 2", test_oci_from_chandef, &oci_data_2); + l_test_add("/band/oci/chandef 3", test_oci_from_chandef, &oci_data_3); + l_test_add("/band/oci/chandef 4", test_oci_from_chandef, &oci_data_4); + return l_test_run(); } diff --git a/unit/test-eapol.c b/unit/test-eapol.c @@ -1940,6 +1940,7 @@ static void eapol_wpa2_handshake_test(const void *data) frame = eapol_create_gtk_2_of_2(EAPOL_PROTOCOL_VERSION_2004, EAPOL_KEY_DESCRIPTOR_VERSION_HMAC_SHA1_AES, eapol_key_test_12.key_replay_counter, + 0, NULL, false, 0, 16); assert(frame); assert(eapol_calculate_mic(IE_RSN_AKM_SUITE_PSK, ptk, frame, @@ -2061,8 +2062,8 @@ static void eapol_wpa_handshake_test(const void *data) frame = eapol_create_gtk_2_of_2(EAPOL_PROTOCOL_VERSION_2004, EAPOL_KEY_DESCRIPTOR_VERSION_HMAC_MD5_ARC4, - eapol_key_test_18.key_replay_counter, true, - gtk_step1->wpa_key_id, 16); + eapol_key_test_18.key_replay_counter, 0, NULL, + true, gtk_step1->wpa_key_id, 16); assert(frame); assert(eapol_calculate_mic(IE_RSN_AKM_SUITE_PSK, ptk, frame, mic, 16)); @@ -2470,7 +2471,7 @@ static void eapol_sm_test_wpa_ptk_gtk_2(const void *data) eapol_exit(); } -static void verify_install_tk(struct handshake_state *hs, +static void verify_install_tk(struct handshake_state *hs, uint8_t key_index, const uint8_t *tk, uint32_t cipher) { struct test_handshake_state *ths = @@ -3625,7 +3626,7 @@ static void test_ap_sta_hs_event(struct handshake_state *hs, assert(event != HANDSHAKE_EVENT_FAILED); } -static void test_ap_sta_install_tk(struct handshake_state *hs, +static void test_ap_sta_install_tk(struct handshake_state *hs, uint8_t key_idx, const uint8_t *tk, uint32_t cipher) { struct test_ap_sta_hs *ths = @@ -3869,9 +3870,9 @@ static void eapol_ap_sta_handshake_ip_alloc_no_req_test(const void *data) handshake_state_set_ssid(s.ap_hs, (void *) ssid, strlen(ssid)); handshake_state_set_pmk(s.ap_hs, psk, 32); s.ap_hs->support_ip_allocation = true; - s.ap_hs->client_ip_addr = 0x01020304; - s.ap_hs->subnet_mask = 0xffff0000; - s.ap_hs->go_ip_addr = 0x01020305; + s.ap_hs->client_ip_addr = L_CPU_TO_BE32(0x01020304); + s.ap_hs->subnet_mask = L_CPU_TO_BE32(0xffff0000); + s.ap_hs->go_ip_addr = L_CPU_TO_BE32(0x01020305); handshake_state_set_authenticator(s.sta_hs, false); handshake_state_set_event_func(s.sta_hs, test_ap_sta_hs_event, &s); diff --git a/unit/test-wsc.c b/unit/test-wsc.c @@ -2345,7 +2345,7 @@ static bool random_nonce(uint8_t nonce[]) return l_getrandom(nonce, 32); } -static void test_ap_sta_install_tk(struct handshake_state *hs, +static void test_ap_sta_install_tk(struct handshake_state *hs, uint8_t key_idx, const uint8_t *tk, uint32_t cipher) { assert(false);