Skip to content

net: wifi: fix the bug by non-wifi network call #90371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2025

Conversation

LeonCT
Copy link
Contributor

@LeonCT LeonCT commented May 23, 2025

  1. Description of the scenario where the problem occurs:
  • When a non-wifi network card device is used in the code to call the wifi api in mgmt mode, the system will crash.
  1. Root cause:
  • In the get_wifi_api function, there is no check to see if the network card type is wifi, which will cause other types of network cards (such as Ethernet devices) to successfully assign the dev api element to the final return value of the function. This will cause a direct crash when other functions call the returned wifi api. The following are the differences between the apis of the two network card device driver layers:
    wifi(eg.drivers\wifi\esp32\src\esp_wifi_drv.c):
    static const struct wifi_mgmt_ops esp32_wifi_mgmt = {
    .scan = esp32_wifi_scan,
    .connect = esp32_wifi_connect,
    .disconnect = esp32_wifi_disconnect,
    .ap_enable = esp32_wifi_ap_enable,
    .ap_disable = esp32_wifi_ap_disable,
    .iface_status = esp32_wifi_status,
    #if defined(CONFIG_NET_STATISTICS_WIFI)
    .get_stats = esp32_wifi_stats,
    #endif
    };

    ethernet(eg.drivers\ethernet\eth_lan865x.c)
    static const struct ethernet_api lan865x_api_func = {
    .iface_api.init = lan865x_iface_init,
    .get_capabilities = lan865x_port_get_capabilities,
    .set_config = lan865x_set_config,
    .send = lan865x_port_send,
    };

    Obviously, the APIs of the above two drivers are different, but after being called by the macro NET_DEVICE_DT_INST_DEFINE, they will be assigned to the api element of the device dev.

3.Device verification:

  • Based on the NXP frdm-rw612 development board, write a WiFi connection example program. Since the development board has both Ethernet and wireless network cards, two network card devices will be enumerated when the system starts. When the function net_if_get_default() is used to obtain the default network card, the obtained network card device is Ethernet, but if the function net_mgmt is used to initiate a WiFi connection at this time, it will directly cause the system to crash. After using the repair code, it will return the call error code instead of the system crash.

  • application demo code for wifi connect:

      static void connect_to_wifi(void) {
          struct net_if *iface = net_if_get_default();
          struct wifi_connect_req_params cnx_params = {
              .ssid = WIFI_SSID,
              .ssid_length = strlen(WIFI_SSID),
              .psk = WIFI_PASSWORD,
              .psk_length = strlen(WIFI_PASSWORD),
              .channel = WIFI_CHANNEL_ANY,
              .security = WIFI_SECURITY_TYPE_PSK,
          };
          LOG_INF("%d", __LINE__);
          net_mgmt_init_event_callback(&wifi_cb, wifi_event_handler,
                                       NET_EVENT_WIFI_CONNECT_RESULT |
                                       NET_EVENT_WIFI_DISCONNECT_RESULT);
          LOG_INF("%d", __LINE__);
          net_mgmt_add_event_callback(&wifi_cb);
          LOG_INF("%d", __LINE__);
          int ret = net_mgmt(NET_REQUEST_WIFI_CONNECT, iface,
                             &cnx_params, sizeof(struct wifi_connect_req_params));
          LOG_INF("%d", __LINE__);
          if (ret) {
              LOG_ERR("Wi-Fi connect failed: %d", ret);
          } else {
              LOG_INF("Connecting to Wi-Fi...");
          }
      }
    

-log print output
Original code run:
image

Fixed code run:

image

Copy link

Hello @LeonCT, and thank you very much for your first pull request to the Zephyr project!
Our Continuous Integration pipeline will execute a series of checks on your Pull Request commit messages and code, and you are expected to address any failures by updating the PR. Please take a look at our commit message guidelines to find out how to format your commit messages, and at our contribution workflow to understand how to update your Pull Request. If you haven't already, please make sure to review the project's Contributor Expectations and update (by amending and force-pushing the commits) your pull request if necessary.
If you are stuck or need help please join us on Discord and ask your question there. Additionally, you can escalate the review when applicable. 😊

@LeonCT LeonCT marked this pull request as ready for review May 23, 2025 05:53
@github-actions github-actions bot added area: Wi-Fi Wi-Fi size: XS A PR changing only a single line of code area: Networking labels May 23, 2025
MaochenWang1
MaochenWang1 previously approved these changes May 23, 2025
@LeonCT LeonCT force-pushed the fix_wifi_net_bug branch 2 times, most recently from 3168f44 to ad83c0c Compare May 24, 2025 02:35
@LeonCT LeonCT force-pushed the fix_wifi_net_bug branch from ad83c0c to 9d3992b Compare May 24, 2025 10:48
Added network type check to fix the system crash caused by non-wifi
network card calling wifi api interface

Company:BSH Crop
Signed-off-by: Chen Tao <[email protected]>
@LeonCT LeonCT force-pushed the fix_wifi_net_bug branch from 9d3992b to 63581bb Compare May 24, 2025 11:05
Copy link

@LeonCT LeonCT requested review from MaochenWang1 and jukkar May 26, 2025 01:57
@kartben kartben merged commit b513e08 into zephyrproject-rtos:main May 27, 2025
27 checks passed
Copy link

Hi @LeonCT!
Congratulations on getting your very first Zephyr pull request merged 🎉🥳. This is a fantastic achievement, and we're thrilled to have you as part of our community!

To celebrate this milestone and showcase your contribution, we'd love to award you the Zephyr Technical Contributor badge. If you're interested, please claim your badge by filling out this form: Claim Your Zephyr Badge.

Thank you for your valuable input, and we look forward to seeing more of your contributions in the future! 🪁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Networking area: Wi-Fi Wi-Fi size: XS A PR changing only a single line of code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants