From dae98ddff7bf603df37d297b9771f1ec3e584be6 Mon Sep 17 00:00:00 2001 From: "kapil.gupta" Date: Tue, 17 Mar 2020 18:54:30 +0530 Subject: [PATCH 01/16] wpa_supplicant: Replace internal RSA APIs by mbedtls APIs Curretly wpa_supplicant uses internal APIs for RSA operations which internally uses lots of big num operations. Big num operations are CPU expensive and can take a lot of time which can cause watchdog timer to tigger. This can be optimize by using mbedtls APIs which uses hardware blocks for big num operations. To fix this, write new crypto_mbedtls-rsa.c which has APIs similar to crypto_internal-rsa.c but uses mbedtls APIs. --- components/wpa_supplicant/CMakeLists.txt | 1 + .../src/crypto/crypto_internal-rsa.c | 3 +- .../src/crypto/crypto_mbedtls-rsa.c | 356 ++++++++++++++++++ 3 files changed, 359 insertions(+), 1 deletion(-) create mode 100644 components/wpa_supplicant/src/crypto/crypto_mbedtls-rsa.c diff --git a/components/wpa_supplicant/CMakeLists.txt b/components/wpa_supplicant/CMakeLists.txt index b1a604ef1b..0772a36eaa 100644 --- a/components/wpa_supplicant/CMakeLists.txt +++ b/components/wpa_supplicant/CMakeLists.txt @@ -21,6 +21,7 @@ set(srcs "port/os_xtensa.c" "src/crypto/crypto_internal-cipher.c" "src/crypto/crypto_internal-modexp.c" "src/crypto/crypto_internal-rsa.c" + "src/crypto/crypto_mbedtls-rsa.c" "src/crypto/crypto_internal.c" "src/crypto/des-internal.c" "src/crypto/dh_group5.c" diff --git a/components/wpa_supplicant/src/crypto/crypto_internal-rsa.c b/components/wpa_supplicant/src/crypto/crypto_internal-rsa.c index 1d63137fa5..ec27b6d977 100644 --- a/components/wpa_supplicant/src/crypto/crypto_internal-rsa.c +++ b/components/wpa_supplicant/src/crypto/crypto_internal-rsa.c @@ -17,6 +17,7 @@ #include "tls/pkcs1.h" #include "tls/pkcs8.h" +#ifndef USE_MBEDTLS_CRYPTO /* Dummy structures; these are just typecast to struct crypto_rsa_key */ struct crypto_public_key; struct crypto_private_key; @@ -28,7 +29,6 @@ struct crypto_public_key * crypto_public_key_import(const u8 *key, size_t len) crypto_rsa_import_public_key(key, len); } - struct crypto_private_key * crypto_private_key_import(const u8 *key, size_t len, const char *passwd) @@ -109,3 +109,4 @@ int crypto_public_key_decrypt_pkcs1(struct crypto_public_key *key, return pkcs1_decrypt_public_key((struct crypto_rsa_key *) key, crypt, crypt_len, plain, plain_len); } +#endif diff --git a/components/wpa_supplicant/src/crypto/crypto_mbedtls-rsa.c b/components/wpa_supplicant/src/crypto/crypto_mbedtls-rsa.c new file mode 100644 index 0000000000..2f50f64eae --- /dev/null +++ b/components/wpa_supplicant/src/crypto/crypto_mbedtls-rsa.c @@ -0,0 +1,356 @@ +/* + * Copyright (C) Copyright 2015-2020 Espressif Systems (Shanghai) PTE LTD, Apache 2.0 License. + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifdef ESP_PLATFORM +#include "mbedtls/bignum.h" +#endif + +#include "utils/includes.h" +#include "utils/common.h" +#include "crypto.h" +#include "common/defs.h" + +#ifdef USE_MBEDTLS_CRYPTO +#include "mbedtls/entropy.h" +#include "mbedtls/ctr_drbg.h" + +#include +#include +#include +#include + +/* Dummy structures; these are just typecast to struct crypto_rsa_key */ +struct crypto_public_key; +struct crypto_private_key; + +static void crypto_dump_verify_info(u32 flags) +{ + char dump_buffer[1024]; + + mbedtls_x509_crt_verify_info(dump_buffer, 1024, " ! ", flags ); + wpa_printf(MSG_ERROR, "%s", dump_buffer); +} + +int crypto_verify_cert(const u8 *cert_start, int certlen, const u8 *ca_cert_start, int ca_certlen) +{ + int ret; + u32 flags = 0; + + mbedtls_x509_crt *cert = os_zalloc(sizeof(mbedtls_x509_crt)); + mbedtls_x509_crt *ca_cert = os_zalloc(sizeof(mbedtls_x509_crt)); + + if (!cert || !ca_cert) { + if (cert) + os_free(cert); + if (ca_cert) + os_free(ca_cert); + wpa_printf(MSG_ERROR, "%s: memory allocation failed", __func__); + return -1; + } + mbedtls_x509_crt_init(cert); + mbedtls_x509_crt_init(ca_cert); + ret = mbedtls_x509_crt_parse(cert, cert_start, certlen); + if (ret < 0) { + wpa_printf(MSG_ERROR, "peer cert parsing failed"); + goto cleanup; + } + ret = mbedtls_x509_crt_parse(ca_cert, ca_cert_start, ca_certlen); + if (ret < 0) { + wpa_printf(MSG_ERROR, "CA cert parsing failed"); + goto cleanup; + } + + ret = mbedtls_x509_crt_verify(cert, ca_cert, NULL, NULL, &flags, NULL, NULL ); + + /* Certification is failed, try to get some more info */ + if (ret != 0) + crypto_dump_verify_info(flags); + +cleanup: + mbedtls_x509_crt_free(cert); + mbedtls_x509_crt_free(ca_cert); + + os_free(cert); + os_free(ca_cert); + + return ret; +} + +struct crypto_public_key * crypto_public_key_import(const u8 *key, size_t len) +{ + int ret; + mbedtls_pk_context *pkey = os_zalloc(sizeof(*pkey)); + + if (!pkey) + return NULL; + + mbedtls_pk_init(pkey); + ret = mbedtls_pk_parse_public_key(pkey, key, len); + + if (ret < 0) { + wpa_printf(MSG_ERROR, "failed to parse public key"); + os_free(pkey); + return NULL; + } + + return (struct crypto_public_key *)pkey; +} + +struct crypto_private_key * crypto_private_key_import(const u8 *key, + size_t len, + const char *passwd) +{ + int ret; + mbedtls_pk_context *pkey = os_zalloc(sizeof(mbedtls_pk_context)); + if (!pkey) + return NULL; + + mbedtls_pk_init(pkey); + + ret = mbedtls_pk_parse_key(pkey, key, len, (const unsigned char *)passwd, passwd ? os_strlen(passwd) : 0); + + if (ret < 0) { + wpa_printf(MSG_ERROR, "failed to parse private key"); + os_free(pkey); + pkey = NULL; + } + + return (struct crypto_private_key *)pkey; +} + +struct crypto_public_key *crypto_public_key_from_cert(const u8 *buf, + size_t len) +{ + int ret; + mbedtls_x509_crt *cert; + mbedtls_pk_context *kctx = os_zalloc(sizeof(*kctx)); + + if (!kctx) { + wpa_printf(MSG_ERROR, "failed to allocate memory"); + return NULL; + } + + cert = os_zalloc(sizeof(mbedtls_x509_crt)); + if (!cert) { + wpa_printf(MSG_ERROR, "failed to allocate memory"); + goto fail; + } + mbedtls_x509_crt_init(cert); + + ret = mbedtls_x509_crt_parse(cert, buf, len); + if (ret < 0) { + wpa_printf(MSG_ERROR, "cert parsing failed"); + goto fail; + } + + mbedtls_pk_init(kctx); + + if(mbedtls_pk_setup(kctx, mbedtls_pk_info_from_type(mbedtls_pk_get_type(&cert->pk))) != 0) { + wpa_printf(MSG_ERROR, "key setup failed"); + goto fail; + } + ret = mbedtls_rsa_copy(mbedtls_pk_rsa(*kctx), mbedtls_pk_rsa(cert->pk)); + + if (ret < 0) { + wpa_printf(MSG_ERROR, "key copy failed"); + goto fail; + } + +cleanup: + mbedtls_x509_crt_free(cert); + os_free(cert); + return (struct crypto_public_key *)kctx; +fail: + os_free(kctx); + kctx = NULL; + goto cleanup; +} + +int crypto_public_key_encrypt_pkcs1_v15(struct crypto_public_key *key, + const u8 *in, size_t inlen, + u8 *out, size_t *outlen) +{ + int ret; + mbedtls_pk_context *pkey = (mbedtls_pk_context *)key; + const char *pers = "rsa_encrypt"; + mbedtls_entropy_context *entropy = os_zalloc(sizeof(*entropy)); + mbedtls_ctr_drbg_context *ctr_drbg = os_zalloc(sizeof(*ctr_drbg)); + + if (!pkey || !entropy || !ctr_drbg) { + if (entropy) + os_free(entropy); + if (ctr_drbg) + os_free(ctr_drbg); + wpa_printf(MSG_ERROR, "failed to allocate memory"); + return -1; + } + + mbedtls_entropy_init( entropy ); + mbedtls_ctr_drbg_init( ctr_drbg ); + + ret = mbedtls_ctr_drbg_seed( ctr_drbg, mbedtls_entropy_func, + entropy, (const unsigned char *) pers, + strlen( pers ) ); + if( ret != 0 ) { + wpa_printf(MSG_ERROR, " failed ! mbedtls_ctr_drbg_seed returned %d", + ret ); + goto cleanup; + } + + ret = mbedtls_rsa_pkcs1_encrypt(mbedtls_pk_rsa(*pkey), mbedtls_ctr_drbg_random, + ctr_drbg, MBEDTLS_RSA_PUBLIC, inlen, in, out); + + if(ret != 0) { + wpa_printf(MSG_ERROR, " failed ! mbedtls_rsa_pkcs1_encrypt returned -0x%04x", -ret); + goto cleanup; + } + *outlen = mbedtls_pk_rsa(*pkey)->len; + +cleanup: + mbedtls_ctr_drbg_free( ctr_drbg ); + mbedtls_entropy_free( entropy ); + os_free(entropy); + os_free(ctr_drbg); + + return ret; +} + + +int crypto_private_key_decrypt_pkcs1_v15(struct crypto_private_key *key, + const u8 *in, size_t inlen, + u8 *out, size_t *outlen) +{ + int ret; + size_t i; + mbedtls_pk_context *pkey = (mbedtls_pk_context *)key; + const char *pers = "rsa_decrypt"; + mbedtls_entropy_context *entropy = os_malloc(sizeof(*entropy)); + mbedtls_ctr_drbg_context *ctr_drbg = os_malloc(sizeof(*ctr_drbg)); + + if (!pkey || !entropy || !ctr_drbg) { + if (entropy) + os_free(entropy); + if (ctr_drbg) + os_free(ctr_drbg); + return -1; + } + mbedtls_ctr_drbg_init( ctr_drbg ); + mbedtls_entropy_init( entropy ); + ret = mbedtls_ctr_drbg_seed(ctr_drbg, mbedtls_entropy_func, + entropy, (const unsigned char *) pers, + strlen(pers)); + + if (ret < 0) + goto cleanup; + + i = mbedtls_pk_rsa(*pkey)->len; + ret = mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_pk_rsa(*pkey), mbedtls_ctr_drbg_random, + ctr_drbg, MBEDTLS_RSA_PRIVATE, &i, in, out, *outlen); + + *outlen = i; + +cleanup: + mbedtls_ctr_drbg_free( ctr_drbg ); + mbedtls_entropy_free( entropy ); + os_free(entropy); + os_free(ctr_drbg); + + return ret; +} + + +int crypto_private_key_sign_pkcs1(struct crypto_private_key *key, + const u8 *in, size_t inlen, + u8 *out, size_t *outlen) +{ + int ret; + mbedtls_pk_context *pkey = (mbedtls_pk_context *)key; + + if((ret = mbedtls_rsa_pkcs1_sign(mbedtls_pk_rsa(*pkey), NULL, NULL, MBEDTLS_RSA_PRIVATE, + (mbedtls_pk_rsa(*pkey))->hash_id, + inlen, in, out)) != 0 ) { + wpa_printf(MSG_ERROR, " failed ! mbedtls_rsa_pkcs1_sign returned %d", ret ); + return -1; + } + *outlen = mbedtls_pk_rsa(*pkey)->len; + + return 0; +} + + +void crypto_public_key_free(struct crypto_public_key *key) +{ + mbedtls_pk_context *pkey = (mbedtls_pk_context *)key; + if (!pkey) + return; + + mbedtls_pk_free(pkey); + os_free(pkey); +} + + +void crypto_private_key_free(struct crypto_private_key *key) +{ + mbedtls_pk_context *pkey = (mbedtls_pk_context *)key; + if (!pkey) + return; + + mbedtls_pk_free(pkey); + os_free(pkey); +} + + +int crypto_public_key_decrypt_pkcs1(struct crypto_public_key *key, + const u8 *crypt, size_t crypt_len, + u8 *plain, size_t *plain_len) +{ + const char *pers = "rsa_decrypt"; + mbedtls_pk_context *pkey = (mbedtls_pk_context *)key; + mbedtls_ctr_drbg_context ctr_drbg; + mbedtls_entropy_context entropy; + size_t i; + + mbedtls_entropy_init( &entropy ); + mbedtls_ctr_drbg_init( &ctr_drbg ); + + int ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, + &entropy, (const unsigned char *) pers, + strlen( pers ) ); + if(ret != 0) { + wpa_printf(MSG_ERROR, " failed ! mbedtls_ctr_drbg_seed returned %d", + ret ); + goto cleanup; + } + + i = mbedtls_pk_rsa(*pkey)->len; + ret = mbedtls_rsa_pkcs1_decrypt(mbedtls_pk_rsa(*pkey), mbedtls_ctr_drbg_random, + &ctr_drbg, MBEDTLS_RSA_PUBLIC, &i, + crypt, plain, *plain_len); + if( ret != 0 ) { + wpa_printf(MSG_ERROR, " failed ! mbedtls_rsa_pkcs1_decrypt returned %d", + ret ); + goto cleanup; + } + *plain_len = i; + +cleanup: + mbedtls_entropy_free( &entropy ); + mbedtls_ctr_drbg_free( &ctr_drbg ); + + return ret; +} +#endif From 7182a654197b9e687b7c4f34ec250645f6f10c8d Mon Sep 17 00:00:00 2001 From: dongyou Date: Tue, 20 Oct 2020 16:26:12 +0800 Subject: [PATCH 02/16] WIFI: Iperf example's parameter opt was uninitialized, may leads setsockoption invalide if it equal to 0 --- examples/wifi/iperf/components/iperf/iperf.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/wifi/iperf/components/iperf/iperf.c b/examples/wifi/iperf/components/iperf/iperf.c index 71d09fee77..3b8402a9b0 100644 --- a/examples/wifi/iperf/components/iperf/iperf.c +++ b/examples/wifi/iperf/components/iperf/iperf.c @@ -124,7 +124,7 @@ static esp_err_t IRAM_ATTR iperf_run_tcp_server(void) int listen_socket; struct timeval t; int sockfd; - int opt; + int opt = 1; listen_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (listen_socket < 0) { @@ -195,7 +195,7 @@ static esp_err_t IRAM_ATTR iperf_run_udp_server(void) int want_recv = 0; uint8_t *buffer; int sockfd; - int opt; + int opt = 1; bool udp_recv_start = true ; sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); @@ -253,7 +253,7 @@ static esp_err_t iperf_run_udp_client(void) int want_send = 0; uint8_t *buffer; int sockfd; - int opt; + int opt = 1; int err; int id; From d39fc7ca0044147c000e6443b489e3b134676633 Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Frintrop Date: Sat, 25 Apr 2020 22:28:45 +0200 Subject: [PATCH 03/16] Fixed links to example directories for fast_scan and scan Signed-off-by: ronghulin https://github.com/espressif/esp-idf/pull/5186 --- examples/wifi/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/wifi/README.md b/examples/wifi/README.md index f08f410384..0aab2e2dc2 100644 --- a/examples/wifi/README.md +++ b/examples/wifi/README.md @@ -30,13 +30,13 @@ See the [README.md](./espnow/README.md) file in the project [espnow](./espnow/). Show how to use fast scan while connecting to an AP. -See the [README.md](./fast_scan/README.md) file in the project [espnow](./espnow/). +See the [README.md](./fast_scan/README.md) file in the project [fast_scan](./fast_scan/). ## scan Show how to scan for all the available APs. -See the [README.md](./scan/README.md) file in the project [espnow](./espnow/). +See the [README.md](./scan/README.md) file in the project [scan](./scan/). # More From 9253d41b1acb898aae1997f5229f87ba4e88ac31 Mon Sep 17 00:00:00 2001 From: ronghulin Date: Fri, 16 Oct 2020 11:47:35 +0800 Subject: [PATCH 04/16] fix TCP retransmission interval --- components/lwip/Kconfig | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/components/lwip/Kconfig b/components/lwip/Kconfig index b75d1a6d0f..563099e20c 100644 --- a/components/lwip/Kconfig +++ b/components/lwip/Kconfig @@ -331,6 +331,13 @@ menu "LWIP" change the memory usage of LWIP, except for preventing new listening TCP connections after the limit is reached. + config LWIP_TCP_HIGH_SPEED_RETRANSMISSION + bool "TCP high speed retransmissions" + default y + help + Speed up the TCP retransmission interval. If disabled, + it is recommended to change the number of SYN retransmissions to 6, + TCP timer interval to 250, and TCP rto time to 3000. config LWIP_TCP_MAXRTX int "Maximum number of retransmissions of data segments" @@ -341,7 +348,8 @@ menu "LWIP" config LWIP_TCP_SYNMAXRTX int "Maximum number of retransmissions of SYN segments" - default 6 + default 6 if !LWIP_TCP_HIGH_SPEED_RETRANSMISSION + default 12 if LWIP_TCP_HIGH_SPEED_RETRANSMISSION range 3 12 help Set maximum number of retransmissions of SYN segments. @@ -475,7 +483,8 @@ menu "LWIP" config LWIP_TCP_RTO_TIME int "Default TCP rto time" - default 3000 + default 3000 if !LWIP_TCP_HIGH_SPEED_RETRANSMISSION + default 1500 if LWIP_TCP_HIGH_SPEED_RETRANSMISSION help Set default TCP rto time for a reasonable initial rto. In bad network environment, recommend set value of rto time to 1500. From 9a7ba5e6fc2f7d762673334cb4d86f4e53d6e21a Mon Sep 17 00:00:00 2001 From: ronghulin Date: Thu, 29 Oct 2020 11:15:53 +0800 Subject: [PATCH 05/16] bugfix: fix TCP timer interval --- components/lwip/Kconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/lwip/Kconfig b/components/lwip/Kconfig index 563099e20c..20dba4d5bb 100644 --- a/components/lwip/Kconfig +++ b/components/lwip/Kconfig @@ -337,7 +337,7 @@ menu "LWIP" help Speed up the TCP retransmission interval. If disabled, it is recommended to change the number of SYN retransmissions to 6, - TCP timer interval to 250, and TCP rto time to 3000. + and TCP initial rto time to 3000. config LWIP_TCP_MAXRTX int "Maximum number of retransmissions of data segments" From e5b52ae4233a0b52969ce7c566dfe75f0f9fcd63 Mon Sep 17 00:00:00 2001 From: "GOPTIONS\\pfrost" Date: Fri, 26 Jul 2019 15:56:40 +0100 Subject: [PATCH 06/16] Reduce log level of hexdumps to verbose Revert "Reduce log level of hexdumps to verbose" Add a menuconfig option to enable or disable the logging in wpa_supplicant Clarify help message --- components/wpa_supplicant/Kconfig | 11 +++++++ .../wpa_supplicant/include/utils/wpa_debug.h | 29 +++++++++---------- .../port/include/supplicant_opt.h | 4 +++ 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/components/wpa_supplicant/Kconfig b/components/wpa_supplicant/Kconfig index 2ac88e4da8..4b79dc8c9a 100644 --- a/components/wpa_supplicant/Kconfig +++ b/components/wpa_supplicant/Kconfig @@ -25,4 +25,15 @@ menu "Supplicant" button bit without setting virtual/physical display/button bit which will cause M2 validation fail, bypassing WPS-Config method validation. + config WPA_DEBUG_PRINT + bool "Print debug messages from WPA Supplicant" + default n + help + Select this option to print logging information from WPA supplicant, + this includes handshake information and key hex dumps depending + on the project logging level. + + Enabling this could increase the build size ~60kb + depending on the project logging level. + endmenu diff --git a/components/wpa_supplicant/include/utils/wpa_debug.h b/components/wpa_supplicant/include/utils/wpa_debug.h index b204ec7d87..8f5cc8b914 100644 --- a/components/wpa_supplicant/include/utils/wpa_debug.h +++ b/components/wpa_supplicant/include/utils/wpa_debug.h @@ -47,6 +47,7 @@ void wpa_debug_close_file(void); */ void wpa_debug_print_timestamp(void); +#ifdef DEBUG_PRINT /** * wpa_printf - conditional printf * @level: priority level (MSG_*) of the message @@ -58,21 +59,6 @@ void wpa_debug_print_timestamp(void); * * Note: New line '\n' is added to the end of the text when printing to stdout. */ -#define DEBUG_PRINT -#define MSG_PRINT - -/** - * wpa_hexdump - conditional hex dump - * @level: priority level (MSG_*) of the message - * @title: title of for the message - * @buf: data buffer to be dumped - * @len: length of the buf - * - * This function is used to print conditional debugging and error messages. The - * output may be directed to stdout, stderr, and/or syslog based on - * configuration. The contents of buf is printed out has hex dump. - */ -#ifdef DEBUG_PRINT #define wpa_printf(level,fmt, args...) ESP_LOG_LEVEL_LOCAL(level, TAG, fmt, ##args) void wpa_dump_mem(char* desc, uint8_t *addr, uint16_t len); @@ -85,7 +71,17 @@ static inline void wpa_hexdump_ascii_key(int level, const char *title, const u8 { } - +/** + * wpa_hexdump - conditional hex dump + * @level: priority level (MSG_*) of the message + * @title: title of for the message + * @buf: data buffer to be dumped + * @len: length of the buf + * + * This function is used to print conditional debugging and error messages. The + * output may be directed to stdout, stderr, and/or syslog based on + * configuration. The contents of buf is printed out has hex dump. + */ void wpa_hexdump(int level, const char *title, const u8 *buf, size_t len); static inline void wpa_hexdump_buf(int level, const char *title, @@ -151,6 +147,7 @@ void wpa_hexdump_ascii_key(int level, const char *title, const u8 *buf, #else #define wpa_printf(level,fmt, args...) #define wpa_hexdump(...) +#define wpa_dump_mem(...) #define wpa_hexdump_buf(...) #define wpa_hexdump_key(...) #define wpa_hexdump_buf_key(...) diff --git a/components/wpa_supplicant/port/include/supplicant_opt.h b/components/wpa_supplicant/port/include/supplicant_opt.h index 23e6872753..a3d4c66620 100644 --- a/components/wpa_supplicant/port/include/supplicant_opt.h +++ b/components/wpa_supplicant/port/include/supplicant_opt.h @@ -24,4 +24,8 @@ #define CONFIG_TLSV12 #endif +#if CONFIG_WPA_DEBUG_PRINT +#define DEBUG_PRINT +#endif + #endif /* _SUPPLICANT_OPT_H */ From 395fa980d8ed1072bc2ceb6da9319e53ecfa9a85 Mon Sep 17 00:00:00 2001 From: Hrudaynath Dhabe Date: Thu, 30 Jan 2020 19:25:45 +0530 Subject: [PATCH 07/16] wpa_supplicant: Fix configurable debug log feature's warning issue --- .../wpa_supplicant/include/utils/wpa_debug.h | 16 ++++++++-------- .../wpa_supplicant/src/eap_peer/eap_tls_common.c | 2 ++ .../wpa_supplicant/src/esp_supplicant/esp_wpa2.c | 4 +++- .../wpa_supplicant/src/wps/wps_registrar.c | 12 ++++++++++-- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/components/wpa_supplicant/include/utils/wpa_debug.h b/components/wpa_supplicant/include/utils/wpa_debug.h index 8f5cc8b914..e32c697fe9 100644 --- a/components/wpa_supplicant/include/utils/wpa_debug.h +++ b/components/wpa_supplicant/include/utils/wpa_debug.h @@ -145,14 +145,14 @@ void wpa_hexdump_ascii(int level, const char *title, const u8 *buf, void wpa_hexdump_ascii_key(int level, const char *title, const u8 *buf, size_t len); #else -#define wpa_printf(level,fmt, args...) -#define wpa_hexdump(...) -#define wpa_dump_mem(...) -#define wpa_hexdump_buf(...) -#define wpa_hexdump_key(...) -#define wpa_hexdump_buf_key(...) -#define wpa_hexdump_ascii(...) -#define wpa_hexdump_ascii_key(...) +#define wpa_printf(level,fmt, args...) do {} while(0) +#define wpa_hexdump(...) do {} while(0) +#define wpa_dump_mem(...) do {} while(0) +#define wpa_hexdump_buf(...) do {} while(0) +#define wpa_hexdump_key(...) do {} while(0) +#define wpa_hexdump_buf_key(...) do {} while(0) +#define wpa_hexdump_ascii(...) do {} while(0) +#define wpa_hexdump_ascii_key(...) do {} while(0) #endif #define wpa_auth_logger diff --git a/components/wpa_supplicant/src/eap_peer/eap_tls_common.c b/components/wpa_supplicant/src/eap_peer/eap_tls_common.c index 1de15542a6..9c93f6d0ca 100644 --- a/components/wpa_supplicant/src/eap_peer/eap_tls_common.c +++ b/components/wpa_supplicant/src/eap_peer/eap_tls_common.c @@ -1005,7 +1005,9 @@ get_defaults: int eap_peer_tls_phase2_nak(struct eap_method_type *types, size_t num_types, struct eap_hdr *hdr, struct wpabuf **resp) { +#ifdef DEBUG_PRINT u8 *pos = (u8 *) (hdr + 1); +#endif size_t i; /* TODO: add support for expanded Nak */ diff --git a/components/wpa_supplicant/src/esp_supplicant/esp_wpa2.c b/components/wpa_supplicant/src/esp_supplicant/esp_wpa2.c index fb3105835f..92cdd00c46 100644 --- a/components/wpa_supplicant/src/esp_supplicant/esp_wpa2.c +++ b/components/wpa_supplicant/src/esp_supplicant/esp_wpa2.c @@ -187,7 +187,6 @@ void wpa2_task(void *pvParameters ) ETSEvent *e; struct eap_sm *sm = gEapSm; bool task_del = false; - uint32_t sig = 0; if (!sm) { return; @@ -195,7 +194,10 @@ void wpa2_task(void *pvParameters ) for (;;) { if ( pdPASS == xQueueReceive(s_wpa2_queue, &e, portMAX_DELAY) ) { +#ifdef DEBUG_PRINT + uint32_t sig = 0; sig = e->sig; +#endif if (e->sig < SIG_WPA2_MAX) { DATA_MUTEX_TAKE(); if(sm->wpa2_sig_cnt[e->sig]) { diff --git a/components/wpa_supplicant/src/wps/wps_registrar.c b/components/wpa_supplicant/src/wps/wps_registrar.c index b8778f2e5d..1ce30c3b96 100644 --- a/components/wpa_supplicant/src/wps/wps_registrar.c +++ b/components/wpa_supplicant/src/wps/wps_registrar.c @@ -2423,14 +2423,18 @@ static int wps_process_wps_state(struct wps_data *wps, const u8 *state) static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc) { - u16 a; +#ifdef DEBUG_PRINT + u16 a; +#endif if (assoc == NULL) { wpa_printf(MSG_DEBUG, "WPS: No Association State received"); return -1; } +#ifdef DEBUG_PRINT a = WPA_GET_BE16(assoc); +#endif wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a); return 0; @@ -2439,14 +2443,18 @@ static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc) static int wps_process_config_error(struct wps_data *wps, const u8 *err) { - u16 e; +#ifdef DEBUG_PRINT + u16 e; +#endif if (err == NULL) { wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received"); return -1; } +#ifdef DEBUG_PRINT e = WPA_GET_BE16(err); +#endif wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e); return 0; From 7b4a2560a61d6639753199e02a155ce0876cd566 Mon Sep 17 00:00:00 2001 From: Hrudaynath Dhabe Date: Sun, 19 Apr 2020 16:40:06 +0530 Subject: [PATCH 08/16] wpa_supplicant: Minor bugfix with wpa_supplicant debug logs. --- components/wpa_supplicant/src/esp_supplicant/esp_wpa2.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/components/wpa_supplicant/src/esp_supplicant/esp_wpa2.c b/components/wpa_supplicant/src/esp_supplicant/esp_wpa2.c index 92cdd00c46..6efa86d9f9 100644 --- a/components/wpa_supplicant/src/esp_supplicant/esp_wpa2.c +++ b/components/wpa_supplicant/src/esp_supplicant/esp_wpa2.c @@ -194,10 +194,6 @@ void wpa2_task(void *pvParameters ) for (;;) { if ( pdPASS == xQueueReceive(s_wpa2_queue, &e, portMAX_DELAY) ) { -#ifdef DEBUG_PRINT - uint32_t sig = 0; - sig = e->sig; -#endif if (e->sig < SIG_WPA2_MAX) { DATA_MUTEX_TAKE(); if(sm->wpa2_sig_cnt[e->sig]) { @@ -234,7 +230,7 @@ void wpa2_task(void *pvParameters ) break; } else { if (s_wifi_wpa2_sync_sem) { - wpa_printf(MSG_DEBUG, "WPA2: wifi->wpa2 api completed sig(%d)", sig); + wpa_printf(MSG_DEBUG, "WPA2: wifi->wpa2 api completed sig(%d)", e->sig); xSemaphoreGive(s_wifi_wpa2_sync_sem); } else { wpa_printf(MSG_ERROR, "WPA2: null wifi->wpa2 sync sem"); @@ -247,7 +243,7 @@ void wpa2_task(void *pvParameters ) wpa_printf(MSG_DEBUG, "WPA2: task deleted"); s_wpa2_queue = NULL; if (s_wifi_wpa2_sync_sem) { - wpa_printf(MSG_DEBUG, "WPA2: wifi->wpa2 api completed sig(%d)", sig); + wpa_printf(MSG_DEBUG, "WPA2: wifi->wpa2 api completed sig(%d)", e->sig); xSemaphoreGive(s_wifi_wpa2_sync_sem); } else { wpa_printf(MSG_ERROR, "WPA2: null wifi->wpa2 sync sem"); From 280a342826d95d999065dde43b81e5cab0e2d340 Mon Sep 17 00:00:00 2001 From: "kapil.gupta" Date: Thu, 10 Sep 2020 15:39:54 +0530 Subject: [PATCH 09/16] esp_wifi: Add support for 802.1x sha256 auth key mode Closes https://github.com/espressif/esp-idf/issues/5805 --- .../src/esp_supplicant/esp_wifi_driver.h | 3 ++- components/wpa_supplicant/src/rsn_supp/wpa.c | 23 +++++++++++++++---- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/components/wpa_supplicant/src/esp_supplicant/esp_wifi_driver.h b/components/wpa_supplicant/src/esp_supplicant/esp_wifi_driver.h index 4415f200a8..e394658e53 100644 --- a/components/wpa_supplicant/src/esp_supplicant/esp_wifi_driver.h +++ b/components/wpa_supplicant/src/esp_supplicant/esp_wifi_driver.h @@ -65,7 +65,8 @@ enum { WPA2_AUTH_CCKM = 0x07, WPA2_AUTH_PSK_SHA256= 0x08, WPA3_AUTH_PSK = 0x09, - WPA2_AUTH_INVALID = 0x0a, + WPA2_AUTH_ENT_SHA256= 0x0a, + WPA2_AUTH_INVALID = 0x0b, }; typedef enum { diff --git a/components/wpa_supplicant/src/rsn_supp/wpa.c b/components/wpa_supplicant/src/rsn_supp/wpa.c index 00297d5b0b..898fdf54db 100644 --- a/components/wpa_supplicant/src/rsn_supp/wpa.c +++ b/components/wpa_supplicant/src/rsn_supp/wpa.c @@ -136,6 +136,20 @@ uint32_t cipher_type_map_public_to_supp(wifi_cipher_type_t cipher) } } +static bool is_wpa2_enterprise_connection(void) +{ + uint8_t authmode; + + if (esp_wifi_sta_prof_is_wpa2_internal()) { + authmode = esp_wifi_sta_get_prof_authmode_internal(); + if ((authmode == WPA2_AUTH_ENT) || (authmode == WPA2_AUTH_ENT_SHA256)) { + return true; + } + } + + return false; +} + /** * get_bssid - Get the current BSSID * @priv: private driver interface data @@ -587,8 +601,7 @@ void wpa_supplicant_process_1_of_4(struct wpa_sm *sm, if (res) goto failed; - if (esp_wifi_sta_prof_is_wpa2_internal() && - esp_wifi_sta_get_prof_authmode_internal() == WPA2_AUTH_ENT) { + if (is_wpa2_enterprise_connection()) { pmksa_cache_set_current(sm, NULL, sm->bssid, 0, 0); } @@ -2078,6 +2091,8 @@ void wpa_set_profile(u32 wpa_proto, u8 auth_mode) sm->proto = wpa_proto; if (auth_mode == WPA2_AUTH_ENT) { sm->key_mgmt = WPA_KEY_MGMT_IEEE8021X; /* for wpa2 enterprise */ + } else if (auth_mode == WPA2_AUTH_ENT_SHA256) { + sm->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256; /* for wpa2 enterprise sha256 */ } else if (auth_mode == WPA2_AUTH_PSK_SHA256) { sm->key_mgmt = WPA_KEY_MGMT_PSK_SHA256; } else if (auth_mode == WPA3_AUTH_PSK) { @@ -2116,9 +2131,7 @@ int wpa_set_bss(char *macddr, char * bssid, u8 pairwise_cipher, u8 group_cipher, memcpy(sm->bssid, bssid, ETH_ALEN); sm->ap_notify_completed_rsne = esp_wifi_sta_is_ap_notify_completed_rsne_internal(); - if (sm->key_mgmt == WPA_KEY_MGMT_SAE || - (esp_wifi_sta_prof_is_wpa2_internal() && - esp_wifi_sta_get_prof_authmode_internal() == WPA2_AUTH_ENT)) { + if (sm->key_mgmt == WPA_KEY_MGMT_SAE || is_wpa2_enterprise_connection()) { if (!esp_wifi_skip_supp_pmkcaching()) { pmksa_cache_set_current(sm, NULL, (const u8*) bssid, 0, 0); wpa_sm_set_pmk_from_pmksa(sm); From 2252c75588f46d19109ca80ca67c35c4fffc89ed Mon Sep 17 00:00:00 2001 From: dongyou Date: Mon, 26 Oct 2020 20:27:44 +0800 Subject: [PATCH 10/16] esp_wifi: update description for WiFi APIs 1. Add description for esp_wifi_set_inactive_time() 2. Add documentation to avoid using WiFi modem sleep for WiFi/BT coexistence 3. Remove description of unusable API esp_wifi_restart() 4. Update esp_now_fetch_peer discription 5. Update table format prblm for esp_wifi_set_max_tx_power() 6. Update description for ssid and password --- components/esp_common/src/esp_err_to_name.c | 2 +- components/esp_wifi/include/esp_now.h | 2 +- components/esp_wifi/include/esp_wifi.h | 32 +++----------------- components/esp_wifi/include/esp_wifi_types.h | 6 ++-- docs/en/api-guides/wifi.rst | 17 +++++------ 5 files changed, 17 insertions(+), 42 deletions(-) diff --git a/components/esp_common/src/esp_err_to_name.c b/components/esp_common/src/esp_err_to_name.c index 1832fd9bf8..b40c5fac81 100644 --- a/components/esp_common/src/esp_err_to_name.c +++ b/components/esp_common/src/esp_err_to_name.c @@ -340,7 +340,7 @@ static const esp_err_msg_t esp_err_msg_table[] = { ERR_TBL_IT(ESP_ERR_WIFI_POST), /* 12306 0x3012 Failed to post the event to WiFi task */ # endif # ifdef ESP_ERR_WIFI_INIT_STATE - ERR_TBL_IT(ESP_ERR_WIFI_INIT_STATE), /* 12307 0x3013 Invalod WiFi state when init/deinit is called */ + ERR_TBL_IT(ESP_ERR_WIFI_INIT_STATE), /* 12307 0x3013 Invalid WiFi state when init/deinit is called */ # endif # ifdef ESP_ERR_WIFI_STOP_STATE ERR_TBL_IT(ESP_ERR_WIFI_STOP_STATE), /* 12308 0x3014 Returned when WiFi is stopping */ diff --git a/components/esp_wifi/include/esp_now.h b/components/esp_wifi/include/esp_now.h index 981c9001e4..2f20c5d392 100644 --- a/components/esp_wifi/include/esp_now.h +++ b/components/esp_wifi/include/esp_now.h @@ -252,7 +252,7 @@ esp_err_t esp_now_mod_peer(const esp_now_peer_info_t *peer); esp_err_t esp_now_get_peer(const uint8_t *peer_addr, esp_now_peer_info_t *peer); /** - * @brief Fetch a peer from peer list + * @brief Fetch a peer from peer list. Only return the peer which address is unicast, for the multicast/broadcast address, the function will ignore and try to find the next in the peer list. * * @param from_head fetch from head of list or not * @param peer peer information diff --git a/components/esp_wifi/include/esp_wifi.h b/components/esp_wifi/include/esp_wifi.h index 640bdbbca8..7c9cdb9e18 100644 --- a/components/esp_wifi/include/esp_wifi.h +++ b/components/esp_wifi/include/esp_wifi.h @@ -85,7 +85,7 @@ extern "C" { #define ESP_ERR_WIFI_NOT_CONNECT (ESP_ERR_WIFI_BASE + 15) /*!< Station still in disconnect status */ #define ESP_ERR_WIFI_POST (ESP_ERR_WIFI_BASE + 18) /*!< Failed to post the event to WiFi task */ -#define ESP_ERR_WIFI_INIT_STATE (ESP_ERR_WIFI_BASE + 19) /*!< Invalod WiFi state when init/deinit is called */ +#define ESP_ERR_WIFI_INIT_STATE (ESP_ERR_WIFI_BASE + 19) /*!< Invalid WiFi state when init/deinit is called */ #define ESP_ERR_WIFI_STOP_STATE (ESP_ERR_WIFI_BASE + 20) /*!< Returned when WiFi is stopping */ /** @@ -309,7 +309,7 @@ esp_err_t esp_wifi_stop(void); * @brief Restore WiFi stack persistent settings to default values * * This function will reset settings made using the following APIs: - * - esp_wifi_get_auto_connect, + * - esp_wifi_set_bandwidth, * - esp_wifi_set_protocol, * - esp_wifi_set_config related * - esp_wifi_set_mode @@ -883,32 +883,8 @@ esp_err_t esp_wifi_set_vendor_ie_cb(esp_vendor_ie_cb_t cb, void *ctx); * @attention 3. Mapping Table {Power, max_tx_power} = {{8, 2}, {20, 5}, {28, 7}, {34, 8}, {44, 11}, * {52, 13}, {56, 14}, {60, 15}, {66, 16}, {72, 18}, {78, 20}}. * @attention 4. Param power unit is 0.25dBm, range is [8, 78] corresponding to 2dBm - 20dBm. - * @attention 5. Relationship between set value and actual value. As follows: - * +------------+--------------+ - * | set value | actual value | - * +============+==============+ - * | [8, 19] | 8 | - * +------------+--------------+ - * | [20, 27] | 20 | - * +------------+--------------+ - * | [28, 33] | 28 | - * +------------+--------------+ - * | [34, 43] | 34 | - * +------------+--------------+ - * | [44, 51] | 44 | - * +------------+--------------+ - * | [52, 55] | 52 | - * +------------+--------------+ - * | [56, 59] | 56 | - * +------------+--------------+ - * | [60, 65] | 60 | - * +------------+--------------+ - * | [66, 71] | 66 | - * +------------+--------------+ - * | [72, 77] | 72 | - * +------------+--------------+ - * | 78 | 78 | - * +------------+--------------+ + * @attention 5. Relationship between set value and actual value. As follows: {set value range, actual value} = {{[8, 19],8}, {[20, 27],20}, {[28, 33],28}, {[34, 43],34}, {[44, 51],44}, {[52, 55],52}, {[56, 59],56}, {[60, 65],60}, {[66, 71],66}, {[72, 77],72}, {78,78}} + * * @param power Maximum WiFi transmitting power. * * @return diff --git a/components/esp_wifi/include/esp_wifi_types.h b/components/esp_wifi/include/esp_wifi_types.h index 5c9d5e2b91..be436e03db 100644 --- a/components/esp_wifi/include/esp_wifi_types.h +++ b/components/esp_wifi/include/esp_wifi_types.h @@ -214,7 +214,7 @@ typedef struct { /** @brief Soft-AP configuration settings for the ESP32 */ typedef struct { uint8_t ssid[32]; /**< SSID of ESP32 soft-AP. If ssid_len field is 0, this must be a Null terminated string. Otherwise, length is set according to ssid_len. */ - uint8_t password[64]; /**< Password of ESP32 soft-AP. Null terminated string. */ + uint8_t password[64]; /**< Password of ESP32 soft-AP. */ uint8_t ssid_len; /**< Optional length of SSID field. */ uint8_t channel; /**< Channel of ESP32 soft-AP */ wifi_auth_mode_t authmode; /**< Auth mode of ESP32 soft-AP. Do not support AUTH_WEP in soft-AP mode */ @@ -225,8 +225,8 @@ typedef struct { /** @brief STA configuration settings for the ESP32 */ typedef struct { - uint8_t ssid[32]; /**< SSID of target AP. Null terminated string. */ - uint8_t password[64]; /**< Password of target AP. Null terminated string.*/ + uint8_t ssid[32]; /**< SSID of target AP. */ + uint8_t password[64]; /**< Password of target AP. */ wifi_scan_method_t scan_method; /**< do all channel scan or fast scan */ bool bssid_set; /**< whether set MAC address of target AP or not. Generally, station_config.bssid_set needs to be 0; and it needs to be 1 only when users need to check the MAC address of the AP.*/ uint8_t bssid[6]; /**< MAC address of target AP*/ diff --git a/docs/en/api-guides/wifi.rst b/docs/en/api-guides/wifi.rst index b57461e416..64c3de2c96 100644 --- a/docs/en/api-guides/wifi.rst +++ b/docs/en/api-guides/wifi.rst @@ -163,9 +163,9 @@ WIFI_EVENT_STA_DISCONNECTED ++++++++++++++++++++++++++++++++++++ This event can be generated in the following scenarios: - - When esp_wifi_disconnect(), or esp_wifi_stop(), or esp_wifi_deinit(), or esp_wifi_restart() is called and the station is already connected to the AP. - - When esp_wifi_connect() is called, but the Wi-Fi driver fails to set up a connection with the AP due to certain reasons, e.g. the scan fails to find the target AP, authentication times out, etc. If there are more than one AP with the same SSID, the disconnected event is raised after the station fails to connect all of the found APs. - - When the Wi-Fi connection is disrupted because of specific reasons, e.g., the station continuously loses N beacons, the AP kicks off the station, the AP's authentication mode is changed, etc. + - When :cpp:func:`esp_wifi_disconnect()`, or :cpp:func:`esp_wifi_stop()`, or :cpp:func:`esp_wifi_deinit()` is called and the station is already connected to the AP. + - When :cpp:func:`esp_wifi_connect()` is called, but the Wi-Fi driver fails to set up a connection with the AP due to certain reasons, e.g. the scan fails to find the target AP, authentication times out, etc. If there are more than one AP with the same SSID, the disconnected event is raised after the station fails to connect all of the found APs. + - When the Wi-Fi connection is disrupted because of specific reasons, e.g., the station continuously loses N beacons, the AP kicks off the station, the AP's authentication mode is changed, etc. Upon receiving this event, the default behavior of the event task is: - Shuts down the station's LwIP netif. @@ -224,8 +224,8 @@ WIFI_EVENT_AP_STADISCONNECTED ++++++++++++++++++++++++++++++++++++ This event can happen in the following scenarios: - - The application calls esp_wifi_disconnect(), or esp_wifi_deauth_sta(), to manually disconnect the station. - - The Wi-Fi driver kicks off the station, e.g. because the AP has not received any packets in the past five minutes, etc. + - The application calls :cpp:func:`esp_wifi_disconnect()`, or esp_wifi_deauth_sta(), to manually disconnect the station. + - The Wi-Fi driver kicks off the station, e.g. because the AP has not received any packets in the past five minutes, etc. The time can be modified by :cpp:func:`esp_wifi_set_inactive_time`. - The station kicks off the AP. When this event happens, the event task will do nothing, but the application event callback needs to do something, e.g., close the socket which is related to this station, etc. @@ -1444,7 +1444,7 @@ In maximum power save mode, station wakes up every listen interval to receive be Call ``esp_wifi_set_ps(WIFI_PS_MIN_MODEM)`` to enable Modem-sleep minimum power save mode or ``esp_wifi_set_ps(WIFI_PS_MAX_MODEM)`` to enable Modem-sleep maximum power save mode after calling :cpp:func:`esp_wifi_init`. When station connects to AP, Modem-sleep will start. When station disconnects from AP, Modem-sleep will stop. -Call ``esp_wifi_set_ps(WIFI_PS_NONE)`` to disable modem sleep entirely. This has much higher power consumption, but provides minimum latency for receiving Wi-Fi data in real time. When modem sleep is enabled, received Wi-Fi data can be delayed for as long as the DTIM period (minimum power save mode) or the listen interval (maximum power save mode). +Call ``esp_wifi_set_ps(WIFI_PS_NONE)`` to disable modem sleep entirely. This has much higher power consumption, but provides minimum latency for receiving Wi-Fi data in real time. When modem sleep is enabled, received Wi-Fi data can be delayed for as long as the DTIM period (minimum power save mode) or the listen interval (maximum power save mode). Disabling modem sleep entirely is not possible for Wi-Fi and Bluetooth coexist mode. The default Modem-sleep mode is WIFI_PS_MIN_MODEM. @@ -1491,9 +1491,7 @@ When the throughput is tested by iperf example, the sdkconfig is :idf_file:`exam Wi-Fi 80211 Packet Send --------------------------- -**Important notes: The API esp_wifi_80211_tx is not available in IDF 2.1, but will be so in the upcoming release.** - -The esp_wifi_80211_tx API can be used to: +The :cpp:func:`esp_wifi_80211_tx` API can be used to: - Send the beacon, probe request, probe response, action frame. - Send the non-QoS data frame. @@ -1842,6 +1840,7 @@ The peak heap memory that Wi-Fi consumes is the **theoretically-maximum memory** - the maximum packet size that the Wi-Fi driver can send: wifi_tx_pkt_size_max So, the peak memory that the Wi-Fi driver consumes can be calculated with the following formula: + wifi_dynamic_peek_memory = (wifi_rx_dynamic_buf_num * wifi_rx_pkt_size_max) + (wifi_tx_dynamic_buf_num * wifi_tx_pkt_size_max) Generally, we do not need to care about the dynamic tx long buffers and dynamic tx long long buffers, because they are management frames which only have a small impact on the system. From 5616dd57dad85ce1cb4c2d51d1e30ae72cc1a10a Mon Sep 17 00:00:00 2001 From: Xia Xiaotian Date: Fri, 6 Nov 2020 20:38:10 +0800 Subject: [PATCH 11/16] Wi-Fi: set softap beacon DTIM count according to TSF timer --- components/esp_wifi/include/esp_wifi_types.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/esp_wifi/include/esp_wifi_types.h b/components/esp_wifi/include/esp_wifi_types.h index be436e03db..d496aa6a10 100644 --- a/components/esp_wifi/include/esp_wifi_types.h +++ b/components/esp_wifi/include/esp_wifi_types.h @@ -220,7 +220,7 @@ typedef struct { wifi_auth_mode_t authmode; /**< Auth mode of ESP32 soft-AP. Do not support AUTH_WEP in soft-AP mode */ uint8_t ssid_hidden; /**< Broadcast SSID or not, default 0, broadcast the SSID */ uint8_t max_connection; /**< Max number of stations allowed to connect in, default 4, max 10 */ - uint16_t beacon_interval; /**< Beacon interval, 100 ~ 60000 ms, default 100 ms */ + uint16_t beacon_interval; /**< Beacon interval which should be multiples of 100. Unit: TU(time unit, 1 TU = 1024 us). Range: 100 ~ 60000. Default value: 100 */ } wifi_ap_config_t; /** @brief STA configuration settings for the ESP32 */ From 2c85d3dd627f52cbcc9c9f009b07f4188bdacfbe Mon Sep 17 00:00:00 2001 From: ChenJianxing Date: Wed, 27 May 2020 15:06:53 +0800 Subject: [PATCH 12/16] esp_wifi: remove wifi tx buffer limits --- components/esp_wifi/Kconfig | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/components/esp_wifi/Kconfig b/components/esp_wifi/Kconfig index 47b548f213..c71e57b676 100644 --- a/components/esp_wifi/Kconfig +++ b/components/esp_wifi/Kconfig @@ -14,8 +14,7 @@ menu "Wi-Fi" config ESP32_WIFI_STATIC_RX_BUFFER_NUM int "Max number of WiFi static RX buffers" - range 2 25 if !SPIRAM_TRY_ALLOCATE_WIFI_LWIP - range 8 25 if SPIRAM_TRY_ALLOCATE_WIFI_LWIP + range 2 25 default 10 if !SPIRAM_TRY_ALLOCATE_WIFI_LWIP default 16 if SPIRAM_TRY_ALLOCATE_WIFI_LWIP help @@ -79,7 +78,7 @@ menu "Wi-Fi" config ESP32_WIFI_STATIC_TX_BUFFER_NUM int "Max number of WiFi static TX buffers" depends on ESP32_WIFI_STATIC_TX_BUFFER - range 6 64 + range 1 64 default 16 help Set the number of WiFi static TX buffers. Each buffer takes approximately 1.6KB of RAM. @@ -94,7 +93,7 @@ menu "Wi-Fi" config ESP32_WIFI_DYNAMIC_TX_BUFFER_NUM int "Max number of WiFi dynamic TX buffers" depends on ESP32_WIFI_DYNAMIC_TX_BUFFER - range 16 128 + range 1 128 default 32 help Set the number of WiFi dynamic TX buffers. The size of each dynamic TX buffer is not fixed, From b2dec946b66854b10d06c4a1618416dc6bcfaae2 Mon Sep 17 00:00:00 2001 From: Nachiket Kukade Date: Fri, 14 Feb 2020 12:23:27 +0530 Subject: [PATCH 13/16] esp_wifi: Update docs for wifi headers --- components/esp_wifi/include/esp_wifi_types.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/components/esp_wifi/include/esp_wifi_types.h b/components/esp_wifi/include/esp_wifi_types.h index d496aa6a10..aa57e921b8 100644 --- a/components/esp_wifi/include/esp_wifi_types.h +++ b/components/esp_wifi/include/esp_wifi_types.h @@ -314,15 +314,15 @@ typedef struct { typedef struct { signed rssi:8; /**< Received Signal Strength Indicator(RSSI) of packet. unit: dBm */ unsigned rate:5; /**< PHY rate encoding of the packet. Only valid for non HT(11bg) packet */ - unsigned :1; /**< reserve */ + unsigned :1; /**< reserved */ unsigned sig_mode:2; /**< 0: non HT(11bg) packet; 1: HT(11n) packet; 3: VHT(11ac) packet */ - unsigned :16; /**< reserve */ + unsigned :16; /**< reserved */ unsigned mcs:7; /**< Modulation Coding Scheme. If is HT(11n) packet, shows the modulation, range from 0 to 76(MSC0 ~ MCS76) */ unsigned cwb:1; /**< Channel Bandwidth of the packet. 0: 20MHz; 1: 40MHz */ - unsigned :16; /**< reserve */ - unsigned smoothing:1; /**< reserve */ - unsigned not_sounding:1; /**< reserve */ - unsigned :1; /**< reserve */ + unsigned :16; /**< reserved */ + unsigned smoothing:1; /**< reserved */ + unsigned not_sounding:1; /**< reserved */ + unsigned :1; /**< reserved */ unsigned aggregation:1; /**< Aggregation. 0: MPDU packet; 1: AMPDU packet */ unsigned stbc:2; /**< Space Time Block Code(STBC). 0: non STBC packet; 1: STBC packet */ unsigned fec_coding:1; /**< Flag is set for 11n packets which are LDPC */ @@ -331,13 +331,13 @@ typedef struct { unsigned ampdu_cnt:8; /**< ampdu cnt */ unsigned channel:4; /**< primary channel on which this packet is received */ unsigned secondary_channel:4; /**< secondary channel on which this packet is received. 0: none; 1: above; 2: below */ - unsigned :8; /**< reserve */ + unsigned :8; /**< reserved */ unsigned timestamp:32; /**< timestamp. The local time when this packet is received. It is precise only if modem sleep or light sleep is not enabled. unit: microsecond */ - unsigned :32; /**< reserve */ - unsigned :31; /**< reserve */ + unsigned :32; /**< reserved */ + unsigned :31; /**< reserved */ unsigned ant:1; /**< antenna number from which this packet is received. 0: WiFi antenna 0; 1: WiFi antenna 1 */ unsigned sig_len:12; /**< length of packet including Frame Check Sequence(FCS) */ - unsigned :12; /**< reserve */ + unsigned :12; /**< reserved */ unsigned rx_state:8; /**< state of the packet. 0: no error; others: error numbers which are not public */ } wifi_pkt_rx_ctrl_t; From 2db6b1578eb6dbabe434e956aa7445fd600eae5c Mon Sep 17 00:00:00 2001 From: Nachiket Kukade Date: Wed, 28 Oct 2020 18:21:50 +0530 Subject: [PATCH 14/16] esp_wifi: Update wifi lib 1. Use flag ESP32_WIFI_ENABLE_WPA3_SAE to control WPA3 code, disabling it code footprint reduces by 7.7kB in libwpa_supplicant.a 2. Fix handling of multiple AP credentials in WPS, apps need update to handle the new event for the fix to work --- .../esp_event/include/esp_event_legacy.h | 4 + components/esp_wifi/include/esp_wifi_types.h | 13 ++++ components/wpa_supplicant/CMakeLists.txt | 5 +- components/wpa_supplicant/component.mk | 6 +- .../wpa_supplicant/src/common/wpa_common.c | 6 +- .../src/esp_supplicant/esp_wpa3_i.h | 4 + .../src/esp_supplicant/esp_wps.c | 75 ++++++++++++------- .../wpa_supplicant/src/rsn_supp/wpa_ie.c | 2 +- components/wpa_supplicant/src/wps/wps.h | 13 ++-- .../wpa_supplicant/src/wps/wps_enrollee.c | 9 +-- examples/wifi/wps/main/wps.c | 54 ++++++++++++- 11 files changed, 142 insertions(+), 49 deletions(-) diff --git a/components/esp_event/include/esp_event_legacy.h b/components/esp_event/include/esp_event_legacy.h index 44e95546a1..4678330d14 100644 --- a/components/esp_event/include/esp_event_legacy.h +++ b/components/esp_event/include/esp_event_legacy.h @@ -80,6 +80,9 @@ typedef wifi_event_sta_authmode_change_t system_event_sta_authmode_change_t; /** Argument structure of SYSTEM_EVENT_STA_WPS_ER_PIN event */ typedef wifi_event_sta_wps_er_pin_t system_event_sta_wps_er_pin_t; +/** Argument structure of SYSTEM_EVENT_STA_WPS_ER_PIN event */ +typedef wifi_event_sta_wps_er_success_t system_event_sta_wps_er_success_t; + /** Argument structure of event */ typedef wifi_event_ap_staconnected_t system_event_ap_staconnected_t; @@ -107,6 +110,7 @@ typedef union { system_event_sta_got_ip_t got_ip; /*!< ESP32 station got IP, first time got IP or when IP is changed */ system_event_sta_wps_er_pin_t sta_er_pin; /*!< ESP32 station WPS enrollee mode PIN code received */ system_event_sta_wps_fail_reason_t sta_er_fail_reason; /*!< ESP32 station WPS enrollee mode failed reason code received */ + system_event_sta_wps_er_success_t sta_er_success; /*!< ESP32 station WPS enrollee success */ system_event_ap_staconnected_t sta_connected; /*!< a station connected to ESP32 soft-AP */ system_event_ap_stadisconnected_t sta_disconnected; /*!< a station disconnected to ESP32 soft-AP */ system_event_ap_probe_req_rx_t ap_probereqrecved; /*!< ESP32 soft-AP receive probe request packet */ diff --git a/components/esp_wifi/include/esp_wifi_types.h b/components/esp_wifi/include/esp_wifi_types.h index aa57e921b8..971eabbfb3 100644 --- a/components/esp_wifi/include/esp_wifi_types.h +++ b/components/esp_wifi/include/esp_wifi_types.h @@ -569,6 +569,19 @@ typedef enum { WPS_FAIL_REASON_MAX } wifi_event_sta_wps_fail_reason_t; +#define MAX_SSID_LEN 32 +#define MAX_PASSPHRASE_LEN 64 +#define MAX_WPS_AP_CRED 3 + +/** Argument structure for WIFI_EVENT_STA_WPS_ER_SUCCESS event */ +typedef struct { + uint8_t ap_cred_cnt; /**< Number of AP credentials received */ + struct { + uint8_t ssid[MAX_SSID_LEN]; /**< SSID of AP */ + uint8_t passphrase[MAX_PASSPHRASE_LEN]; /**< Passphrase for the AP */ + } ap_cred[MAX_WPS_AP_CRED]; /**< All AP credentials received from WPS handshake */ +} wifi_event_sta_wps_er_success_t; + /** Argument structure for WIFI_EVENT_AP_STACONNECTED event */ typedef struct { uint8_t mac[6]; /**< MAC address of the station connected to ESP32 soft-AP */ diff --git a/components/wpa_supplicant/CMakeLists.txt b/components/wpa_supplicant/CMakeLists.txt index 0772a36eaa..30570e7c5c 100644 --- a/components/wpa_supplicant/CMakeLists.txt +++ b/components/wpa_supplicant/CMakeLists.txt @@ -123,7 +123,10 @@ target_compile_definitions(${COMPONENT_LIB} PRIVATE CONFIG_ECC CONFIG_SHA256 CONFIG_IEEE80211W - CONFIG_WPA3_SAE ) +if(CONFIG_ESP32_WIFI_ENABLE_WPA3_SAE) + target_compile_definitions(${COMPONENT_LIB} PRIVATE CONFIG_WPA3_SAE) +endif() + set_property(TARGET ${COMPONENT_LIB} APPEND PROPERTY LINK_INTERFACE_MULTIPLICITY 3) diff --git a/components/wpa_supplicant/component.mk b/components/wpa_supplicant/component.mk index 29212dd64c..c616e49f93 100644 --- a/components/wpa_supplicant/component.mk +++ b/components/wpa_supplicant/component.mk @@ -26,4 +26,8 @@ else COMPONENT_OBJEXCLUDE := src/crypto/tls_mbedtls.o endif -CFLAGS += -DCONFIG_WPA3_SAE -DCONFIG_IEEE80211W -DESP_SUPPLICANT -DIEEE8021X_EAPOL -DEAP_PEER_METHOD -DEAP_TLS -DEAP_TTLS -DEAP_PEAP -DEAP_MSCHAPv2 -DUSE_WPA2_TASK -DCONFIG_WPS2 -DCONFIG_WPS_PIN -DUSE_WPS_TASK -DESPRESSIF_USE -DESP32_WORKAROUND -DCONFIG_ECC -D__ets__ -Wno-strict-aliasing +CFLAGS += -DCONFIG_IEEE80211W -DESP_SUPPLICANT -DIEEE8021X_EAPOL -DEAP_PEER_METHOD -DEAP_TLS -DEAP_TTLS -DEAP_PEAP -DEAP_MSCHAPv2 -DUSE_WPA2_TASK -DCONFIG_WPS2 -DCONFIG_WPS_PIN -DUSE_WPS_TASK -DESPRESSIF_USE -DESP32_WORKAROUND -DCONFIG_ECC -D__ets__ -Wno-strict-aliasing + +ifdef CONFIG_ESP32_WIFI_ENABLE_WPA3_SAE + CFLAGS += -DCONFIG_WPA3_SAE +endif diff --git a/components/wpa_supplicant/src/common/wpa_common.c b/components/wpa_supplicant/src/common/wpa_common.c index ba909fee26..a7532c47b9 100644 --- a/components/wpa_supplicant/src/common/wpa_common.c +++ b/components/wpa_supplicant/src/common/wpa_common.c @@ -58,11 +58,11 @@ static int rsn_key_mgmt_to_bitfield(const u8 *s) if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FT_PSK) return WPA_KEY_MGMT_FT_PSK; #endif /* CONFIG_IEEE80211R */ +#ifdef CONFIG_IEEE80211W #ifdef CONFIG_WPA3_SAE if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_SAE) return WPA_KEY_MGMT_SAE; #endif /* CONFIG_WPA3_SAE */ -#ifdef CONFIG_IEEE80211W if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_802_1X_SHA256) return WPA_KEY_MGMT_IEEE8021X_SHA256; if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_PSK_SHA256) @@ -396,10 +396,10 @@ int wpa_eapol_key_mic(const u8 *key, int ver, const u8 *buf, size_t len, #ifdef CONFIG_IEEE80211W #ifdef CONFIG_WPA3_SAE case WPA_KEY_INFO_TYPE_AKM_DEFINED: -#endif +#endif /* CONFIG_WPA3_SAE */ case WPA_KEY_INFO_TYPE_AES_128_CMAC: return omac1_aes_128(key, buf, len, mic); -#endif +#endif /* CONFIG_IEEE80211W */ default: return -1; } diff --git a/components/wpa_supplicant/src/esp_supplicant/esp_wpa3_i.h b/components/wpa_supplicant/src/esp_supplicant/esp_wpa3_i.h index 93223040e8..738df29181 100644 --- a/components/wpa_supplicant/src/esp_supplicant/esp_wpa3_i.h +++ b/components/wpa_supplicant/src/esp_supplicant/esp_wpa3_i.h @@ -31,5 +31,9 @@ static inline void esp_wifi_register_wpa3_cb(struct wpa_funcs *wpa_cb) wpa_cb->wpa3_parse_sae_msg = NULL; } +static inline void esp_wpa3_free_sae_data(void) +{ +} + #endif /* CONFIG_WPA3_SAE */ #endif /* ESP_WPA3_H */ diff --git a/components/wpa_supplicant/src/esp_supplicant/esp_wps.c b/components/wpa_supplicant/src/esp_supplicant/esp_wps.c index cbcd351973..9f71aeb98d 100644 --- a/components/wpa_supplicant/src/esp_supplicant/esp_wps.c +++ b/components/wpa_supplicant/src/esp_supplicant/esp_wps.c @@ -594,19 +594,16 @@ wps_parse_scan_result(struct wps_scan_ie *scan) } esp_wifi_enable_sta_privacy_internal(); - os_bzero(sm->ssid, sizeof(sm->ssid)); - strncpy((char *)sm->ssid, (char *)&scan->ssid[2], (int)scan->ssid[1]); - sm->ssid_len = scan->ssid[1]; + strncpy((char *)sm->config.ssid, (char *)&scan->ssid[2], (int)scan->ssid[1]); if (scan->bssid) { memcpy(gWpsSm->bssid, scan->bssid, ETH_ALEN); memcpy(sm->config.bssid, scan->bssid, ETH_ALEN); sm->config.bssid_set = 1; } else { } - wpa_printf(MSG_DEBUG, "wps discover [%s]", sm->ssid); + wpa_printf(MSG_DEBUG, "wps discover [%s]", (char *)sm->config.ssid); sm->scan_cnt = 0; - memcpy(sm->config.ssid, sm->ssid, sm->ssid_len); sm->channel = scan->chan; return true; @@ -944,9 +941,10 @@ int wps_stop_process(system_event_sta_wps_fail_reason_t reason_code) sm->discover_ssid_cnt = 0; sm->wps->state = SEND_M1; os_bzero(sm->bssid, ETH_ALEN); - os_bzero(sm->ssid, 32); - sm->ssid_len = 0; + os_bzero(sm->ssid, sizeof(sm->ssid)); + os_bzero(sm->ssid_len, sizeof(sm->ssid_len)); os_bzero((u8 *)&sm->config, sizeof(wifi_sta_config_t)); + sm->ap_cred_cnt = 0; esp_wifi_disarm_sta_connection_timer_internal(); ets_timer_disarm(&sm->wps_msg_timeout_timer); @@ -988,15 +986,17 @@ int wps_finish(void) ets_timer_disarm(&sm->wps_timeout_timer); ets_timer_disarm(&sm->wps_msg_timeout_timer); - memset(config, 0x00, sizeof(wifi_sta_config_t)); - memcpy(config->sta.ssid, sm->ssid, sm->ssid_len); - memcpy(config->sta.password, sm->key, sm->key_len); - memcpy(config->sta.bssid, sm->bssid, ETH_ALEN); - config->sta.bssid_set = 0; - esp_wifi_set_config(0, config); - os_free(config); - config = NULL; + if (sm->ap_cred_cnt == 1) { + memset(config, 0x00, sizeof(wifi_sta_config_t)); + memcpy(config->sta.ssid, sm->ssid[0], sm->ssid_len[0]); + memcpy(config->sta.password, sm->key[0], sm->key_len[0]); + memcpy(config->sta.bssid, sm->bssid, ETH_ALEN); + config->sta.bssid_set = 0; + esp_wifi_set_config(0, config); + os_free(config); + config = NULL; + } ets_timer_disarm(&sm->wps_success_cb_timer); ets_timer_arm(&sm->wps_success_cb_timer, 1000, 0); @@ -1505,7 +1505,25 @@ void wifi_station_wps_success_internal(void) { system_event_t evt; evt.event_id = SYSTEM_EVENT_STA_WPS_ER_SUCCESS; - esp_wifi_send_event_internal(&evt); + struct wps_sm *sm = gWpsSm; + int i; + + /* + * For only one AP credential don't sned event data, wps_finish() has already set + * the config. This is for backward compatibility. + */ + if (sm->ap_cred_cnt > 1) { + evt.event_info.sta_er_success.ap_cred_cnt = sm->ap_cred_cnt; + for (i = 0; i < MAX_WPS_AP_CRED; i++) { + os_memcpy(evt.event_info.sta_er_success.ap_cred[i].ssid, + sm->ssid[i], sm->ssid_len[i]); + os_memcpy(evt.event_info.sta_er_success.ap_cred[i].passphrase, + sm->key[i], sm->key_len[i]); + } + esp_wifi_send_event_internal(&evt); + } else { + esp_wifi_send_event_internal(&evt); + } } void wifi_station_wps_success(void) @@ -1716,44 +1734,45 @@ wps_sm_get(void) } int -wps_ssid_save(u8 *ssid, u8 ssid_len) +wps_ssid_save(u8 *ssid, u8 ssid_len, u8 idx) { u8 *tmpssid; - if (!ssid || !gWpsSm) { + if (!ssid || !gWpsSm || idx > 2) { return ESP_FAIL; } - memset(gWpsSm->ssid, 0x00, sizeof(gWpsSm->ssid)); - memcpy(gWpsSm->ssid, ssid, ssid_len); - gWpsSm->ssid_len = ssid_len; + memset(gWpsSm->ssid[idx], 0x00, sizeof(gWpsSm->ssid[idx])); + memcpy(gWpsSm->ssid[idx], ssid, ssid_len); + gWpsSm->ssid_len[idx] = ssid_len; + gWpsSm->ap_cred_cnt++; tmpssid = (u8 *)os_zalloc(ssid_len + 1); if (tmpssid) { memcpy(tmpssid, ssid, ssid_len); - wpa_printf(MSG_DEBUG, "WPS: ssid[%s]", tmpssid); + wpa_printf(MSG_DEBUG, "WPS: key[%s]", tmpssid); os_free(tmpssid); } return ESP_OK; } int -wps_key_save(char *key, u8 key_len) +wps_key_save(char *key, u8 key_len, u8 idx) { u8 *tmpkey; - if (!key || !gWpsSm) { + if (!key || !gWpsSm || idx > 2) { return ESP_FAIL; } - memset(gWpsSm->key, 0x00, sizeof(gWpsSm->key)); - memcpy(gWpsSm->key, key, key_len); - gWpsSm->key_len = key_len; + memset(gWpsSm->key[idx], 0x00, sizeof(gWpsSm->key[idx])); + memcpy(gWpsSm->key[idx], key, key_len); + gWpsSm->key_len[idx] = key_len; tmpkey = (u8 *)os_zalloc(key_len + 1); if (tmpkey) { memcpy(tmpkey, key, key_len); - wpa_printf(MSG_DEBUG, "WPS: key[%s]", tmpkey); + wpa_printf(MSG_DEBUG, "WPS: key[%s], idx - %d", tmpkey, idx); os_free(tmpkey); } return ESP_OK; diff --git a/components/wpa_supplicant/src/rsn_supp/wpa_ie.c b/components/wpa_supplicant/src/rsn_supp/wpa_ie.c index d648a45429..497d50ec7b 100644 --- a/components/wpa_supplicant/src/rsn_supp/wpa_ie.c +++ b/components/wpa_supplicant/src/rsn_supp/wpa_ie.c @@ -203,11 +203,11 @@ static int wpa_gen_wpa_ie_rsn(u8 *rsn_ie, size_t rsn_ie_len, RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_802_1X_SHA256); } else if (key_mgmt == WPA_KEY_MGMT_PSK_SHA256) { RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_PSK_SHA256); -#endif /* CONFIG_IEEE80211W */ #ifdef CONFIG_WPA3_SAE } else if (key_mgmt == WPA_KEY_MGMT_SAE) { RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_SAE); #endif /* CONFIG_WPA3_SAE */ +#endif /* CONFIG_IEEE80211W */ } else { wpa_printf(MSG_DEBUG, "Invalid key management type (%d).", key_mgmt); diff --git a/components/wpa_supplicant/src/wps/wps.h b/components/wpa_supplicant/src/wps/wps.h index ab2eb00bda..dd7245b7c0 100644 --- a/components/wpa_supplicant/src/wps/wps.h +++ b/components/wpa_supplicant/src/wps/wps.h @@ -1027,13 +1027,14 @@ struct wps_sm { u8 identity_len; u8 ownaddr[ETH_ALEN]; u8 bssid[ETH_ALEN]; - u8 ssid[32]; - u8 ssid_len; + u8 ssid[MAX_WPS_AP_CRED][MAX_SSID_LEN]; + u8 ssid_len[MAX_WPS_AP_CRED]; + char key[MAX_WPS_AP_CRED][MAX_PASSPHRASE_LEN]; + u8 key_len[MAX_WPS_AP_CRED]; + u8 ap_cred_cnt; struct wps_device_data *dev; u8 uuid[16]; u8 eapol_version; - char key[64]; - u8 key_len; ETSTimer wps_timeout_timer; ETSTimer wps_msg_timeout_timer; ETSTimer wps_scan_timer; @@ -1057,8 +1058,8 @@ struct wps_sm { #define WIFI_CAPINFO_PRIVACY 0x0010 struct wps_sm *wps_sm_get(void); -int wps_ssid_save(u8 *ssid, u8 ssid_len); -int wps_key_save(char *key, u8 key_len); +int wps_ssid_save(u8 *ssid, u8 ssid_len, u8 idx); +int wps_key_save(char *key, u8 key_len, u8 idx); int wps_station_wps_register_cb(wps_st_cb_t cb); int wps_station_wps_unregister_cb(void); int wps_start_pending(void); diff --git a/components/wpa_supplicant/src/wps/wps_enrollee.c b/components/wpa_supplicant/src/wps/wps_enrollee.c index 7a30836577..77c7580202 100644 --- a/components/wpa_supplicant/src/wps/wps_enrollee.c +++ b/components/wpa_supplicant/src/wps/wps_enrollee.c @@ -651,7 +651,7 @@ static int wps_process_r_snonce2(struct wps_data *wps, const u8 *r_snonce2) static int wps_process_cred_e(struct wps_data *wps, const u8 *cred, - size_t cred_len, int wps2) + size_t cred_len, int cred_idx, int wps2) { struct wps_parse_attr *attr; struct wpabuf msg; @@ -711,9 +711,8 @@ static int wps_process_cred_e(struct wps_data *wps, const u8 *cred, goto _out; } #endif /* CONFIG_WPS2 */ - - wps_ssid_save(wps->cred.ssid, wps->cred.ssid_len); - wps_key_save((char *)wps->cred.key, wps->cred.key_len); + wps_ssid_save(wps->cred.ssid, wps->cred.ssid_len, cred_idx); + wps_key_save((char *)wps->cred.key, wps->cred.key_len, cred_idx); if (wps->wps->cred_cb) { wps->cred.cred_attr = cred - 4; @@ -748,7 +747,7 @@ static int wps_process_creds(struct wps_data *wps, const u8 *cred[], for (i = 0; i < num_cred; i++) { int res; - res = wps_process_cred_e(wps, cred[i], cred_len[i], wps2); + res = wps_process_cred_e(wps, cred[i], cred_len[i], i, wps2); if (res == 0) ok++; else if (res == -2) { diff --git a/examples/wifi/wps/main/wps.c b/examples/wifi/wps/main/wps.c index 5ac103b7f1..ed99100fde 100644 --- a/examples/wifi/wps/main/wps.c +++ b/examples/wifi/wps/main/wps.c @@ -27,6 +27,7 @@ #include "esp_wps.h" #include "esp_event.h" #include "nvs_flash.h" +#include /*set wps mode via project configuration */ @@ -38,6 +39,7 @@ #define WPS_MODE WPS_TYPE_DISABLE #endif /*CONFIG_EXAMPLE_WPS_TYPE_PBC*/ +#define MAX_RETRY_ATTEMPTS 2 #ifndef PIN2STR #define PIN2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5], (a)[6], (a)[7] @@ -46,23 +48,67 @@ static const char *TAG = "example_wps"; static esp_wps_config_t config = WPS_CONFIG_INIT_DEFAULT(WPS_MODE); +static wifi_config_t wps_ap_creds[MAX_WPS_AP_CRED]; +static int s_ap_creds_num = 0; +static int s_retry_num = 0; static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { + static int ap_idx = 1; + switch (event_id) { case WIFI_EVENT_STA_START: ESP_LOGI(TAG, "WIFI_EVENT_STA_START"); break; case WIFI_EVENT_STA_DISCONNECTED: ESP_LOGI(TAG, "WIFI_EVENT_STA_DISCONNECTED"); - ESP_ERROR_CHECK(esp_wifi_connect()); + if (s_retry_num < MAX_RETRY_ATTEMPTS) { + ESP_ERROR_CHECK(esp_wifi_connect()); + s_retry_num++; + } else if (ap_idx < s_ap_creds_num) { + /* Try the next AP credential if first one fails */ + + if (ap_idx < s_ap_creds_num) { + ESP_LOGI(TAG, "Connecting to SSID: %s, Passphrase: %s", + wps_ap_creds[ap_idx].sta.ssid, wps_ap_creds[ap_idx].sta.password); + ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wps_ap_creds[ap_idx++]) ); + ESP_ERROR_CHECK(esp_wifi_connect()); + } + s_retry_num = 0; + } else { + ESP_LOGI(TAG, "Failed to connect!"); + } + break; case WIFI_EVENT_STA_WPS_ER_SUCCESS: ESP_LOGI(TAG, "WIFI_EVENT_STA_WPS_ER_SUCCESS"); - /* esp_wifi_wps_start() only gets ssid & password, so call esp_wifi_connect() here. */ - ESP_ERROR_CHECK(esp_wifi_wps_disable()); - ESP_ERROR_CHECK(esp_wifi_connect()); + { + wifi_event_sta_wps_er_success_t *evt = + (wifi_event_sta_wps_er_success_t *)event_data; + int i; + + if (evt) { + s_ap_creds_num = evt->ap_cred_cnt; + for (i = 0; i < s_ap_creds_num; i++) { + memcpy(wps_ap_creds[i].sta.ssid, evt->ap_cred[i].ssid, + sizeof(evt->ap_cred[i].ssid)); + memcpy(wps_ap_creds[i].sta.password, evt->ap_cred[i].passphrase, + sizeof(evt->ap_cred[i].passphrase)); + } + /* If multiple AP credentials are received from WPS, connect with first one */ + ESP_LOGI(TAG, "Connecting to SSID: %s, Passphrase: %s", + wps_ap_creds[0].sta.ssid, wps_ap_creds[0].sta.password); + ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wps_ap_creds[0]) ); + } + /* + * If only one AP credential is received from WPS, there will be no event data and + * esp_wifi_set_config() is already called by WPS modules for backward compatibility + * with legacy apps. So directly attempt connection here. + */ + ESP_ERROR_CHECK(esp_wifi_wps_disable()); + ESP_ERROR_CHECK(esp_wifi_connect()); + } break; case WIFI_EVENT_STA_WPS_ER_FAILED: ESP_LOGI(TAG, "WIFI_EVENT_STA_WPS_ER_FAILED"); From 54c012905fbf6e248a1ab2f0ad8a75ca170147ff Mon Sep 17 00:00:00 2001 From: aditi_lonkar Date: Tue, 28 Jul 2020 17:57:55 +0530 Subject: [PATCH 15/16] wifi: Fix esp_wifi log levels --- components/esp_wifi/include/esp_private/wifi.h | 3 ++- components/esp_wifi/src/wifi_init.c | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/components/esp_wifi/include/esp_private/wifi.h b/components/esp_wifi/include/esp_private/wifi.h index 4e995b679a..359f49d419 100644 --- a/components/esp_wifi/include/esp_private/wifi.h +++ b/components/esp_wifi/include/esp_private/wifi.h @@ -53,7 +53,8 @@ typedef struct { * */ typedef enum { - WIFI_LOG_ERROR = 0, /*enabled by default*/ + WIFI_LOG_NONE = 0, + WIFI_LOG_ERROR , /*enabled by default*/ WIFI_LOG_WARNING, /*enabled by default*/ WIFI_LOG_INFO, /*enabled by default*/ WIFI_LOG_DEBUG, /*can be set in menuconfig*/ diff --git a/components/esp_wifi/src/wifi_init.c b/components/esp_wifi/src/wifi_init.c index db4c5d2efb..29ceadb8df 100644 --- a/components/esp_wifi/src/wifi_init.c +++ b/components/esp_wifi/src/wifi_init.c @@ -53,8 +53,10 @@ static void __attribute__((constructor)) s_set_default_wifi_log_level() so set it at runtime startup. Done here not in esp_wifi_init() to allow the user to set the level again before esp_wifi_init() is called. */ - esp_log_level_set("wifi", CONFIG_LOG_DEFAULT_LEVEL); + esp_log_level_set("wifi", CONFIG_LOG_DEFAULT_LEVEL); esp_log_level_set("mesh", CONFIG_LOG_DEFAULT_LEVEL); + esp_log_level_set("smartconfig", CONFIG_LOG_DEFAULT_LEVEL); + esp_log_level_set("ESPNOW", CONFIG_LOG_DEFAULT_LEVEL); } static void esp_wifi_set_debug_log() From 160e43a95bb37f6fc5d1913593ce03520cfa3b67 Mon Sep 17 00:00:00 2001 From: zhangyanjiao Date: Tue, 1 Dec 2020 20:10:35 +0800 Subject: [PATCH 16/16] esp_wifi: update wifi lib for bugfixs --- components/esp_wifi/lib_esp32 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/esp_wifi/lib_esp32 b/components/esp_wifi/lib_esp32 index afbe070254..c4b9dd09a2 160000 --- a/components/esp_wifi/lib_esp32 +++ b/components/esp_wifi/lib_esp32 @@ -1 +1 @@ -Subproject commit afbe070254ced57b6dae169aa46405457fcb362a +Subproject commit c4b9dd09a274a0955589f68f556bb8069abf1682