From 606d5594fc6f1a8969642da75eff49b28d8024e1 Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Mon, 16 Nov 2026 16:52:44 -0800 Subject: [PATCH 1/4] raspberrypi: retry a WiFi join with WPA3 SAE connect() always joined with CYW43_AUTH_WPA2_AES_PSK, which never authenticates with a WPA3-only network, so connect() timed out with "Unknown failure 1". The cyw43 driver cannot tell WPA3 from WPA2 in a scan, and in station mode CYW43_AUTH_WPA3_WPA2_AES_PSK joins with SAE only, which would break WPA2-only networks. So keep joining with WPA2 first. If the AP has still not accepted authentication at half the timeout (and at least 4 s), retry once with CYW43_AUTH_WPA3_SAE_AES_PSK and a fresh timeout. A WPA2 AP accepts authentication about 3 s into the join, even with a wrong password, so WPA2 joins never retry. The driver clears the AUTH bit when the join completes or fails, so it is latched, and the retry only happens while the link is still joining. Fixes #10385. Co-Authored-By: Claude Opus 5.5 --- ports/raspberrypi/common-hal/wifi/Radio.c | 24 +++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/ports/raspberrypi/common-hal/wifi/Radio.c b/ports/raspberrypi/common-hal/wifi/Radio.c index 4d1dd0624cd..afde6120bbc 100644 --- a/ports/raspberrypi/common-hal/wifi/Radio.c +++ b/ports/raspberrypi/common-hal/wifi/Radio.c @@ -33,6 +33,9 @@ #include "shared/netutils/dhcpserver.h" +// WIFI_JOIN_STATE_AUTH in cyw43_ctrl.c, which does not export it. +#define CYW43_JOIN_STATE_AUTH (0x0200) + #define MAC_ADDRESS_LENGTH 6 #define NETIF_STA (&cyw43_state.netif[CYW43_ITF_STA]) @@ -366,6 +369,12 @@ wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t cyw43_arch_wifi_connect_async((const char *)ssid, (const char *)password, auth_mode); // TODO: Implement authmode check like in espressif + // Join with WPA2. If the AP has not accepted authentication by half the + // timeout (at least 4 s), retry once with WPA3 SAE and a fresh timeout. + // The driver clears the AUTH bit once the join completes, so remember it. + uint64_t sae_fallback_at = start + MAX(timeout_ms / 2, 4000); + bool authenticated = false; + while (port_get_raw_ticks(NULL) < deadline) { RUN_BACKGROUND_TASKS; if (mp_hal_is_interrupted()) { @@ -373,6 +382,9 @@ wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t } int result = cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_STA); + if (cyw43_state.wifi_join_state & CYW43_JOIN_STATE_AUTH) { + authenticated = true; + } switch (result) { case CYW43_LINK_UP: @@ -385,6 +397,14 @@ wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t case CYW43_LINK_BADAUTH: return WIFI_RADIO_ERROR_AUTH_FAIL; } + + if (result == CYW43_LINK_JOIN && auth_mode == CYW43_AUTH_WPA2_AES_PSK && + !authenticated && port_get_raw_ticks(NULL) >= sae_fallback_at) { + common_hal_wifi_radio_stop_station(self); + auth_mode = CYW43_AUTH_WPA3_SAE_AES_PSK; + cyw43_arch_wifi_connect_async((const char *)ssid, (const char *)password, auth_mode); + deadline = port_get_raw_ticks(NULL) + timeout_ms; + } } // Being here means we either timed out or got interrupted. @@ -417,8 +437,8 @@ mp_obj_t common_hal_wifi_radio_get_ap_info(wifi_radio_obj_t *self) { cyw43_wifi_get_bssid(&cyw43_state, record.bssid); - // connect() joins with either CYW43_AUTH_OPEN or CYW43_AUTH_WPA2_AES_PSK. - // Encode that with the scan result bits from cyw43_ll.c: 1 is privacy, 4 is WPA2. + // connect() joins open, WPA2 or WPA3 SAE. Encode that with the scan result + // bits from cyw43_ll.c, which reports WPA2 and WPA3 alike: 1 is privacy, 4 is WPA2. record.auth_mode = self->connected_with_password ? (1 | 4) : 0; wifi_network_obj_t *ap_info = mp_obj_malloc(wifi_network_obj_t, &wifi_network_type); From 25575eeebf61f8bb234bef1fc78a6386b2984bcb Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Tue, 17 Nov 2026 06:23:02 -0800 Subject: [PATCH 2/4] raspberrypi: pick WPA3 from a scan before joining Update cyw43-driver, whose scan results now report WPA3 and the other security types from the RSN and WPA IEs. Before joining, scan for the SSID and join with WPA3 SAE if the network advertises WPA3, otherwise WPA2. This replaces the timed WPA2 to WPA3 retry, which never ran on a WPA3-only network because the WPA2 join failed straight away. Network.authmode now shows WPA3 and Enterprise, and ap_info.authmode reports the security actually used for the join. Co-Authored-By: Claude Opus 5.5 --- ports/raspberrypi/Makefile | 2 +- ports/raspberrypi/common-hal/wifi/Network.c | 23 ++++--- ports/raspberrypi/common-hal/wifi/Radio.c | 66 +++++++++++++-------- ports/raspberrypi/common-hal/wifi/Radio.h | 2 +- ports/raspberrypi/lib/cyw43-driver | 2 +- 5 files changed, 57 insertions(+), 38 deletions(-) diff --git a/ports/raspberrypi/Makefile b/ports/raspberrypi/Makefile index bfacd3a6c9c..a01fd43a457 100755 --- a/ports/raspberrypi/Makefile +++ b/ports/raspberrypi/Makefile @@ -587,7 +587,7 @@ SRC_SDK := \ $(SRC_SDK_CHIP_VARIANT) \ SRC_SDK := $(addprefix sdk/, $(SRC_SDK)) -$(patsubst %.c,$(BUILD)/%.o,$(SRC_SDK) $(SRC_CYW43)): CFLAGS += -Wno-missing-prototypes -Wno-undef -Wno-unused-function -Wno-nested-externs -Wno-strict-prototypes -Wno-double-promotion -Wno-sign-compare -Wno-unused-variable -Wno-strict-overflow -Ilib/cyw43-driver +$(patsubst %.c,$(BUILD)/%.o,$(SRC_SDK) $(SRC_CYW43)): CFLAGS += -Wno-missing-prototypes -Wno-undef -Wno-unused-function -Wno-nested-externs -Wno-strict-prototypes -Wno-double-promotion -Wno-sign-compare -Wno-discarded-qualifiers -Wno-unused-variable -Wno-strict-overflow -Ilib/cyw43-driver SRC_C += \ boards/$(BOARD)/board.c \ diff --git a/ports/raspberrypi/common-hal/wifi/Network.c b/ports/raspberrypi/common-hal/wifi/Network.c index a323276003c..0e94cb82fbb 100644 --- a/ports/raspberrypi/common-hal/wifi/Network.c +++ b/ports/raspberrypi/common-hal/wifi/Network.c @@ -35,22 +35,27 @@ mp_obj_t common_hal_wifi_network_get_country(wifi_network_obj_t *self) { } mp_obj_t common_hal_wifi_network_get_authmode(wifi_network_obj_t *self) { + uint32_t auth = self->record.auth_mode; uint32_t authmode_mask = 0; - if (self->record.auth_mode == 0) { + if (auth == 0) { authmode_mask = AUTHMODE_OPEN; } - if (self->record.auth_mode & 1) { - authmode_mask |= AUTHMODE_PSK; - } - ; - if (self->record.auth_mode & 2) { + if (auth & CYW43_AUTH_FLAG_WPA_SECURITY) { authmode_mask |= AUTHMODE_WPA; } - ; - if (self->record.auth_mode & 4) { + if (auth & CYW43_AUTH_FLAG_WPA2_SECURITY) { authmode_mask |= AUTHMODE_WPA2; } - ; + if (auth & CYW43_AUTH_FLAG_WPA3_SECURITY) { + authmode_mask |= AUTHMODE_WPA3; + } + if (auth & CYW43_AUTH_FLAG_ENTERPRISE_ENABLED) { + authmode_mask |= AUTHMODE_ENTERPRISE; + } else if (authmode_mask & (AUTHMODE_WPA | AUTHMODE_WPA2 | AUTHMODE_WPA3)) { + authmode_mask |= AUTHMODE_PSK; + } else if (auth & CYW43_AUTH_FLAG_WEP_ENABLED) { + authmode_mask |= AUTHMODE_WEP; + } mp_obj_t authmode_list = mp_obj_new_list(0, NULL); if (authmode_mask != 0) { for (uint32_t i = 0; i < 32; i++) { diff --git a/ports/raspberrypi/common-hal/wifi/Radio.c b/ports/raspberrypi/common-hal/wifi/Radio.c index afde6120bbc..061eaf12b9b 100644 --- a/ports/raspberrypi/common-hal/wifi/Radio.c +++ b/ports/raspberrypi/common-hal/wifi/Radio.c @@ -33,9 +33,6 @@ #include "shared/netutils/dhcpserver.h" -// WIFI_JOIN_STATE_AUTH in cyw43_ctrl.c, which does not export it. -#define CYW43_JOIN_STATE_AUTH (0x0200) - #define MAC_ADDRESS_LENGTH 6 #define NETIF_STA (&cyw43_state.netif[CYW43_ITF_STA]) @@ -341,6 +338,34 @@ static bool connection_unchanged(wifi_radio_obj_t *self, const uint8_t *ssid, si return true; } +// Static, not on the stack: results can arrive after scan_for_auth() gives up. +static uint32_t scan_auth; + +static int scan_for_auth_cb(void *env, const cyw43_ev_scan_result_t *result) { + scan_auth |= result->auth_mode; + return 0; +} + +// Scan for one SSID. Returns its scan result auth flags, or 0 if not seen. +static uint32_t scan_for_auth(wifi_radio_obj_t *self, const uint8_t *ssid, size_t ssid_len, uint64_t deadline) { + // A user scan owns the driver's scan callback. + if (self->current_scan != NULL) { + return 0; + } + cyw43_wifi_scan_options_t opts = {0}; + opts.ssid_len = ssid_len; + memcpy(opts.ssid, ssid, ssid_len); + scan_auth = 0; + if (cyw43_wifi_scan(&cyw43_state, &opts, NULL, scan_for_auth_cb) != 0) { + return 0; + } + while (cyw43_wifi_scan_active(&cyw43_state) && port_get_raw_ticks(NULL) < deadline && + !mp_hal_is_interrupted()) { + RUN_BACKGROUND_TASKS; + } + return scan_auth; +} + wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t *ssid, size_t ssid_len, uint8_t *password, size_t password_len, uint8_t channel, mp_float_t timeout, uint8_t *bssid, size_t bssid_len) { if (!common_hal_wifi_radio_get_enabled(self)) { mp_raise_RuntimeError(MP_ERROR_TEXT("WiFi is not enabled")); @@ -361,19 +386,20 @@ wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t // disconnect common_hal_wifi_radio_stop_station(self); + // Joining with WPA3 SAE fails on a WPA2-only network, so scan for the + // network first and use WPA3 only if it advertises it. + uint32_t auth_mode = CYW43_AUTH_OPEN; + if (password_len) { + uint32_t ap_auth = scan_for_auth(self, ssid, ssid_len, deadline); + auth_mode = (ap_auth & CYW43_AUTH_FLAG_WPA3_SECURITY) ? + CYW43_AUTH_WPA3_SAE_AES_PSK : CYW43_AUTH_WPA2_AES_PSK; + } + // connect. Record the network now: the join carries on after a timeout. memcpy(self->connected_ssid, ssid, ssid_len); self->connected_ssid_len = ssid_len; - self->connected_with_password = password_len != 0; - int auth_mode = password_len ? CYW43_AUTH_WPA2_AES_PSK : CYW43_AUTH_OPEN; + self->connected_auth_mode = auth_mode; cyw43_arch_wifi_connect_async((const char *)ssid, (const char *)password, auth_mode); - // TODO: Implement authmode check like in espressif - - // Join with WPA2. If the AP has not accepted authentication by half the - // timeout (at least 4 s), retry once with WPA3 SAE and a fresh timeout. - // The driver clears the AUTH bit once the join completes, so remember it. - uint64_t sae_fallback_at = start + MAX(timeout_ms / 2, 4000); - bool authenticated = false; while (port_get_raw_ticks(NULL) < deadline) { RUN_BACKGROUND_TASKS; @@ -382,9 +408,6 @@ wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t } int result = cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_STA); - if (cyw43_state.wifi_join_state & CYW43_JOIN_STATE_AUTH) { - authenticated = true; - } switch (result) { case CYW43_LINK_UP: @@ -397,14 +420,6 @@ wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t case CYW43_LINK_BADAUTH: return WIFI_RADIO_ERROR_AUTH_FAIL; } - - if (result == CYW43_LINK_JOIN && auth_mode == CYW43_AUTH_WPA2_AES_PSK && - !authenticated && port_get_raw_ticks(NULL) >= sae_fallback_at) { - common_hal_wifi_radio_stop_station(self); - auth_mode = CYW43_AUTH_WPA3_SAE_AES_PSK; - cyw43_arch_wifi_connect_async((const char *)ssid, (const char *)password, auth_mode); - deadline = port_get_raw_ticks(NULL) + timeout_ms; - } } // Being here means we either timed out or got interrupted. @@ -437,9 +452,8 @@ mp_obj_t common_hal_wifi_radio_get_ap_info(wifi_radio_obj_t *self) { cyw43_wifi_get_bssid(&cyw43_state, record.bssid); - // connect() joins open, WPA2 or WPA3 SAE. Encode that with the scan result - // bits from cyw43_ll.c, which reports WPA2 and WPA3 alike: 1 is privacy, 4 is WPA2. - record.auth_mode = self->connected_with_password ? (1 | 4) : 0; + // CYW43_AUTH_* values use the same flags as a scan result's auth_mode. + record.auth_mode = self->connected_auth_mode; wifi_network_obj_t *ap_info = mp_obj_malloc(wifi_network_obj_t, &wifi_network_type); ap_info->record = record; diff --git a/ports/raspberrypi/common-hal/wifi/Radio.h b/ports/raspberrypi/common-hal/wifi/Radio.h index 6dbaa9ea174..1b63c5a68af 100644 --- a/ports/raspberrypi/common-hal/wifi/Radio.h +++ b/ports/raspberrypi/common-hal/wifi/Radio.h @@ -17,7 +17,7 @@ typedef struct { wifi_scannednetworks_obj_t *current_scan; uint8_t connected_ssid[32]; uint8_t connected_ssid_len; - bool connected_with_password; + uint32_t connected_auth_mode; // CYW43_AUTH_* passed to the last join bool enabled; } wifi_radio_obj_t; diff --git a/ports/raspberrypi/lib/cyw43-driver b/ports/raspberrypi/lib/cyw43-driver index dd7568229f3..774b8f962aa 160000 --- a/ports/raspberrypi/lib/cyw43-driver +++ b/ports/raspberrypi/lib/cyw43-driver @@ -1 +1 @@ -Subproject commit dd7568229f3bf7a37737b9e1ef250c26efe75b23 +Subproject commit 774b8f962aae9ad85359c56e8814f8656e54b945 From 662b0e86f28a88bdee296658c46d8d90e0b7988f Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Fri, 20 Nov 2026 14:05:36 -0800 Subject: [PATCH 3/4] raspberrypi: use cyw43-driver circuitpython11 branch Pins the adafruit fork's circuitpython11 branch, which carries georgerobotics/cyw43-driver#179 (WPA IE scan parsing fix). Co-Authored-By: Claude Opus 5.5 --- .gitmodules | 2 +- ports/raspberrypi/lib/cyw43-driver | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitmodules b/.gitmodules index 9972696b825..9167ec80fa5 100644 --- a/.gitmodules +++ b/.gitmodules @@ -304,7 +304,7 @@ [submodule "ports/raspberrypi/lib/cyw43-driver"] path = ports/raspberrypi/lib/cyw43-driver url = https://github.com/adafruit/cyw43-driver - branch = main + branch = circuitpython11 [submodule "ports/raspberrypi/lib/lwip"] path = ports/raspberrypi/lib/lwip url = https://github.com/adafruit/lwip.git diff --git a/ports/raspberrypi/lib/cyw43-driver b/ports/raspberrypi/lib/cyw43-driver index 774b8f962aa..a117ced15c1 160000 --- a/ports/raspberrypi/lib/cyw43-driver +++ b/ports/raspberrypi/lib/cyw43-driver @@ -1 +1 @@ -Subproject commit 774b8f962aae9ad85359c56e8814f8656e54b945 +Subproject commit a117ced15c1cc56280e3a6592740494c28769179 From a7c01376275a4ffb1e40c711c4f8eb601893f953 Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Fri, 20 Nov 2026 20:06:45 -0800 Subject: [PATCH 4/4] raspberrypi: match SSID in auth scan, quiet cyw43 warnings Only count scan results for the SSID being joined when picking WPA2 or WPA3. Scope -Wno-discarded-qualifiers to cyw43_ll.c, the one file that needs it. Silence CYW43_WARN so the driver does not print to the console for every network offering an unknown cipher. Co-Authored-By: Claude Opus 5.5 --- ports/raspberrypi/Makefile | 4 +++- ports/raspberrypi/common-hal/wifi/Radio.c | 9 ++++++++- ports/raspberrypi/cyw43_configport.h | 4 ++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/ports/raspberrypi/Makefile b/ports/raspberrypi/Makefile index a01fd43a457..28d1872f023 100755 --- a/ports/raspberrypi/Makefile +++ b/ports/raspberrypi/Makefile @@ -587,7 +587,9 @@ SRC_SDK := \ $(SRC_SDK_CHIP_VARIANT) \ SRC_SDK := $(addprefix sdk/, $(SRC_SDK)) -$(patsubst %.c,$(BUILD)/%.o,$(SRC_SDK) $(SRC_CYW43)): CFLAGS += -Wno-missing-prototypes -Wno-undef -Wno-unused-function -Wno-nested-externs -Wno-strict-prototypes -Wno-double-promotion -Wno-sign-compare -Wno-discarded-qualifiers -Wno-unused-variable -Wno-strict-overflow -Ilib/cyw43-driver +$(patsubst %.c,$(BUILD)/%.o,$(SRC_SDK) $(SRC_CYW43)): CFLAGS += -Wno-missing-prototypes -Wno-undef -Wno-unused-function -Wno-nested-externs -Wno-strict-prototypes -Wno-double-promotion -Wno-sign-compare -Wno-unused-variable -Wno-strict-overflow -Ilib/cyw43-driver +# cyw43_ll.c assigns string literals to a non-const pointer in its scan IE parser. +$(BUILD)/lib/cyw43-driver/src/cyw43_ll.o: CFLAGS += -Wno-discarded-qualifiers SRC_C += \ boards/$(BOARD)/board.c \ diff --git a/ports/raspberrypi/common-hal/wifi/Radio.c b/ports/raspberrypi/common-hal/wifi/Radio.c index 061eaf12b9b..0cf2e4743e3 100644 --- a/ports/raspberrypi/common-hal/wifi/Radio.c +++ b/ports/raspberrypi/common-hal/wifi/Radio.c @@ -340,9 +340,14 @@ static bool connection_unchanged(wifi_radio_obj_t *self, const uint8_t *ssid, si // Static, not on the stack: results can arrive after scan_for_auth() gives up. static uint32_t scan_auth; +static uint8_t scan_ssid[32]; +static size_t scan_ssid_len; static int scan_for_auth_cb(void *env, const cyw43_ev_scan_result_t *result) { - scan_auth |= result->auth_mode; + // Only count results for the SSID being joined. + if (result->ssid_len == scan_ssid_len && memcmp(result->ssid, scan_ssid, scan_ssid_len) == 0) { + scan_auth |= result->auth_mode; + } return 0; } @@ -355,6 +360,8 @@ static uint32_t scan_for_auth(wifi_radio_obj_t *self, const uint8_t *ssid, size_ cyw43_wifi_scan_options_t opts = {0}; opts.ssid_len = ssid_len; memcpy(opts.ssid, ssid, ssid_len); + memcpy(scan_ssid, ssid, ssid_len); + scan_ssid_len = ssid_len; scan_auth = 0; if (cyw43_wifi_scan(&cyw43_state, &opts, NULL, scan_for_auth_cb) != 0) { return 0; diff --git a/ports/raspberrypi/cyw43_configport.h b/ports/raspberrypi/cyw43_configport.h index c1769436ae4..5ff70e14d13 100644 --- a/ports/raspberrypi/cyw43_configport.h +++ b/ports/raspberrypi/cyw43_configport.h @@ -16,6 +16,10 @@ #define CYW43_NETUTILS (1) +// Keep driver warnings off the serial console. Every scan warns once per network +// that offers a cipher the driver does not know, such as GCMP on WPA3 routers. +#define CYW43_WARN(...) ((void)0) + #if CIRCUITPY_USB_DEVICE #include "supervisor/usb.h" #define CYW43_EVENT_POLL_HOOK usb_background();