blob: cef38173efa2b2f1412b44bfc83336cc8be3f80b [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd / Hardware feature query and different modes
3 * Copyright 2002-2003, Instant802 Networks, Inc.
4 * Copyright 2005-2006, Devicescape Software, Inc.
Dmitry Shmidt04949592012-07-19 12:16:46 -07005 * Copyright (c) 2008-2012, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006 *
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007 * This software may be distributed under the terms of the BSD license.
8 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009 */
10
11#include "utils/includes.h"
12
13#include "utils/common.h"
14#include "utils/eloop.h"
15#include "common/ieee802_11_defs.h"
16#include "common/ieee802_11_common.h"
Dmitry Shmidtcce06662013-11-04 18:44:24 -080017#include "common/wpa_ctrl.h"
Dmitry Shmidtff787d52015-01-12 13:01:47 -080018#include "common/hw_features_common.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070019#include "hostapd.h"
20#include "ap_config.h"
21#include "ap_drv_ops.h"
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070022#include "acs.h"
Dmitry Shmidt7832adb2014-04-29 10:53:02 -070023#include "ieee802_11.h"
24#include "beacon.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070025#include "hw_features.h"
26
27
28void hostapd_free_hw_features(struct hostapd_hw_modes *hw_features,
29 size_t num_hw_features)
30{
31 size_t i;
32
33 if (hw_features == NULL)
34 return;
35
36 for (i = 0; i < num_hw_features; i++) {
37 os_free(hw_features[i].channels);
38 os_free(hw_features[i].rates);
39 }
40
41 os_free(hw_features);
42}
43
44
Dmitry Shmidt051af732013-10-22 13:52:46 -070045#ifndef CONFIG_NO_STDOUT_DEBUG
46static char * dfs_info(struct hostapd_channel_data *chan)
47{
48 static char info[256];
49 char *state;
50
51 switch (chan->flag & HOSTAPD_CHAN_DFS_MASK) {
52 case HOSTAPD_CHAN_DFS_UNKNOWN:
53 state = "unknown";
54 break;
55 case HOSTAPD_CHAN_DFS_USABLE:
56 state = "usable";
57 break;
58 case HOSTAPD_CHAN_DFS_UNAVAILABLE:
59 state = "unavailable";
60 break;
61 case HOSTAPD_CHAN_DFS_AVAILABLE:
62 state = "available";
63 break;
64 default:
65 return "";
66 }
67 os_snprintf(info, sizeof(info), " (DFS state = %s)", state);
68 info[sizeof(info) - 1] = '\0';
69
70 return info;
71}
72#endif /* CONFIG_NO_STDOUT_DEBUG */
73
74
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070075int hostapd_get_hw_features(struct hostapd_iface *iface)
76{
77 struct hostapd_data *hapd = iface->bss[0];
Dmitry Shmidt2f74e362015-01-21 13:19:05 -080078 int i, j;
Sunil Ravic0f5d412024-09-11 22:12:49 +000079 unsigned int k;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070080 u16 num_modes, flags;
81 struct hostapd_hw_modes *modes;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070082 u8 dfs_domain;
Sunil Ravi2a14cf12023-11-21 00:54:38 +000083 enum hostapd_hw_mode mode = HOSTAPD_MODE_IEEE80211ANY;
84 bool is_6ghz = false;
85 bool orig_mode_valid = false;
Sunil Ravic0f5d412024-09-11 22:12:49 +000086 struct hostapd_multi_hw_info *multi_hw_info;
87 unsigned int num_multi_hws;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070088
89 if (hostapd_drv_none(hapd))
90 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070091 modes = hostapd_get_hw_feature_data(hapd, &num_modes, &flags,
92 &dfs_domain);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070093 if (modes == NULL) {
94 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
95 HOSTAPD_LEVEL_DEBUG,
96 "Fetching hardware channel/rate support not "
97 "supported.");
98 return -1;
99 }
100
101 iface->hw_flags = flags;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700102 iface->dfs_domain = dfs_domain;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700103
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000104 if (iface->current_mode) {
105 /*
106 * Received driver event CHANNEL_LIST_CHANGED when the current
107 * hw mode is valid. Clear iface->current_mode temporarily as
108 * the mode instance will be replaced with a new instance and
109 * the current pointer would be pointing to freed memory.
110 */
111 orig_mode_valid = true;
112 mode = iface->current_mode->mode;
Sunil Ravi99c035e2024-07-12 01:42:03 +0000113 is_6ghz = iface->current_mode->is_6ghz;
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000114 iface->current_mode = NULL;
115 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700116 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
117 iface->hw_features = modes;
118 iface->num_hw_features = num_modes;
119
120 for (i = 0; i < num_modes; i++) {
121 struct hostapd_hw_modes *feature = &modes[i];
Dmitry Shmidt051af732013-10-22 13:52:46 -0700122 int dfs_enabled = hapd->iconf->ieee80211h &&
123 (iface->drv_flags & WPA_DRIVER_FLAGS_RADAR);
124
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000125 /* Restore orignal mode if possible */
126 if (orig_mode_valid && feature->mode == mode &&
127 feature->num_channels > 0 &&
128 is_6ghz == is_6ghz_freq(feature->channels[0].freq))
129 iface->current_mode = feature;
130
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700131 /* set flag for channels we can use in current regulatory
132 * domain */
133 for (j = 0; j < feature->num_channels; j++) {
Dmitry Shmidt051af732013-10-22 13:52:46 -0700134 int dfs = 0;
135
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700136 /*
137 * Disable all channels that are marked not to allow
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800138 * to initiate radiation (a.k.a. passive scan and no
139 * IBSS).
Dmitry Shmidt051af732013-10-22 13:52:46 -0700140 * Use radar channels only if the driver supports DFS.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700141 */
Dmitry Shmidt051af732013-10-22 13:52:46 -0700142 if ((feature->channels[j].flag &
143 HOSTAPD_CHAN_RADAR) && dfs_enabled) {
144 dfs = 1;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700145 } else if (((feature->channels[j].flag &
146 HOSTAPD_CHAN_RADAR) &&
147 !(iface->drv_flags &
148 WPA_DRIVER_FLAGS_DFS_OFFLOAD)) ||
149 (feature->channels[j].flag &
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800150 HOSTAPD_CHAN_NO_IR)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700151 feature->channels[j].flag |=
152 HOSTAPD_CHAN_DISABLED;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700153 }
154
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700155 if (feature->channels[j].flag & HOSTAPD_CHAN_DISABLED)
156 continue;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700157
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700158 wpa_printf(MSG_MSGDUMP, "Allowed channel: mode=%d "
Dmitry Shmidt051af732013-10-22 13:52:46 -0700159 "chan=%d freq=%d MHz max_tx_power=%d dBm%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700160 feature->mode,
161 feature->channels[j].chan,
162 feature->channels[j].freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700163 feature->channels[j].max_tx_power,
164 dfs ? dfs_info(&feature->channels[j]) : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700165 }
166 }
167
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000168 if (orig_mode_valid && !iface->current_mode) {
169 wpa_printf(MSG_ERROR,
170 "%s: Could not update iface->current_mode",
171 __func__);
172 }
173
Sunil Ravic0f5d412024-09-11 22:12:49 +0000174 multi_hw_info = hostapd_get_multi_hw_info(hapd, &num_multi_hws);
175 if (!multi_hw_info)
176 return 0;
177
178 hostapd_free_multi_hw_info(iface->multi_hw_info);
179 iface->multi_hw_info = multi_hw_info;
180 iface->num_multi_hws = num_multi_hws;
181
182 wpa_printf(MSG_DEBUG, "Multiple underlying hardwares info:");
183
184 for (k = 0; k < num_multi_hws; k++) {
185 struct hostapd_multi_hw_info *hw_info = &multi_hw_info[k];
186
187 wpa_printf(MSG_DEBUG,
188 " %d. hw_idx=%u, frequency range: %d-%d MHz",
189 k + 1, hw_info->hw_idx, hw_info->start_freq,
190 hw_info->end_freq);
191 }
192
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800193 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700194}
195
196
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800197int hostapd_prepare_rates(struct hostapd_iface *iface,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700198 struct hostapd_hw_modes *mode)
199{
200 int i, num_basic_rates = 0;
201 int basic_rates_a[] = { 60, 120, 240, -1 };
202 int basic_rates_b[] = { 10, 20, -1 };
203 int basic_rates_g[] = { 10, 20, 55, 110, -1 };
204 int *basic_rates;
205
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800206 if (iface->conf->basic_rates)
207 basic_rates = iface->conf->basic_rates;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700208 else switch (mode->mode) {
209 case HOSTAPD_MODE_IEEE80211A:
210 basic_rates = basic_rates_a;
211 break;
212 case HOSTAPD_MODE_IEEE80211B:
213 basic_rates = basic_rates_b;
214 break;
215 case HOSTAPD_MODE_IEEE80211G:
216 basic_rates = basic_rates_g;
217 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800218 case HOSTAPD_MODE_IEEE80211AD:
219 return 0; /* No basic rates for 11ad */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700220 default:
221 return -1;
222 }
223
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800224 i = 0;
225 while (basic_rates[i] >= 0)
226 i++;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700227 if (i)
228 i++; /* -1 termination */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800229 os_free(iface->basic_rates);
230 iface->basic_rates = os_malloc(i * sizeof(int));
231 if (iface->basic_rates)
232 os_memcpy(iface->basic_rates, basic_rates, i * sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700233
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800234 os_free(iface->current_rates);
235 iface->num_rates = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700236
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800237 iface->current_rates =
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700238 os_calloc(mode->num_rates, sizeof(struct hostapd_rate_data));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800239 if (!iface->current_rates) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700240 wpa_printf(MSG_ERROR, "Failed to allocate memory for rate "
241 "table.");
242 return -1;
243 }
244
245 for (i = 0; i < mode->num_rates; i++) {
246 struct hostapd_rate_data *rate;
247
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800248 if (iface->conf->supported_rates &&
249 !hostapd_rate_found(iface->conf->supported_rates,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700250 mode->rates[i]))
251 continue;
252
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800253 rate = &iface->current_rates[iface->num_rates];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700254 rate->rate = mode->rates[i];
255 if (hostapd_rate_found(basic_rates, rate->rate)) {
256 rate->flags |= HOSTAPD_RATE_BASIC;
257 num_basic_rates++;
258 }
259 wpa_printf(MSG_DEBUG, "RATE[%d] rate=%d flags=0x%x",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800260 iface->num_rates, rate->rate, rate->flags);
261 iface->num_rates++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700262 }
263
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800264 if ((iface->num_rates == 0 || num_basic_rates == 0) &&
265 (!iface->conf->ieee80211n || !iface->conf->require_ht)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700266 wpa_printf(MSG_ERROR, "No rates remaining in supported/basic "
267 "rate sets (%d,%d).",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800268 iface->num_rates, num_basic_rates);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700269 return -1;
270 }
271
272 return 0;
273}
274
275
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700276static int ieee80211n_allowed_ht40_channel_pair(struct hostapd_iface *iface)
277{
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800278 int pri_freq, sec_freq;
279 struct hostapd_channel_data *p_chan, *s_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700280
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800281 pri_freq = iface->freq;
282 sec_freq = pri_freq + iface->conf->secondary_channel * 20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700283
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800284 if (!iface->current_mode)
285 return 0;
286
287 p_chan = hw_get_channel_freq(iface->current_mode->mode, pri_freq, NULL,
288 iface->hw_features,
289 iface->num_hw_features);
290
291 s_chan = hw_get_channel_freq(iface->current_mode->mode, sec_freq, NULL,
292 iface->hw_features,
293 iface->num_hw_features);
294
295 return allowed_ht40_channel_pair(iface->current_mode->mode,
296 p_chan, s_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700297}
298
299
300static void ieee80211n_switch_pri_sec(struct hostapd_iface *iface)
301{
302 if (iface->conf->secondary_channel > 0) {
303 iface->conf->channel += 4;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800304 iface->freq += 20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700305 iface->conf->secondary_channel = -1;
306 } else {
307 iface->conf->channel -= 4;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800308 iface->freq -= 20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700309 iface->conf->secondary_channel = 1;
310 }
311}
312
313
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700314static int ieee80211n_check_40mhz_5g(struct hostapd_iface *iface,
315 struct wpa_scan_results *scan_res)
316{
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800317 unsigned int pri_freq, sec_freq;
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800318 int res;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800319 struct hostapd_channel_data *pri_chan, *sec_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700320
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800321 pri_freq = iface->freq;
322 sec_freq = pri_freq + iface->conf->secondary_channel * 20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700323
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800324 if (!iface->current_mode)
325 return 0;
326 pri_chan = hw_get_channel_freq(iface->current_mode->mode, pri_freq,
327 NULL, iface->hw_features,
328 iface->num_hw_features);
329 sec_chan = hw_get_channel_freq(iface->current_mode->mode, sec_freq,
330 NULL, iface->hw_features,
331 iface->num_hw_features);
332
333 res = check_40mhz_5g(scan_res, pri_chan, sec_chan);
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800334
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800335 if (res == 2) {
336 if (iface->conf->no_pri_sec_switch) {
337 wpa_printf(MSG_DEBUG,
338 "Cannot switch PRI/SEC channels due to local constraint");
339 } else {
340 ieee80211n_switch_pri_sec(iface);
341 }
342 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700343
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800344 return !!res;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -0700345}
346
347
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700348static int ieee80211n_check_40mhz_2g4(struct hostapd_iface *iface,
349 struct wpa_scan_results *scan_res)
350{
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800351 int pri_chan, sec_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700352
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800353 pri_chan = iface->conf->channel;
354 sec_chan = pri_chan + iface->conf->secondary_channel * 4;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700355
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800356 return check_40mhz_2g4(iface->current_mode, scan_res, pri_chan,
357 sec_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700358}
359
360
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700361static void ieee80211n_check_scan(struct hostapd_iface *iface)
362{
363 struct wpa_scan_results *scan_res;
364 int oper40;
Hai Shalom60840252021-02-19 19:02:11 -0800365 int res = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700366
367 /* Check list of neighboring BSSes (from scan) to see whether 40 MHz is
Dmitry Shmidt04949592012-07-19 12:16:46 -0700368 * allowed per IEEE Std 802.11-2012, 10.15.3.2 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700369
370 iface->scan_cb = NULL;
371
372 scan_res = hostapd_driver_get_scan_results(iface->bss[0]);
373 if (scan_res == NULL) {
374 hostapd_setup_interface_complete(iface, 1);
375 return;
376 }
377
378 if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A)
379 oper40 = ieee80211n_check_40mhz_5g(iface, scan_res);
380 else
381 oper40 = ieee80211n_check_40mhz_2g4(iface, scan_res);
382 wpa_scan_results_free(scan_res);
383
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700384 iface->secondary_ch = iface->conf->secondary_channel;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700385 if (!oper40) {
386 wpa_printf(MSG_INFO, "20/40 MHz operation not permitted on "
387 "channel pri=%d sec=%d based on overlapping BSSes",
388 iface->conf->channel,
389 iface->conf->channel +
390 iface->conf->secondary_channel * 4);
391 iface->conf->secondary_channel = 0;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700392 if (iface->drv_flags & WPA_DRIVER_FLAGS_HT_2040_COEX) {
393 /*
394 * TODO: Could consider scheduling another scan to check
395 * if channel width can be changed if no coex reports
396 * are received from associating stations.
397 */
398 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700399 }
400
Hai Shalom60840252021-02-19 19:02:11 -0800401#ifdef CONFIG_IEEE80211AX
402 if (iface->conf->secondary_channel &&
403 iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
404 iface->conf->ieee80211ax) {
405 struct he_capabilities *he_cap;
406
407 he_cap = &iface->current_mode->he_capab[IEEE80211_MODE_AP];
408 if (!(he_cap->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
409 HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_IN_2G)) {
410 wpa_printf(MSG_DEBUG,
411 "HE: 40 MHz channel width is not supported in 2.4 GHz; clear secondary channel configuration");
412 iface->conf->secondary_channel = 0;
413 }
414 }
415#endif /* CONFIG_IEEE80211AX */
416
417 if (iface->conf->secondary_channel)
418 res = ieee80211n_allowed_ht40_channel_pair(iface);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700419 if (!res) {
420 iface->conf->secondary_channel = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -0700421 hostapd_set_oper_centr_freq_seg0_idx(iface->conf, 0);
422 hostapd_set_oper_centr_freq_seg1_idx(iface->conf, 0);
Sunil8cd6f4d2022-06-28 18:40:46 +0000423 hostapd_set_oper_chwidth(iface->conf, CONF_OPER_CHWIDTH_USE_HT);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800424 res = 1;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700425 wpa_printf(MSG_INFO, "Fallback to 20 MHz");
426 }
427
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700428 hostapd_setup_interface_complete(iface, !res);
429}
430
431
Dmitry Shmidt04949592012-07-19 12:16:46 -0700432static void ieee80211n_scan_channels_2g4(struct hostapd_iface *iface,
433 struct wpa_driver_scan_params *params)
434{
435 /* Scan only the affected frequency range */
436 int pri_freq, sec_freq;
437 int affected_start, affected_end;
438 int i, pos;
439 struct hostapd_hw_modes *mode;
440
441 if (iface->current_mode == NULL)
442 return;
443
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800444 pri_freq = iface->freq;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700445 if (iface->conf->secondary_channel > 0)
446 sec_freq = pri_freq + 20;
447 else
448 sec_freq = pri_freq - 20;
Dmitry Shmidt41712582015-06-29 11:02:15 -0700449 /*
450 * Note: Need to find the PRI channel also in cases where the affected
451 * channel is the SEC channel of a 40 MHz BSS, so need to include the
452 * scanning coverage here to be 40 MHz from the center frequency.
453 */
454 affected_start = (pri_freq + sec_freq) / 2 - 40;
455 affected_end = (pri_freq + sec_freq) / 2 + 40;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700456 wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
457 affected_start, affected_end);
458
459 mode = iface->current_mode;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700460 params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
Dmitry Shmidt04949592012-07-19 12:16:46 -0700461 if (params->freqs == NULL)
462 return;
463 pos = 0;
464
465 for (i = 0; i < mode->num_channels; i++) {
466 struct hostapd_channel_data *chan = &mode->channels[i];
467 if (chan->flag & HOSTAPD_CHAN_DISABLED)
468 continue;
469 if (chan->freq < affected_start ||
470 chan->freq > affected_end)
471 continue;
472 params->freqs[pos++] = chan->freq;
473 }
474}
475
476
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800477static void ieee80211n_scan_channels_5g(struct hostapd_iface *iface,
478 struct wpa_driver_scan_params *params)
479{
480 /* Scan only the affected frequency range */
481 int pri_freq;
482 int affected_start, affected_end;
483 int i, pos;
484 struct hostapd_hw_modes *mode;
485
486 if (iface->current_mode == NULL)
487 return;
488
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800489 pri_freq = iface->freq;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800490 if (iface->conf->secondary_channel > 0) {
491 affected_start = pri_freq - 10;
492 affected_end = pri_freq + 30;
493 } else {
494 affected_start = pri_freq - 30;
495 affected_end = pri_freq + 10;
496 }
497 wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
498 affected_start, affected_end);
499
500 mode = iface->current_mode;
501 params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
502 if (params->freqs == NULL)
503 return;
504 pos = 0;
505
506 for (i = 0; i < mode->num_channels; i++) {
507 struct hostapd_channel_data *chan = &mode->channels[i];
508 if (chan->flag & HOSTAPD_CHAN_DISABLED)
509 continue;
510 if (chan->freq < affected_start ||
511 chan->freq > affected_end)
512 continue;
513 params->freqs[pos++] = chan->freq;
514 }
515}
516
517
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700518static void ap_ht40_scan_retry(void *eloop_data, void *user_data)
519{
520#define HT2040_COEX_SCAN_RETRY 15
521 struct hostapd_iface *iface = eloop_data;
522 struct wpa_driver_scan_params params;
523 int ret;
524
525 os_memset(&params, 0, sizeof(params));
526 if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
527 ieee80211n_scan_channels_2g4(iface, &params);
528 else
529 ieee80211n_scan_channels_5g(iface, &params);
530
531 ret = hostapd_driver_scan(iface->bss[0], &params);
532 iface->num_ht40_scan_tries++;
533 os_free(params.freqs);
534
535 if (ret == -EBUSY &&
536 iface->num_ht40_scan_tries < HT2040_COEX_SCAN_RETRY) {
537 wpa_printf(MSG_ERROR,
538 "Failed to request a scan of neighboring BSSes ret=%d (%s) - try to scan again (attempt %d)",
539 ret, strerror(-ret), iface->num_ht40_scan_tries);
540 eloop_register_timeout(1, 0, ap_ht40_scan_retry, iface, NULL);
541 return;
542 }
543
544 if (ret == 0) {
545 iface->scan_cb = ieee80211n_check_scan;
Sunil Ravi99c035e2024-07-12 01:42:03 +0000546 iface->bss[0]->scan_cookie = params.scan_cookie;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700547 return;
548 }
549
550 wpa_printf(MSG_DEBUG,
551 "Failed to request a scan in device, bringing up in HT20 mode");
552 iface->conf->secondary_channel = 0;
553 iface->conf->ht_capab &= ~HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
554 hostapd_setup_interface_complete(iface, 0);
555}
556
557
558void hostapd_stop_setup_timers(struct hostapd_iface *iface)
559{
560 eloop_cancel_timeout(ap_ht40_scan_retry, iface, NULL);
561}
562
563
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700564static int ieee80211n_check_40mhz(struct hostapd_iface *iface)
565{
566 struct wpa_driver_scan_params params;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700567 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700568
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800569 /* Check that HT40 is used and PRI / SEC switch is allowed */
570 if (!iface->conf->secondary_channel || iface->conf->no_pri_sec_switch)
571 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700572
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800573 hostapd_set_state(iface, HAPD_IFACE_HT_SCAN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700574 wpa_printf(MSG_DEBUG, "Scan for neighboring BSSes prior to enabling "
575 "40 MHz channel");
576 os_memset(&params, 0, sizeof(params));
Dmitry Shmidt04949592012-07-19 12:16:46 -0700577 if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
578 ieee80211n_scan_channels_2g4(iface, &params);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800579 else
580 ieee80211n_scan_channels_5g(iface, &params);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700581
582 ret = hostapd_driver_scan(iface->bss[0], &params);
583 os_free(params.freqs);
584
585 if (ret == -EBUSY) {
586 wpa_printf(MSG_ERROR,
587 "Failed to request a scan of neighboring BSSes ret=%d (%s) - try to scan again",
588 ret, strerror(-ret));
589 iface->num_ht40_scan_tries = 1;
590 eloop_cancel_timeout(ap_ht40_scan_retry, iface, NULL);
591 eloop_register_timeout(1, 0, ap_ht40_scan_retry, iface, NULL);
592 return 1;
593 }
594
595 if (ret < 0) {
596 wpa_printf(MSG_ERROR,
597 "Failed to request a scan of neighboring BSSes ret=%d (%s)",
598 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700599 return -1;
600 }
601
602 iface->scan_cb = ieee80211n_check_scan;
Sunil Ravi99c035e2024-07-12 01:42:03 +0000603 iface->bss[0]->scan_cookie = params.scan_cookie;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700604 return 1;
605}
606
607
608static int ieee80211n_supported_ht_capab(struct hostapd_iface *iface)
609{
610 u16 hw = iface->current_mode->ht_capab;
611 u16 conf = iface->conf->ht_capab;
612
613 if ((conf & HT_CAP_INFO_LDPC_CODING_CAP) &&
614 !(hw & HT_CAP_INFO_LDPC_CODING_CAP)) {
615 wpa_printf(MSG_ERROR, "Driver does not support configured "
616 "HT capability [LDPC]");
617 return 0;
618 }
619
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700620 /*
621 * Driver ACS chosen channel may not be HT40 due to internal driver
622 * restrictions.
623 */
624 if (!iface->conf->acs && (conf & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700625 !(hw & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
626 wpa_printf(MSG_ERROR, "Driver does not support configured "
627 "HT capability [HT40*]");
628 return 0;
629 }
630
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700631 if ((conf & HT_CAP_INFO_GREEN_FIELD) &&
632 !(hw & HT_CAP_INFO_GREEN_FIELD)) {
633 wpa_printf(MSG_ERROR, "Driver does not support configured "
634 "HT capability [GF]");
635 return 0;
636 }
637
638 if ((conf & HT_CAP_INFO_SHORT_GI20MHZ) &&
639 !(hw & HT_CAP_INFO_SHORT_GI20MHZ)) {
640 wpa_printf(MSG_ERROR, "Driver does not support configured "
641 "HT capability [SHORT-GI-20]");
642 return 0;
643 }
644
645 if ((conf & HT_CAP_INFO_SHORT_GI40MHZ) &&
646 !(hw & HT_CAP_INFO_SHORT_GI40MHZ)) {
647 wpa_printf(MSG_ERROR, "Driver does not support configured "
648 "HT capability [SHORT-GI-40]");
649 return 0;
650 }
651
652 if ((conf & HT_CAP_INFO_TX_STBC) && !(hw & HT_CAP_INFO_TX_STBC)) {
653 wpa_printf(MSG_ERROR, "Driver does not support configured "
654 "HT capability [TX-STBC]");
655 return 0;
656 }
657
658 if ((conf & HT_CAP_INFO_RX_STBC_MASK) >
659 (hw & HT_CAP_INFO_RX_STBC_MASK)) {
660 wpa_printf(MSG_ERROR, "Driver does not support configured "
661 "HT capability [RX-STBC*]");
662 return 0;
663 }
664
665 if ((conf & HT_CAP_INFO_DELAYED_BA) &&
666 !(hw & HT_CAP_INFO_DELAYED_BA)) {
667 wpa_printf(MSG_ERROR, "Driver does not support configured "
668 "HT capability [DELAYED-BA]");
669 return 0;
670 }
671
672 if ((conf & HT_CAP_INFO_MAX_AMSDU_SIZE) &&
673 !(hw & HT_CAP_INFO_MAX_AMSDU_SIZE)) {
674 wpa_printf(MSG_ERROR, "Driver does not support configured "
675 "HT capability [MAX-AMSDU-7935]");
676 return 0;
677 }
678
679 if ((conf & HT_CAP_INFO_DSSS_CCK40MHZ) &&
680 !(hw & HT_CAP_INFO_DSSS_CCK40MHZ)) {
681 wpa_printf(MSG_ERROR, "Driver does not support configured "
682 "HT capability [DSSS_CCK-40]");
683 return 0;
684 }
685
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700686 if ((conf & HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT) &&
687 !(hw & HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT)) {
688 wpa_printf(MSG_ERROR, "Driver does not support configured "
689 "HT capability [LSIG-TXOP-PROT]");
690 return 0;
691 }
692
693 return 1;
694}
695
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700696
697#ifdef CONFIG_IEEE80211AC
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700698static int ieee80211ac_supported_vht_capab(struct hostapd_iface *iface)
699{
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800700 struct hostapd_hw_modes *mode = iface->current_mode;
701 u32 hw = mode->vht_capab;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700702 u32 conf = iface->conf->vht_capab;
703
704 wpa_printf(MSG_DEBUG, "hw vht capab: 0x%x, conf vht capab: 0x%x",
705 hw, conf);
706
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800707 if (mode->mode == HOSTAPD_MODE_IEEE80211G &&
708 iface->conf->bss[0]->vendor_vht &&
709 mode->vht_capab == 0 && iface->hw_features) {
710 int i;
711
712 for (i = 0; i < iface->num_hw_features; i++) {
713 if (iface->hw_features[i].mode ==
714 HOSTAPD_MODE_IEEE80211A) {
715 mode = &iface->hw_features[i];
716 hw = mode->vht_capab;
717 wpa_printf(MSG_DEBUG,
718 "update hw vht capab based on 5 GHz band: 0x%x",
719 hw);
720 break;
721 }
722 }
723 }
724
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800725 return ieee80211ac_cap_check(hw, conf);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700726}
727#endif /* CONFIG_IEEE80211AC */
728
Hai Shalom81f62d82019-07-22 12:10:00 -0700729
730#ifdef CONFIG_IEEE80211AX
731static int ieee80211ax_supported_he_capab(struct hostapd_iface *iface)
732{
733 return 1;
734}
735#endif /* CONFIG_IEEE80211AX */
736
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700737
738int hostapd_check_ht_capab(struct hostapd_iface *iface)
739{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700740 int ret;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800741
742 if (is_6ghz_freq(iface->freq))
743 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700744 if (!iface->conf->ieee80211n)
745 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800746
747 if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211B &&
748 iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G &&
749 (iface->conf->ht_capab & HT_CAP_INFO_DSSS_CCK40MHZ)) {
750 wpa_printf(MSG_DEBUG,
751 "Disable HT capability [DSSS_CCK-40] on 5 GHz band");
752 iface->conf->ht_capab &= ~HT_CAP_INFO_DSSS_CCK40MHZ;
753 }
754
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700755 if (!ieee80211n_supported_ht_capab(iface))
756 return -1;
Hai Shalom81f62d82019-07-22 12:10:00 -0700757#ifdef CONFIG_IEEE80211AX
758 if (iface->conf->ieee80211ax &&
759 !ieee80211ax_supported_he_capab(iface))
760 return -1;
761#endif /* CONFIG_IEEE80211AX */
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700762#ifdef CONFIG_IEEE80211AC
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800763 if (iface->conf->ieee80211ac &&
764 !ieee80211ac_supported_vht_capab(iface))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700765 return -1;
766#endif /* CONFIG_IEEE80211AC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700767 ret = ieee80211n_check_40mhz(iface);
768 if (ret)
769 return ret;
770 if (!ieee80211n_allowed_ht40_channel_pair(iface))
771 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700772
773 return 0;
774}
775
776
Hai Shalomc3565922019-10-28 11:58:20 -0700777int hostapd_check_edmg_capab(struct hostapd_iface *iface)
778{
779 struct hostapd_hw_modes *mode = iface->hw_features;
780 struct ieee80211_edmg_config edmg;
781
782 if (!iface->conf->enable_edmg)
783 return 0;
784
785 hostapd_encode_edmg_chan(iface->conf->enable_edmg,
786 iface->conf->edmg_channel,
787 iface->conf->channel,
788 &edmg);
789
790 if (mode->edmg.channels && ieee802_edmg_is_allowed(mode->edmg, edmg))
791 return 0;
792
793 wpa_printf(MSG_WARNING, "Requested EDMG configuration is not valid");
794 wpa_printf(MSG_INFO, "EDMG capab: channels 0x%x, bw_config %d",
795 mode->edmg.channels, mode->edmg.bw_config);
796 wpa_printf(MSG_INFO,
797 "Requested EDMG configuration: channels 0x%x, bw_config %d",
798 edmg.channels, edmg.bw_config);
799 return -1;
800}
801
802
Hai Shalom60840252021-02-19 19:02:11 -0800803int hostapd_check_he_6ghz_capab(struct hostapd_iface *iface)
804{
805#ifdef CONFIG_IEEE80211AX
806 struct he_capabilities *he_cap;
807 u16 hw;
808
809 if (!iface->current_mode || !is_6ghz_freq(iface->freq))
810 return 0;
811
812 he_cap = &iface->current_mode->he_capab[IEEE80211_MODE_AP];
813 hw = he_cap->he_6ghz_capa;
814 if (iface->conf->he_6ghz_max_mpdu >
815 ((hw & HE_6GHZ_BAND_CAP_MAX_MPDU_LEN_MASK) >>
816 HE_6GHZ_BAND_CAP_MAX_MPDU_LEN_SHIFT)) {
817 wpa_printf(MSG_ERROR,
818 "The driver does not support the configured HE 6 GHz Max MPDU length");
819 return -1;
820 }
821
822 if (iface->conf->he_6ghz_max_ampdu_len_exp >
823 ((hw & HE_6GHZ_BAND_CAP_MAX_AMPDU_LEN_EXP_MASK) >>
824 HE_6GHZ_BAND_CAP_MAX_AMPDU_LEN_EXP_SHIFT)) {
825 wpa_printf(MSG_ERROR,
826 "The driver does not support the configured HE 6 GHz Max AMPDU Length Exponent");
827 return -1;
828 }
829
830 if (iface->conf->he_6ghz_rx_ant_pat &&
831 !(hw & HE_6GHZ_BAND_CAP_RX_ANTPAT_CONS)) {
832 wpa_printf(MSG_ERROR,
833 "The driver does not support the configured HE 6 GHz Rx Antenna Pattern");
834 return -1;
835 }
836
837 if (iface->conf->he_6ghz_tx_ant_pat &&
838 !(hw & HE_6GHZ_BAND_CAP_TX_ANTPAT_CONS)) {
839 wpa_printf(MSG_ERROR,
840 "The driver does not support the configured HE 6 GHz Tx Antenna Pattern");
841 return -1;
842 }
843#endif /* CONFIG_IEEE80211AX */
844 return 0;
845}
846
847
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000848/* Returns:
849 * 1 = usable
850 * 0 = not usable
851 * -1 = not currently usable due to 6 GHz NO-IR
852 */
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700853static int hostapd_is_usable_chan(struct hostapd_iface *iface,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800854 int frequency, int primary)
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700855{
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700856 struct hostapd_channel_data *chan;
857
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700858 if (!iface->current_mode)
859 return 0;
860
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800861 chan = hw_get_channel_freq(iface->current_mode->mode, frequency, NULL,
862 iface->hw_features, iface->num_hw_features);
Hai Shalom74f70d42019-02-11 14:42:39 -0800863 if (!chan)
864 return 0;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700865
Hai Shalom74f70d42019-02-11 14:42:39 -0800866 if ((primary && chan_pri_allowed(chan)) ||
867 (!primary && !(chan->flag & HOSTAPD_CHAN_DISABLED)))
868 return 1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700869
Hai Shalom74f70d42019-02-11 14:42:39 -0800870 wpa_printf(MSG_INFO,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800871 "Frequency %d (%s) not allowed for AP mode, flags: 0x%x%s%s",
872 frequency, primary ? "primary" : "secondary",
Hai Shalom74f70d42019-02-11 14:42:39 -0800873 chan->flag,
874 chan->flag & HOSTAPD_CHAN_NO_IR ? " NO-IR" : "",
875 chan->flag & HOSTAPD_CHAN_RADAR ? " RADAR" : "");
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000876
877 if (is_6ghz_freq(chan->freq) && (chan->flag & HOSTAPD_CHAN_NO_IR))
878 return -1;
879
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700880 return 0;
881}
882
883
Hai Shalomc3565922019-10-28 11:58:20 -0700884static int hostapd_is_usable_edmg(struct hostapd_iface *iface)
885{
886 int i, contiguous = 0;
887 int num_of_enabled = 0;
888 int max_contiguous = 0;
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000889 int err;
Hai Shalomc3565922019-10-28 11:58:20 -0700890 struct ieee80211_edmg_config edmg;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800891 struct hostapd_channel_data *pri_chan;
Hai Shalomc3565922019-10-28 11:58:20 -0700892
893 if (!iface->conf->enable_edmg)
894 return 1;
895
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800896 if (!iface->current_mode)
897 return 0;
898 pri_chan = hw_get_channel_freq(iface->current_mode->mode,
899 iface->freq, NULL,
900 iface->hw_features,
901 iface->num_hw_features);
Hai Shaloma20dcd72022-02-04 13:43:00 -0800902 if (!pri_chan)
903 return 0;
Hai Shalomc3565922019-10-28 11:58:20 -0700904 hostapd_encode_edmg_chan(iface->conf->enable_edmg,
905 iface->conf->edmg_channel,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800906 pri_chan->chan,
Hai Shalomc3565922019-10-28 11:58:20 -0700907 &edmg);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800908 if (!(edmg.channels & BIT(pri_chan->chan - 1)))
Hai Shalomc3565922019-10-28 11:58:20 -0700909 return 0;
910
911 /* 60 GHz channels 1..6 */
912 for (i = 0; i < 6; i++) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700913 int freq = 56160 + 2160 * (i + 1);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800914
Hai Shalomc3565922019-10-28 11:58:20 -0700915 if (edmg.channels & BIT(i)) {
916 contiguous++;
917 num_of_enabled++;
918 } else {
919 contiguous = 0;
920 continue;
921 }
922
923 /* P802.11ay defines that the total number of subfields
924 * set to one does not exceed 4.
925 */
926 if (num_of_enabled > 4)
927 return 0;
928
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000929 err = hostapd_is_usable_chan(iface, freq, 1);
930 if (err <= 0)
931 return err;
Hai Shalomc3565922019-10-28 11:58:20 -0700932
933 if (contiguous > max_contiguous)
934 max_contiguous = contiguous;
935 }
936
937 /* Check if the EDMG configuration is valid under the limitations
938 * of P802.11ay.
939 */
940 /* check bw_config against contiguous EDMG channels */
941 switch (edmg.bw_config) {
942 case EDMG_BW_CONFIG_4:
943 if (!max_contiguous)
944 return 0;
945 break;
946 case EDMG_BW_CONFIG_5:
947 if (max_contiguous < 2)
948 return 0;
949 break;
950 default:
951 return 0;
952 }
953
954 return 1;
955}
956
957
Sunil Ravi036cec52023-03-29 11:35:17 -0700958static bool hostapd_is_usable_punct_bitmap(struct hostapd_iface *iface)
959{
960#ifdef CONFIG_IEEE80211BE
961 struct hostapd_config *conf = iface->conf;
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000962 u16 bw;
963 u8 start_chan;
Sunil Ravi036cec52023-03-29 11:35:17 -0700964
965 if (!conf->punct_bitmap)
966 return true;
967
968 if (!conf->ieee80211be) {
969 wpa_printf(MSG_ERROR,
970 "Currently RU puncturing is supported only if ieee80211be is enabled");
971 return false;
972 }
973
974 if (iface->freq >= 2412 && iface->freq <= 2484) {
975 wpa_printf(MSG_ERROR,
976 "RU puncturing not supported in 2.4 GHz");
977 return false;
978 }
979
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000980 /*
981 * In the 6 GHz band, eht_oper_chwidth is ignored. Use operating class
982 * to determine channel width.
983 */
984 if (conf->op_class == 137) {
985 bw = 320;
986 start_chan = conf->eht_oper_centr_freq_seg0_idx - 30;
987 } else {
988 switch (conf->eht_oper_chwidth) {
989 case 0:
990 wpa_printf(MSG_ERROR,
991 "RU puncturing is supported only in 80 MHz and 160 MHz");
992 return false;
993 case 1:
994 bw = 80;
995 start_chan = conf->eht_oper_centr_freq_seg0_idx - 6;
996 break;
997 case 2:
998 bw = 160;
999 start_chan = conf->eht_oper_centr_freq_seg0_idx - 14;
1000 break;
1001 default:
1002 return false;
1003 }
Sunil Ravi036cec52023-03-29 11:35:17 -07001004 }
1005
1006 if (!is_punct_bitmap_valid(bw, (conf->channel - start_chan) / 4,
1007 conf->punct_bitmap)) {
1008 wpa_printf(MSG_ERROR, "Invalid puncturing bitmap");
1009 return false;
1010 }
1011#endif /* CONFIG_IEEE80211BE */
1012
1013 return true;
1014}
1015
1016
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001017/* Returns:
1018 * 1 = usable
1019 * 0 = not usable
1020 * -1 = not currently usable due to 6 GHz NO-IR
1021 */
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001022static int hostapd_is_usable_chans(struct hostapd_iface *iface)
1023{
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001024 int secondary_freq;
Hai Shalom74f70d42019-02-11 14:42:39 -08001025 struct hostapd_channel_data *pri_chan;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001026 int err, err2;
Hai Shalom74f70d42019-02-11 14:42:39 -08001027
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001028 if (!iface->current_mode)
Hai Shalom74f70d42019-02-11 14:42:39 -08001029 return 0;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001030 pri_chan = hw_get_channel_freq(iface->current_mode->mode,
1031 iface->freq, NULL,
1032 iface->hw_features,
1033 iface->num_hw_features);
1034 if (!pri_chan) {
1035 wpa_printf(MSG_ERROR, "Primary frequency not present");
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001036 return 0;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001037 }
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001038
1039 err = hostapd_is_usable_chan(iface, pri_chan->freq, 1);
1040 if (err <= 0) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001041 wpa_printf(MSG_ERROR, "Primary frequency not allowed");
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001042 return err;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001043 }
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001044 err = hostapd_is_usable_edmg(iface);
1045 if (err <= 0)
1046 return err;
Hai Shalomc3565922019-10-28 11:58:20 -07001047
Sunil Ravi036cec52023-03-29 11:35:17 -07001048 if (!hostapd_is_usable_punct_bitmap(iface))
1049 return 0;
1050
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001051 if (!iface->conf->secondary_channel)
1052 return 1;
1053
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001054 err = hostapd_is_usable_chan(iface, iface->freq +
1055 iface->conf->secondary_channel * 20, 0);
1056 if (err > 0) {
Hai Shaloma20dcd72022-02-04 13:43:00 -08001057 if (iface->conf->secondary_channel == 1 &&
1058 (pri_chan->allowed_bw & HOSTAPD_CHAN_WIDTH_40P))
1059 return 1;
1060 if (iface->conf->secondary_channel == -1 &&
1061 (pri_chan->allowed_bw & HOSTAPD_CHAN_WIDTH_40M))
1062 return 1;
1063 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001064 if (!iface->conf->ht40_plus_minus_allowed)
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001065 return err;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001066
1067 /* Both HT40+ and HT40- are set, pick a valid secondary channel */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001068 secondary_freq = iface->freq + 20;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001069 err2 = hostapd_is_usable_chan(iface, secondary_freq, 0);
1070 if (err2 > 0 && (pri_chan->allowed_bw & HOSTAPD_CHAN_WIDTH_40P)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001071 iface->conf->secondary_channel = 1;
1072 return 1;
1073 }
1074
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001075 secondary_freq = iface->freq - 20;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001076 err2 = hostapd_is_usable_chan(iface, secondary_freq, 0);
1077 if (err2 > 0 && (pri_chan->allowed_bw & HOSTAPD_CHAN_WIDTH_40M)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001078 iface->conf->secondary_channel = -1;
1079 return 1;
1080 }
1081
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001082 return err;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001083}
1084
1085
Sunil Ravi640215c2023-06-28 23:08:09 +00001086static bool skip_mode(struct hostapd_iface *iface,
1087 struct hostapd_hw_modes *mode)
1088{
1089 int chan;
1090
1091 if (iface->freq > 0 && !hw_mode_get_channel(mode, iface->freq, &chan))
1092 return true;
1093
1094 if (is_6ghz_op_class(iface->conf->op_class) && iface->freq == 0 &&
Sunil Ravi99c035e2024-07-12 01:42:03 +00001095 !mode->is_6ghz)
Sunil Ravi640215c2023-06-28 23:08:09 +00001096 return true;
1097
1098 return false;
1099}
1100
1101
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001102int hostapd_determine_mode(struct hostapd_iface *iface)
Hai Shalomfdcde762020-04-02 11:19:20 -07001103{
1104 int i;
1105 enum hostapd_hw_mode target_mode;
1106
1107 if (iface->current_mode ||
1108 iface->conf->hw_mode != HOSTAPD_MODE_IEEE80211ANY)
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001109 return 0;
Hai Shalomfdcde762020-04-02 11:19:20 -07001110
1111 if (iface->freq < 4000)
1112 target_mode = HOSTAPD_MODE_IEEE80211G;
1113 else if (iface->freq > 50000)
1114 target_mode = HOSTAPD_MODE_IEEE80211AD;
1115 else
1116 target_mode = HOSTAPD_MODE_IEEE80211A;
1117
1118 for (i = 0; i < iface->num_hw_features; i++) {
1119 struct hostapd_hw_modes *mode;
1120
1121 mode = &iface->hw_features[i];
1122 if (mode->mode == target_mode) {
Sunil Ravi640215c2023-06-28 23:08:09 +00001123 if (skip_mode(iface, mode))
1124 continue;
1125
Hai Shalomfdcde762020-04-02 11:19:20 -07001126 iface->current_mode = mode;
1127 iface->conf->hw_mode = mode->mode;
1128 break;
1129 }
1130 }
1131
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001132 if (!iface->current_mode) {
1133 wpa_printf(MSG_ERROR, "ACS/CSA: Cannot decide mode");
1134 return -1;
1135 }
1136 return 0;
Hai Shalomfdcde762020-04-02 11:19:20 -07001137}
1138
1139
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001140static enum hostapd_chan_status
1141hostapd_check_chans(struct hostapd_iface *iface)
1142{
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001143 if (iface->freq) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001144 int err;
1145
Hai Shalomfdcde762020-04-02 11:19:20 -07001146 hostapd_determine_mode(iface);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001147
1148 err = hostapd_is_usable_chans(iface);
1149 if (err <= 0) {
1150 if (!err)
1151 return HOSTAPD_CHAN_INVALID;
1152 return HOSTAPD_CHAN_INVALID_NO_IR;
1153 }
1154 return HOSTAPD_CHAN_VALID;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001155 }
1156
1157 /*
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001158 * The user set channel=0 or channel=acs_survey
1159 * which is used to trigger ACS.
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001160 */
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001161
1162 switch (acs_init(iface)) {
1163 case HOSTAPD_CHAN_ACS:
1164 return HOSTAPD_CHAN_ACS;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001165 case HOSTAPD_CHAN_INVALID_NO_IR:
1166 return HOSTAPD_CHAN_INVALID_NO_IR;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001167 case HOSTAPD_CHAN_VALID:
1168 case HOSTAPD_CHAN_INVALID:
1169 default:
1170 return HOSTAPD_CHAN_INVALID;
1171 }
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001172}
1173
1174
1175static void hostapd_notify_bad_chans(struct hostapd_iface *iface)
1176{
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07001177 if (!iface->current_mode) {
1178 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
1179 HOSTAPD_LEVEL_WARNING,
1180 "Hardware does not support configured mode");
1181 return;
1182 }
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001183 hostapd_logger(iface->bss[0], NULL,
1184 HOSTAPD_MODULE_IEEE80211,
1185 HOSTAPD_LEVEL_WARNING,
Hai Shalom60840252021-02-19 19:02:11 -08001186 "Configured channel (%d) or frequency (%d) (secondary_channel=%d) not found from the channel list of the current mode (%d) %s",
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001187 iface->conf->channel,
Hai Shalom60840252021-02-19 19:02:11 -08001188 iface->freq, iface->conf->secondary_channel,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001189 iface->current_mode->mode,
1190 hostapd_hw_mode_txt(iface->current_mode->mode));
1191 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
1192 HOSTAPD_LEVEL_WARNING,
1193 "Hardware does not support configured channel");
1194}
1195
1196
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07001197int hostapd_acs_completed(struct hostapd_iface *iface, int err)
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001198{
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07001199 int ret = -1;
1200
1201 if (err)
1202 goto out;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001203
1204 switch (hostapd_check_chans(iface)) {
1205 case HOSTAPD_CHAN_VALID:
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001206 iface->is_no_ir = false;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001207 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO,
1208 ACS_EVENT_COMPLETED "freq=%d channel=%d",
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001209 iface->freq, iface->conf->channel);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001210 break;
1211 case HOSTAPD_CHAN_ACS:
1212 wpa_printf(MSG_ERROR, "ACS error - reported complete, but no result available");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001213 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, ACS_EVENT_FAILED);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001214 hostapd_notify_bad_chans(iface);
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07001215 goto out;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001216 case HOSTAPD_CHAN_INVALID_NO_IR:
1217 iface->is_no_ir = true;
1218 /* fall through */
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001219 case HOSTAPD_CHAN_INVALID:
1220 default:
1221 wpa_printf(MSG_ERROR, "ACS picked unusable channels");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001222 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, ACS_EVENT_FAILED);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001223 hostapd_notify_bad_chans(iface);
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07001224 goto out;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001225 }
1226
1227 ret = hostapd_check_ht_capab(iface);
1228 if (ret < 0)
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07001229 goto out;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001230 if (ret == 1) {
1231 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback");
1232 return 0;
1233 }
1234
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07001235 ret = 0;
1236out:
1237 return hostapd_setup_interface_complete(iface, ret);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001238}
1239
1240
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001241/**
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001242 * hostapd_csa_update_hwmode - Update hardware mode
1243 * @iface: Pointer to interface data.
1244 * Returns: 0 on success, < 0 on failure
1245 *
1246 * Update hardware mode when the operating channel changed because of CSA.
1247 */
1248int hostapd_csa_update_hwmode(struct hostapd_iface *iface)
1249{
1250 if (!iface || !iface->conf)
1251 return -1;
1252
1253 iface->current_mode = NULL;
1254 iface->conf->hw_mode = HOSTAPD_MODE_IEEE80211ANY;
1255
1256 return hostapd_determine_mode(iface);
1257}
1258
1259
1260/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001261 * hostapd_select_hw_mode - Select the hardware mode
1262 * @iface: Pointer to interface data.
Jouni Malinen87fd2792011-05-16 18:35:42 +03001263 * Returns: 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001264 *
1265 * Sets up the hardware mode, channel, rates, and passive scanning
1266 * based on the configuration.
1267 */
1268int hostapd_select_hw_mode(struct hostapd_iface *iface)
1269{
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001270 int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001271
1272 if (iface->num_hw_features < 1)
1273 return -1;
1274
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001275 if ((iface->conf->hw_mode == HOSTAPD_MODE_IEEE80211G ||
Hai Shalom81f62d82019-07-22 12:10:00 -07001276 iface->conf->ieee80211n || iface->conf->ieee80211ac ||
Sunil8cd6f4d2022-06-28 18:40:46 +00001277 iface->conf->ieee80211ax || iface->conf->ieee80211be) &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001278 iface->conf->channel == 14) {
Sunil8cd6f4d2022-06-28 18:40:46 +00001279 wpa_printf(MSG_INFO, "Disable OFDM/HT/VHT/HE/EHT on channel 14");
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001280 iface->conf->hw_mode = HOSTAPD_MODE_IEEE80211B;
1281 iface->conf->ieee80211n = 0;
1282 iface->conf->ieee80211ac = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -07001283 iface->conf->ieee80211ax = 0;
Sunil8cd6f4d2022-06-28 18:40:46 +00001284 iface->conf->ieee80211be = 0;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001285 }
1286
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001287 iface->current_mode = NULL;
1288 for (i = 0; i < iface->num_hw_features; i++) {
1289 struct hostapd_hw_modes *mode = &iface->hw_features[i];
Hai Shalom60840252021-02-19 19:02:11 -08001290
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001291 if (mode->mode == iface->conf->hw_mode) {
Sunil Ravi640215c2023-06-28 23:08:09 +00001292 if (skip_mode(iface, mode))
Hai Shalomc3565922019-10-28 11:58:20 -07001293 continue;
Hai Shalom60840252021-02-19 19:02:11 -08001294
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001295 iface->current_mode = mode;
1296 break;
1297 }
1298 }
1299
1300 if (iface->current_mode == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001301 if ((iface->drv_flags & WPA_DRIVER_FLAGS_ACS_OFFLOAD) &&
1302 (iface->drv_flags & WPA_DRIVER_FLAGS_SUPPORT_HW_MODE_ANY)) {
1303 wpa_printf(MSG_DEBUG,
1304 "Using offloaded hw_mode=any ACS");
1305 } else if (!(iface->drv_flags & WPA_DRIVER_FLAGS_ACS_OFFLOAD) &&
1306 iface->conf->hw_mode == HOSTAPD_MODE_IEEE80211ANY) {
1307 wpa_printf(MSG_DEBUG,
1308 "Using internal ACS for hw_mode=any");
1309 } else {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07001310 wpa_printf(MSG_ERROR,
1311 "Hardware does not support configured mode");
1312 hostapd_logger(iface->bss[0], NULL,
1313 HOSTAPD_MODULE_IEEE80211,
1314 HOSTAPD_LEVEL_WARNING,
1315 "Hardware does not support configured mode (%d) (hw_mode in hostapd.conf)",
1316 (int) iface->conf->hw_mode);
1317 return -2;
1318 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001319 }
1320
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001321 switch (hostapd_check_chans(iface)) {
1322 case HOSTAPD_CHAN_VALID:
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001323 iface->is_no_ir = false;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001324 return 0;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001325 case HOSTAPD_CHAN_ACS: /* ACS will run and later complete */
1326 return 1;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001327 case HOSTAPD_CHAN_INVALID_NO_IR:
1328 iface->is_no_ir = true;
1329 /* fall through */
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001330 case HOSTAPD_CHAN_INVALID:
1331 default:
1332 hostapd_notify_bad_chans(iface);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001333 return -3;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001334 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001335}
1336
1337
1338const char * hostapd_hw_mode_txt(int mode)
1339{
1340 switch (mode) {
1341 case HOSTAPD_MODE_IEEE80211A:
1342 return "IEEE 802.11a";
1343 case HOSTAPD_MODE_IEEE80211B:
1344 return "IEEE 802.11b";
1345 case HOSTAPD_MODE_IEEE80211G:
1346 return "IEEE 802.11g";
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001347 case HOSTAPD_MODE_IEEE80211AD:
1348 return "IEEE 802.11ad";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001349 default:
1350 return "UNKNOWN";
1351 }
1352}
1353
1354
1355int hostapd_hw_get_freq(struct hostapd_data *hapd, int chan)
1356{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001357 return hw_get_freq(hapd->iface->current_mode, chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001358}
1359
1360
1361int hostapd_hw_get_channel(struct hostapd_data *hapd, int freq)
1362{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001363 int i, channel;
1364 struct hostapd_hw_modes *mode;
1365
Hai Shalom1dc4d202019-04-29 16:22:27 -07001366 if (hapd->iface->current_mode) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001367 channel = hw_get_chan(hapd->iface->current_mode->mode, freq,
1368 hapd->iface->hw_features,
1369 hapd->iface->num_hw_features);
Hai Shalom1dc4d202019-04-29 16:22:27 -07001370 if (channel)
1371 return channel;
1372 }
1373
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001374 /* Check other available modes since the channel list for the current
1375 * mode did not include the specified frequency. */
Hai Shalom1dc4d202019-04-29 16:22:27 -07001376 if (!hapd->iface->hw_features)
1377 return 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001378 for (i = 0; i < hapd->iface->num_hw_features; i++) {
1379 mode = &hapd->iface->hw_features[i];
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001380 channel = hw_get_chan(mode->mode, freq,
1381 hapd->iface->hw_features,
1382 hapd->iface->num_hw_features);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001383 if (channel)
1384 return channel;
1385 }
1386 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001387}
Hai Shalomfdcde762020-04-02 11:19:20 -07001388
1389
1390int hostapd_hw_skip_mode(struct hostapd_iface *iface,
1391 struct hostapd_hw_modes *mode)
1392{
1393 int i;
1394
1395 if (iface->current_mode)
1396 return mode != iface->current_mode;
1397 if (mode->mode != HOSTAPD_MODE_IEEE80211B)
1398 return 0;
1399 for (i = 0; i < iface->num_hw_features; i++) {
1400 if (iface->hw_features[i].mode == HOSTAPD_MODE_IEEE80211G)
1401 return 1;
1402 }
1403 return 0;
1404}
Sunil Ravic0f5d412024-09-11 22:12:49 +00001405
1406
1407void hostapd_free_multi_hw_info(struct hostapd_multi_hw_info *multi_hw_info)
1408{
1409 os_free(multi_hw_info);
1410}
1411
1412
1413int hostapd_set_current_hw_info(struct hostapd_iface *iface, int oper_freq)
1414{
1415 struct hostapd_multi_hw_info *hw_info;
1416 unsigned int i;
1417
1418 if (!iface->num_multi_hws)
1419 return 0;
1420
1421 for (i = 0; i < iface->num_multi_hws; i++) {
1422 hw_info = &iface->multi_hw_info[i];
1423
1424 if (hw_info->start_freq <= oper_freq &&
1425 hw_info->end_freq >= oper_freq) {
1426 iface->current_hw_info = hw_info;
1427 wpa_printf(MSG_DEBUG,
1428 "Mode: Selected underlying hardware: hw_idx=%u",
1429 iface->current_hw_info->hw_idx);
1430 return 0;
1431 }
1432 }
1433
1434 return -1;
1435}