Skip to content

Commit

Permalink
modules: hostap: Check for band and channel compatibility
Browse files Browse the repository at this point in the history
If a user provides both band and channel then check if they are
compatible or not.

Also, move the multiple remove network cleanup to goto.

Signed-off-by: Chaitanya Tata <[email protected]>
  • Loading branch information
krish2718 authored and nordicjm committed Nov 7, 2023
1 parent 3444f81 commit 679e9b1
Showing 1 changed file with 42 additions and 9 deletions.
51 changes: 42 additions & 9 deletions modules/hostap/src/supp_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,28 @@ static int wpa_supp_supported_channels(struct wpa_supplicant *wpa_s, uint8_t ban
return 0;
}

static int wpa_supp_band_chan_compat(struct wpa_supplicant *wpa_s, uint8_t band, uint8_t channel)
{
struct hostapd_hw_modes *mode = NULL;
int i;

mode = get_mode_by_band(wpa_s, band);
if (!mode) {
wpa_printf(MSG_ERROR, "Unsupported or invalid band: %d", band);
return -EINVAL;
}

for (i = 0; i < mode->num_channels; i++) {
if (mode->channels[i].freq == channel) {
return mode->channels[i].freq;
}
}

wpa_printf(MSG_ERROR, "Channel %d not supported for band %d", channel, band);

return -EINVAL;
}

static inline void wpa_supp_restart_status_work(void)
{
/* Terminate synchronously */
Expand Down Expand Up @@ -296,8 +318,7 @@ int z_wpa_supplicant_connect(const struct device *dev,
if (params->band != WIFI_FREQ_BAND_UNKNOWN) {
ret = wpa_supp_supported_channels(wpa_s, params->band, &chan_list);
if (ret < 0) {
_wpa_cli_cmd_v("remove_network %d", resp.network_id);
goto out;
goto rem_net;
}

if (chan_list) {
Expand Down Expand Up @@ -331,7 +352,7 @@ int z_wpa_supplicant_connect(const struct device *dev,
ret = -1;
wpa_printf(MSG_ERROR, "Unsupported security type: %d",
params->security);
goto out;
goto rem_net;
}

if (params->mfp) {
Expand All @@ -344,13 +365,21 @@ int z_wpa_supplicant_connect(const struct device *dev,
_wpa_cli_cmd_v("enable_network %d", resp.network_id);

if (params->channel != WIFI_CHANNEL_ANY) {
int freq = chan_to_freq(params->channel);
int freq;

if (freq < 0) {
ret = -1;
wpa_printf(MSG_ERROR, "Invalid channel %d",
params->channel);
goto out;
if (params->band != WIFI_FREQ_BAND_UNKNOWN) {
freq = wpa_supp_band_chan_compat(wpa_s, params->band, params->channel);
if (freq < 0) {
goto rem_net;
}
} else {
freq = chan_to_freq(params->channel);
if (freq < 0) {
ret = -1;
wpa_printf(MSG_ERROR, "Invalid channel %d",
params->channel);
goto rem_net;
}
}
_wpa_cli_cmd_v("set_network %d scan_freq %d",
resp.network_id, freq);
Expand All @@ -362,6 +391,10 @@ int z_wpa_supplicant_connect(const struct device *dev,
wpa_supp_api_ctrl.requested_op = CONNECT;
wpa_supp_api_ctrl.connection_timeout = params->timeout;

goto out;

rem_net:
_wpa_cli_cmd_v("remove_network %d", resp.network_id);
out:
k_mutex_unlock(&wpa_supplicant_mutex);

Expand Down

0 comments on commit 679e9b1

Please sign in to comment.