blob: 842d9f5ba52a966a5e864525f59572869027a94c [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;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070079 u16 num_modes, flags;
80 struct hostapd_hw_modes *modes;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070081 u8 dfs_domain;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070082
83 if (hostapd_drv_none(hapd))
84 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070085 modes = hostapd_get_hw_feature_data(hapd, &num_modes, &flags,
86 &dfs_domain);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070087 if (modes == NULL) {
88 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
89 HOSTAPD_LEVEL_DEBUG,
90 "Fetching hardware channel/rate support not "
91 "supported.");
92 return -1;
93 }
94
95 iface->hw_flags = flags;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070096 iface->dfs_domain = dfs_domain;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070097
98 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
99 iface->hw_features = modes;
100 iface->num_hw_features = num_modes;
101
102 for (i = 0; i < num_modes; i++) {
103 struct hostapd_hw_modes *feature = &modes[i];
Dmitry Shmidt051af732013-10-22 13:52:46 -0700104 int dfs_enabled = hapd->iconf->ieee80211h &&
105 (iface->drv_flags & WPA_DRIVER_FLAGS_RADAR);
106
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700107 /* set flag for channels we can use in current regulatory
108 * domain */
109 for (j = 0; j < feature->num_channels; j++) {
Dmitry Shmidt051af732013-10-22 13:52:46 -0700110 int dfs = 0;
111
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700112 /*
113 * Disable all channels that are marked not to allow
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800114 * to initiate radiation (a.k.a. passive scan and no
115 * IBSS).
Dmitry Shmidt051af732013-10-22 13:52:46 -0700116 * Use radar channels only if the driver supports DFS.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700117 */
Dmitry Shmidt051af732013-10-22 13:52:46 -0700118 if ((feature->channels[j].flag &
119 HOSTAPD_CHAN_RADAR) && dfs_enabled) {
120 dfs = 1;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700121 } else if (((feature->channels[j].flag &
122 HOSTAPD_CHAN_RADAR) &&
123 !(iface->drv_flags &
124 WPA_DRIVER_FLAGS_DFS_OFFLOAD)) ||
125 (feature->channels[j].flag &
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800126 HOSTAPD_CHAN_NO_IR)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700127 feature->channels[j].flag |=
128 HOSTAPD_CHAN_DISABLED;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700129 }
130
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700131 if (feature->channels[j].flag & HOSTAPD_CHAN_DISABLED)
132 continue;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700133
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700134 wpa_printf(MSG_MSGDUMP, "Allowed channel: mode=%d "
Dmitry Shmidt051af732013-10-22 13:52:46 -0700135 "chan=%d freq=%d MHz max_tx_power=%d dBm%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700136 feature->mode,
137 feature->channels[j].chan,
138 feature->channels[j].freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700139 feature->channels[j].max_tx_power,
140 dfs ? dfs_info(&feature->channels[j]) : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700141 }
142 }
143
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800144 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700145}
146
147
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800148int hostapd_prepare_rates(struct hostapd_iface *iface,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700149 struct hostapd_hw_modes *mode)
150{
151 int i, num_basic_rates = 0;
152 int basic_rates_a[] = { 60, 120, 240, -1 };
153 int basic_rates_b[] = { 10, 20, -1 };
154 int basic_rates_g[] = { 10, 20, 55, 110, -1 };
155 int *basic_rates;
156
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800157 if (iface->conf->basic_rates)
158 basic_rates = iface->conf->basic_rates;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700159 else switch (mode->mode) {
160 case HOSTAPD_MODE_IEEE80211A:
161 basic_rates = basic_rates_a;
162 break;
163 case HOSTAPD_MODE_IEEE80211B:
164 basic_rates = basic_rates_b;
165 break;
166 case HOSTAPD_MODE_IEEE80211G:
167 basic_rates = basic_rates_g;
168 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800169 case HOSTAPD_MODE_IEEE80211AD:
170 return 0; /* No basic rates for 11ad */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700171 default:
172 return -1;
173 }
174
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800175 i = 0;
176 while (basic_rates[i] >= 0)
177 i++;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700178 if (i)
179 i++; /* -1 termination */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800180 os_free(iface->basic_rates);
181 iface->basic_rates = os_malloc(i * sizeof(int));
182 if (iface->basic_rates)
183 os_memcpy(iface->basic_rates, basic_rates, i * sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700184
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800185 os_free(iface->current_rates);
186 iface->num_rates = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700187
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800188 iface->current_rates =
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700189 os_calloc(mode->num_rates, sizeof(struct hostapd_rate_data));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800190 if (!iface->current_rates) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700191 wpa_printf(MSG_ERROR, "Failed to allocate memory for rate "
192 "table.");
193 return -1;
194 }
195
196 for (i = 0; i < mode->num_rates; i++) {
197 struct hostapd_rate_data *rate;
198
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800199 if (iface->conf->supported_rates &&
200 !hostapd_rate_found(iface->conf->supported_rates,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700201 mode->rates[i]))
202 continue;
203
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800204 rate = &iface->current_rates[iface->num_rates];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700205 rate->rate = mode->rates[i];
206 if (hostapd_rate_found(basic_rates, rate->rate)) {
207 rate->flags |= HOSTAPD_RATE_BASIC;
208 num_basic_rates++;
209 }
210 wpa_printf(MSG_DEBUG, "RATE[%d] rate=%d flags=0x%x",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800211 iface->num_rates, rate->rate, rate->flags);
212 iface->num_rates++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700213 }
214
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800215 if ((iface->num_rates == 0 || num_basic_rates == 0) &&
216 (!iface->conf->ieee80211n || !iface->conf->require_ht)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700217 wpa_printf(MSG_ERROR, "No rates remaining in supported/basic "
218 "rate sets (%d,%d).",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800219 iface->num_rates, num_basic_rates);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700220 return -1;
221 }
222
223 return 0;
224}
225
226
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700227static int ieee80211n_allowed_ht40_channel_pair(struct hostapd_iface *iface)
228{
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800229 int pri_freq, sec_freq;
230 struct hostapd_channel_data *p_chan, *s_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700231
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800232 pri_freq = iface->freq;
233 sec_freq = pri_freq + iface->conf->secondary_channel * 20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700234
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800235 if (!iface->current_mode)
236 return 0;
237
238 p_chan = hw_get_channel_freq(iface->current_mode->mode, pri_freq, NULL,
239 iface->hw_features,
240 iface->num_hw_features);
241
242 s_chan = hw_get_channel_freq(iface->current_mode->mode, sec_freq, NULL,
243 iface->hw_features,
244 iface->num_hw_features);
245
246 return allowed_ht40_channel_pair(iface->current_mode->mode,
247 p_chan, s_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700248}
249
250
251static void ieee80211n_switch_pri_sec(struct hostapd_iface *iface)
252{
253 if (iface->conf->secondary_channel > 0) {
254 iface->conf->channel += 4;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800255 iface->freq += 20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700256 iface->conf->secondary_channel = -1;
257 } else {
258 iface->conf->channel -= 4;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800259 iface->freq -= 20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700260 iface->conf->secondary_channel = 1;
261 }
262}
263
264
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700265static int ieee80211n_check_40mhz_5g(struct hostapd_iface *iface,
266 struct wpa_scan_results *scan_res)
267{
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800268 unsigned int pri_freq, sec_freq;
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800269 int res;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800270 struct hostapd_channel_data *pri_chan, *sec_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700271
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800272 pri_freq = iface->freq;
273 sec_freq = pri_freq + iface->conf->secondary_channel * 20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700274
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800275 if (!iface->current_mode)
276 return 0;
277 pri_chan = hw_get_channel_freq(iface->current_mode->mode, pri_freq,
278 NULL, iface->hw_features,
279 iface->num_hw_features);
280 sec_chan = hw_get_channel_freq(iface->current_mode->mode, sec_freq,
281 NULL, iface->hw_features,
282 iface->num_hw_features);
283
284 res = check_40mhz_5g(scan_res, pri_chan, sec_chan);
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800285
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800286 if (res == 2) {
287 if (iface->conf->no_pri_sec_switch) {
288 wpa_printf(MSG_DEBUG,
289 "Cannot switch PRI/SEC channels due to local constraint");
290 } else {
291 ieee80211n_switch_pri_sec(iface);
292 }
293 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700294
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800295 return !!res;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -0700296}
297
298
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700299static int ieee80211n_check_40mhz_2g4(struct hostapd_iface *iface,
300 struct wpa_scan_results *scan_res)
301{
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800302 int pri_chan, sec_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700303
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800304 pri_chan = iface->conf->channel;
305 sec_chan = pri_chan + iface->conf->secondary_channel * 4;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700306
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800307 return check_40mhz_2g4(iface->current_mode, scan_res, pri_chan,
308 sec_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700309}
310
311
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700312static void ieee80211n_check_scan(struct hostapd_iface *iface)
313{
314 struct wpa_scan_results *scan_res;
315 int oper40;
Hai Shalom60840252021-02-19 19:02:11 -0800316 int res = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700317
318 /* Check list of neighboring BSSes (from scan) to see whether 40 MHz is
Dmitry Shmidt04949592012-07-19 12:16:46 -0700319 * allowed per IEEE Std 802.11-2012, 10.15.3.2 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700320
321 iface->scan_cb = NULL;
322
323 scan_res = hostapd_driver_get_scan_results(iface->bss[0]);
324 if (scan_res == NULL) {
325 hostapd_setup_interface_complete(iface, 1);
326 return;
327 }
328
329 if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A)
330 oper40 = ieee80211n_check_40mhz_5g(iface, scan_res);
331 else
332 oper40 = ieee80211n_check_40mhz_2g4(iface, scan_res);
333 wpa_scan_results_free(scan_res);
334
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700335 iface->secondary_ch = iface->conf->secondary_channel;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700336 if (!oper40) {
337 wpa_printf(MSG_INFO, "20/40 MHz operation not permitted on "
338 "channel pri=%d sec=%d based on overlapping BSSes",
339 iface->conf->channel,
340 iface->conf->channel +
341 iface->conf->secondary_channel * 4);
342 iface->conf->secondary_channel = 0;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700343 if (iface->drv_flags & WPA_DRIVER_FLAGS_HT_2040_COEX) {
344 /*
345 * TODO: Could consider scheduling another scan to check
346 * if channel width can be changed if no coex reports
347 * are received from associating stations.
348 */
349 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700350 }
351
Hai Shalom60840252021-02-19 19:02:11 -0800352#ifdef CONFIG_IEEE80211AX
353 if (iface->conf->secondary_channel &&
354 iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
355 iface->conf->ieee80211ax) {
356 struct he_capabilities *he_cap;
357
358 he_cap = &iface->current_mode->he_capab[IEEE80211_MODE_AP];
359 if (!(he_cap->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
360 HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_IN_2G)) {
361 wpa_printf(MSG_DEBUG,
362 "HE: 40 MHz channel width is not supported in 2.4 GHz; clear secondary channel configuration");
363 iface->conf->secondary_channel = 0;
364 }
365 }
366#endif /* CONFIG_IEEE80211AX */
367
368 if (iface->conf->secondary_channel)
369 res = ieee80211n_allowed_ht40_channel_pair(iface);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700370 if (!res) {
371 iface->conf->secondary_channel = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -0700372 hostapd_set_oper_centr_freq_seg0_idx(iface->conf, 0);
373 hostapd_set_oper_centr_freq_seg1_idx(iface->conf, 0);
Sunil8cd6f4d2022-06-28 18:40:46 +0000374 hostapd_set_oper_chwidth(iface->conf, CONF_OPER_CHWIDTH_USE_HT);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800375 res = 1;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700376 wpa_printf(MSG_INFO, "Fallback to 20 MHz");
377 }
378
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700379 hostapd_setup_interface_complete(iface, !res);
380}
381
382
Dmitry Shmidt04949592012-07-19 12:16:46 -0700383static void ieee80211n_scan_channels_2g4(struct hostapd_iface *iface,
384 struct wpa_driver_scan_params *params)
385{
386 /* Scan only the affected frequency range */
387 int pri_freq, sec_freq;
388 int affected_start, affected_end;
389 int i, pos;
390 struct hostapd_hw_modes *mode;
391
392 if (iface->current_mode == NULL)
393 return;
394
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800395 pri_freq = iface->freq;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700396 if (iface->conf->secondary_channel > 0)
397 sec_freq = pri_freq + 20;
398 else
399 sec_freq = pri_freq - 20;
Dmitry Shmidt41712582015-06-29 11:02:15 -0700400 /*
401 * Note: Need to find the PRI channel also in cases where the affected
402 * channel is the SEC channel of a 40 MHz BSS, so need to include the
403 * scanning coverage here to be 40 MHz from the center frequency.
404 */
405 affected_start = (pri_freq + sec_freq) / 2 - 40;
406 affected_end = (pri_freq + sec_freq) / 2 + 40;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700407 wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
408 affected_start, affected_end);
409
410 mode = iface->current_mode;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700411 params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
Dmitry Shmidt04949592012-07-19 12:16:46 -0700412 if (params->freqs == NULL)
413 return;
414 pos = 0;
415
416 for (i = 0; i < mode->num_channels; i++) {
417 struct hostapd_channel_data *chan = &mode->channels[i];
418 if (chan->flag & HOSTAPD_CHAN_DISABLED)
419 continue;
420 if (chan->freq < affected_start ||
421 chan->freq > affected_end)
422 continue;
423 params->freqs[pos++] = chan->freq;
424 }
425}
426
427
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800428static void ieee80211n_scan_channels_5g(struct hostapd_iface *iface,
429 struct wpa_driver_scan_params *params)
430{
431 /* Scan only the affected frequency range */
432 int pri_freq;
433 int affected_start, affected_end;
434 int i, pos;
435 struct hostapd_hw_modes *mode;
436
437 if (iface->current_mode == NULL)
438 return;
439
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800440 pri_freq = iface->freq;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800441 if (iface->conf->secondary_channel > 0) {
442 affected_start = pri_freq - 10;
443 affected_end = pri_freq + 30;
444 } else {
445 affected_start = pri_freq - 30;
446 affected_end = pri_freq + 10;
447 }
448 wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
449 affected_start, affected_end);
450
451 mode = iface->current_mode;
452 params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
453 if (params->freqs == NULL)
454 return;
455 pos = 0;
456
457 for (i = 0; i < mode->num_channels; i++) {
458 struct hostapd_channel_data *chan = &mode->channels[i];
459 if (chan->flag & HOSTAPD_CHAN_DISABLED)
460 continue;
461 if (chan->freq < affected_start ||
462 chan->freq > affected_end)
463 continue;
464 params->freqs[pos++] = chan->freq;
465 }
466}
467
468
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700469static void ap_ht40_scan_retry(void *eloop_data, void *user_data)
470{
471#define HT2040_COEX_SCAN_RETRY 15
472 struct hostapd_iface *iface = eloop_data;
473 struct wpa_driver_scan_params params;
474 int ret;
475
476 os_memset(&params, 0, sizeof(params));
477 if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
478 ieee80211n_scan_channels_2g4(iface, &params);
479 else
480 ieee80211n_scan_channels_5g(iface, &params);
481
482 ret = hostapd_driver_scan(iface->bss[0], &params);
483 iface->num_ht40_scan_tries++;
484 os_free(params.freqs);
485
486 if (ret == -EBUSY &&
487 iface->num_ht40_scan_tries < HT2040_COEX_SCAN_RETRY) {
488 wpa_printf(MSG_ERROR,
489 "Failed to request a scan of neighboring BSSes ret=%d (%s) - try to scan again (attempt %d)",
490 ret, strerror(-ret), iface->num_ht40_scan_tries);
491 eloop_register_timeout(1, 0, ap_ht40_scan_retry, iface, NULL);
492 return;
493 }
494
495 if (ret == 0) {
496 iface->scan_cb = ieee80211n_check_scan;
497 return;
498 }
499
500 wpa_printf(MSG_DEBUG,
501 "Failed to request a scan in device, bringing up in HT20 mode");
502 iface->conf->secondary_channel = 0;
503 iface->conf->ht_capab &= ~HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
504 hostapd_setup_interface_complete(iface, 0);
505}
506
507
508void hostapd_stop_setup_timers(struct hostapd_iface *iface)
509{
510 eloop_cancel_timeout(ap_ht40_scan_retry, iface, NULL);
511}
512
513
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700514static int ieee80211n_check_40mhz(struct hostapd_iface *iface)
515{
516 struct wpa_driver_scan_params params;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700517 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700518
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800519 /* Check that HT40 is used and PRI / SEC switch is allowed */
520 if (!iface->conf->secondary_channel || iface->conf->no_pri_sec_switch)
521 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700522
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800523 hostapd_set_state(iface, HAPD_IFACE_HT_SCAN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700524 wpa_printf(MSG_DEBUG, "Scan for neighboring BSSes prior to enabling "
525 "40 MHz channel");
526 os_memset(&params, 0, sizeof(params));
Dmitry Shmidt04949592012-07-19 12:16:46 -0700527 if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
528 ieee80211n_scan_channels_2g4(iface, &params);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800529 else
530 ieee80211n_scan_channels_5g(iface, &params);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700531
532 ret = hostapd_driver_scan(iface->bss[0], &params);
533 os_free(params.freqs);
534
535 if (ret == -EBUSY) {
536 wpa_printf(MSG_ERROR,
537 "Failed to request a scan of neighboring BSSes ret=%d (%s) - try to scan again",
538 ret, strerror(-ret));
539 iface->num_ht40_scan_tries = 1;
540 eloop_cancel_timeout(ap_ht40_scan_retry, iface, NULL);
541 eloop_register_timeout(1, 0, ap_ht40_scan_retry, iface, NULL);
542 return 1;
543 }
544
545 if (ret < 0) {
546 wpa_printf(MSG_ERROR,
547 "Failed to request a scan of neighboring BSSes ret=%d (%s)",
548 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700549 return -1;
550 }
551
552 iface->scan_cb = ieee80211n_check_scan;
553 return 1;
554}
555
556
557static int ieee80211n_supported_ht_capab(struct hostapd_iface *iface)
558{
559 u16 hw = iface->current_mode->ht_capab;
560 u16 conf = iface->conf->ht_capab;
561
562 if ((conf & HT_CAP_INFO_LDPC_CODING_CAP) &&
563 !(hw & HT_CAP_INFO_LDPC_CODING_CAP)) {
564 wpa_printf(MSG_ERROR, "Driver does not support configured "
565 "HT capability [LDPC]");
566 return 0;
567 }
568
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700569 /*
570 * Driver ACS chosen channel may not be HT40 due to internal driver
571 * restrictions.
572 */
573 if (!iface->conf->acs && (conf & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700574 !(hw & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
575 wpa_printf(MSG_ERROR, "Driver does not support configured "
576 "HT capability [HT40*]");
577 return 0;
578 }
579
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700580 if ((conf & HT_CAP_INFO_GREEN_FIELD) &&
581 !(hw & HT_CAP_INFO_GREEN_FIELD)) {
582 wpa_printf(MSG_ERROR, "Driver does not support configured "
583 "HT capability [GF]");
584 return 0;
585 }
586
587 if ((conf & HT_CAP_INFO_SHORT_GI20MHZ) &&
588 !(hw & HT_CAP_INFO_SHORT_GI20MHZ)) {
589 wpa_printf(MSG_ERROR, "Driver does not support configured "
590 "HT capability [SHORT-GI-20]");
591 return 0;
592 }
593
594 if ((conf & HT_CAP_INFO_SHORT_GI40MHZ) &&
595 !(hw & HT_CAP_INFO_SHORT_GI40MHZ)) {
596 wpa_printf(MSG_ERROR, "Driver does not support configured "
597 "HT capability [SHORT-GI-40]");
598 return 0;
599 }
600
601 if ((conf & HT_CAP_INFO_TX_STBC) && !(hw & HT_CAP_INFO_TX_STBC)) {
602 wpa_printf(MSG_ERROR, "Driver does not support configured "
603 "HT capability [TX-STBC]");
604 return 0;
605 }
606
607 if ((conf & HT_CAP_INFO_RX_STBC_MASK) >
608 (hw & HT_CAP_INFO_RX_STBC_MASK)) {
609 wpa_printf(MSG_ERROR, "Driver does not support configured "
610 "HT capability [RX-STBC*]");
611 return 0;
612 }
613
614 if ((conf & HT_CAP_INFO_DELAYED_BA) &&
615 !(hw & HT_CAP_INFO_DELAYED_BA)) {
616 wpa_printf(MSG_ERROR, "Driver does not support configured "
617 "HT capability [DELAYED-BA]");
618 return 0;
619 }
620
621 if ((conf & HT_CAP_INFO_MAX_AMSDU_SIZE) &&
622 !(hw & HT_CAP_INFO_MAX_AMSDU_SIZE)) {
623 wpa_printf(MSG_ERROR, "Driver does not support configured "
624 "HT capability [MAX-AMSDU-7935]");
625 return 0;
626 }
627
628 if ((conf & HT_CAP_INFO_DSSS_CCK40MHZ) &&
629 !(hw & HT_CAP_INFO_DSSS_CCK40MHZ)) {
630 wpa_printf(MSG_ERROR, "Driver does not support configured "
631 "HT capability [DSSS_CCK-40]");
632 return 0;
633 }
634
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700635 if ((conf & HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT) &&
636 !(hw & HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT)) {
637 wpa_printf(MSG_ERROR, "Driver does not support configured "
638 "HT capability [LSIG-TXOP-PROT]");
639 return 0;
640 }
641
642 return 1;
643}
644
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700645
646#ifdef CONFIG_IEEE80211AC
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700647static int ieee80211ac_supported_vht_capab(struct hostapd_iface *iface)
648{
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800649 struct hostapd_hw_modes *mode = iface->current_mode;
650 u32 hw = mode->vht_capab;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700651 u32 conf = iface->conf->vht_capab;
652
653 wpa_printf(MSG_DEBUG, "hw vht capab: 0x%x, conf vht capab: 0x%x",
654 hw, conf);
655
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800656 if (mode->mode == HOSTAPD_MODE_IEEE80211G &&
657 iface->conf->bss[0]->vendor_vht &&
658 mode->vht_capab == 0 && iface->hw_features) {
659 int i;
660
661 for (i = 0; i < iface->num_hw_features; i++) {
662 if (iface->hw_features[i].mode ==
663 HOSTAPD_MODE_IEEE80211A) {
664 mode = &iface->hw_features[i];
665 hw = mode->vht_capab;
666 wpa_printf(MSG_DEBUG,
667 "update hw vht capab based on 5 GHz band: 0x%x",
668 hw);
669 break;
670 }
671 }
672 }
673
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800674 return ieee80211ac_cap_check(hw, conf);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700675}
676#endif /* CONFIG_IEEE80211AC */
677
Hai Shalom81f62d82019-07-22 12:10:00 -0700678
679#ifdef CONFIG_IEEE80211AX
680static int ieee80211ax_supported_he_capab(struct hostapd_iface *iface)
681{
682 return 1;
683}
684#endif /* CONFIG_IEEE80211AX */
685
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700686
687int hostapd_check_ht_capab(struct hostapd_iface *iface)
688{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700689 int ret;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800690
691 if (is_6ghz_freq(iface->freq))
692 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700693 if (!iface->conf->ieee80211n)
694 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800695
696 if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211B &&
697 iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G &&
698 (iface->conf->ht_capab & HT_CAP_INFO_DSSS_CCK40MHZ)) {
699 wpa_printf(MSG_DEBUG,
700 "Disable HT capability [DSSS_CCK-40] on 5 GHz band");
701 iface->conf->ht_capab &= ~HT_CAP_INFO_DSSS_CCK40MHZ;
702 }
703
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700704 if (!ieee80211n_supported_ht_capab(iface))
705 return -1;
Hai Shalom81f62d82019-07-22 12:10:00 -0700706#ifdef CONFIG_IEEE80211AX
707 if (iface->conf->ieee80211ax &&
708 !ieee80211ax_supported_he_capab(iface))
709 return -1;
710#endif /* CONFIG_IEEE80211AX */
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700711#ifdef CONFIG_IEEE80211AC
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800712 if (iface->conf->ieee80211ac &&
713 !ieee80211ac_supported_vht_capab(iface))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700714 return -1;
715#endif /* CONFIG_IEEE80211AC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700716 ret = ieee80211n_check_40mhz(iface);
717 if (ret)
718 return ret;
719 if (!ieee80211n_allowed_ht40_channel_pair(iface))
720 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700721
722 return 0;
723}
724
725
Hai Shalomc3565922019-10-28 11:58:20 -0700726int hostapd_check_edmg_capab(struct hostapd_iface *iface)
727{
728 struct hostapd_hw_modes *mode = iface->hw_features;
729 struct ieee80211_edmg_config edmg;
730
731 if (!iface->conf->enable_edmg)
732 return 0;
733
734 hostapd_encode_edmg_chan(iface->conf->enable_edmg,
735 iface->conf->edmg_channel,
736 iface->conf->channel,
737 &edmg);
738
739 if (mode->edmg.channels && ieee802_edmg_is_allowed(mode->edmg, edmg))
740 return 0;
741
742 wpa_printf(MSG_WARNING, "Requested EDMG configuration is not valid");
743 wpa_printf(MSG_INFO, "EDMG capab: channels 0x%x, bw_config %d",
744 mode->edmg.channels, mode->edmg.bw_config);
745 wpa_printf(MSG_INFO,
746 "Requested EDMG configuration: channels 0x%x, bw_config %d",
747 edmg.channels, edmg.bw_config);
748 return -1;
749}
750
751
Hai Shalom60840252021-02-19 19:02:11 -0800752int hostapd_check_he_6ghz_capab(struct hostapd_iface *iface)
753{
754#ifdef CONFIG_IEEE80211AX
755 struct he_capabilities *he_cap;
756 u16 hw;
757
758 if (!iface->current_mode || !is_6ghz_freq(iface->freq))
759 return 0;
760
761 he_cap = &iface->current_mode->he_capab[IEEE80211_MODE_AP];
762 hw = he_cap->he_6ghz_capa;
763 if (iface->conf->he_6ghz_max_mpdu >
764 ((hw & HE_6GHZ_BAND_CAP_MAX_MPDU_LEN_MASK) >>
765 HE_6GHZ_BAND_CAP_MAX_MPDU_LEN_SHIFT)) {
766 wpa_printf(MSG_ERROR,
767 "The driver does not support the configured HE 6 GHz Max MPDU length");
768 return -1;
769 }
770
771 if (iface->conf->he_6ghz_max_ampdu_len_exp >
772 ((hw & HE_6GHZ_BAND_CAP_MAX_AMPDU_LEN_EXP_MASK) >>
773 HE_6GHZ_BAND_CAP_MAX_AMPDU_LEN_EXP_SHIFT)) {
774 wpa_printf(MSG_ERROR,
775 "The driver does not support the configured HE 6 GHz Max AMPDU Length Exponent");
776 return -1;
777 }
778
779 if (iface->conf->he_6ghz_rx_ant_pat &&
780 !(hw & HE_6GHZ_BAND_CAP_RX_ANTPAT_CONS)) {
781 wpa_printf(MSG_ERROR,
782 "The driver does not support the configured HE 6 GHz Rx Antenna Pattern");
783 return -1;
784 }
785
786 if (iface->conf->he_6ghz_tx_ant_pat &&
787 !(hw & HE_6GHZ_BAND_CAP_TX_ANTPAT_CONS)) {
788 wpa_printf(MSG_ERROR,
789 "The driver does not support the configured HE 6 GHz Tx Antenna Pattern");
790 return -1;
791 }
792#endif /* CONFIG_IEEE80211AX */
793 return 0;
794}
795
796
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700797static int hostapd_is_usable_chan(struct hostapd_iface *iface,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800798 int frequency, int primary)
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700799{
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700800 struct hostapd_channel_data *chan;
801
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700802 if (!iface->current_mode)
803 return 0;
804
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800805 chan = hw_get_channel_freq(iface->current_mode->mode, frequency, NULL,
806 iface->hw_features, iface->num_hw_features);
Hai Shalom74f70d42019-02-11 14:42:39 -0800807 if (!chan)
808 return 0;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700809
Hai Shalom74f70d42019-02-11 14:42:39 -0800810 if ((primary && chan_pri_allowed(chan)) ||
811 (!primary && !(chan->flag & HOSTAPD_CHAN_DISABLED)))
812 return 1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700813
Hai Shalom74f70d42019-02-11 14:42:39 -0800814 wpa_printf(MSG_INFO,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800815 "Frequency %d (%s) not allowed for AP mode, flags: 0x%x%s%s",
816 frequency, primary ? "primary" : "secondary",
Hai Shalom74f70d42019-02-11 14:42:39 -0800817 chan->flag,
818 chan->flag & HOSTAPD_CHAN_NO_IR ? " NO-IR" : "",
819 chan->flag & HOSTAPD_CHAN_RADAR ? " RADAR" : "");
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700820 return 0;
821}
822
823
Hai Shalomc3565922019-10-28 11:58:20 -0700824static int hostapd_is_usable_edmg(struct hostapd_iface *iface)
825{
826 int i, contiguous = 0;
827 int num_of_enabled = 0;
828 int max_contiguous = 0;
829 struct ieee80211_edmg_config edmg;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800830 struct hostapd_channel_data *pri_chan;
Hai Shalomc3565922019-10-28 11:58:20 -0700831
832 if (!iface->conf->enable_edmg)
833 return 1;
834
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800835 if (!iface->current_mode)
836 return 0;
837 pri_chan = hw_get_channel_freq(iface->current_mode->mode,
838 iface->freq, NULL,
839 iface->hw_features,
840 iface->num_hw_features);
Hai Shaloma20dcd72022-02-04 13:43:00 -0800841 if (!pri_chan)
842 return 0;
Hai Shalomc3565922019-10-28 11:58:20 -0700843 hostapd_encode_edmg_chan(iface->conf->enable_edmg,
844 iface->conf->edmg_channel,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800845 pri_chan->chan,
Hai Shalomc3565922019-10-28 11:58:20 -0700846 &edmg);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800847 if (!(edmg.channels & BIT(pri_chan->chan - 1)))
Hai Shalomc3565922019-10-28 11:58:20 -0700848 return 0;
849
850 /* 60 GHz channels 1..6 */
851 for (i = 0; i < 6; i++) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700852 int freq = 56160 + 2160 * (i + 1);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800853
Hai Shalomc3565922019-10-28 11:58:20 -0700854 if (edmg.channels & BIT(i)) {
855 contiguous++;
856 num_of_enabled++;
857 } else {
858 contiguous = 0;
859 continue;
860 }
861
862 /* P802.11ay defines that the total number of subfields
863 * set to one does not exceed 4.
864 */
865 if (num_of_enabled > 4)
866 return 0;
867
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800868 if (!hostapd_is_usable_chan(iface, freq, 1))
Hai Shalomc3565922019-10-28 11:58:20 -0700869 return 0;
870
871 if (contiguous > max_contiguous)
872 max_contiguous = contiguous;
873 }
874
875 /* Check if the EDMG configuration is valid under the limitations
876 * of P802.11ay.
877 */
878 /* check bw_config against contiguous EDMG channels */
879 switch (edmg.bw_config) {
880 case EDMG_BW_CONFIG_4:
881 if (!max_contiguous)
882 return 0;
883 break;
884 case EDMG_BW_CONFIG_5:
885 if (max_contiguous < 2)
886 return 0;
887 break;
888 default:
889 return 0;
890 }
891
892 return 1;
893}
894
895
Sunil Ravi036cec52023-03-29 11:35:17 -0700896static bool hostapd_is_usable_punct_bitmap(struct hostapd_iface *iface)
897{
898#ifdef CONFIG_IEEE80211BE
899 struct hostapd_config *conf = iface->conf;
900 u8 bw, start_chan;
901
902 if (!conf->punct_bitmap)
903 return true;
904
905 if (!conf->ieee80211be) {
906 wpa_printf(MSG_ERROR,
907 "Currently RU puncturing is supported only if ieee80211be is enabled");
908 return false;
909 }
910
911 if (iface->freq >= 2412 && iface->freq <= 2484) {
912 wpa_printf(MSG_ERROR,
913 "RU puncturing not supported in 2.4 GHz");
914 return false;
915 }
916
917 switch (conf->eht_oper_chwidth) {
918 case 0:
919 wpa_printf(MSG_ERROR,
920 "RU puncturing is supported only in 80 MHz and 160 MHz");
921 return false;
922 case 1:
923 bw = 80;
924 start_chan = conf->eht_oper_centr_freq_seg0_idx - 6;
925 break;
926 case 2:
927 bw = 160;
928 start_chan = conf->eht_oper_centr_freq_seg0_idx - 14;
929 break;
930 default:
931 return false;
932 }
933
934 if (!is_punct_bitmap_valid(bw, (conf->channel - start_chan) / 4,
935 conf->punct_bitmap)) {
936 wpa_printf(MSG_ERROR, "Invalid puncturing bitmap");
937 return false;
938 }
939#endif /* CONFIG_IEEE80211BE */
940
941 return true;
942}
943
944
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700945static int hostapd_is_usable_chans(struct hostapd_iface *iface)
946{
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800947 int secondary_freq;
Hai Shalom74f70d42019-02-11 14:42:39 -0800948 struct hostapd_channel_data *pri_chan;
949
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800950 if (!iface->current_mode)
Hai Shalom74f70d42019-02-11 14:42:39 -0800951 return 0;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800952 pri_chan = hw_get_channel_freq(iface->current_mode->mode,
953 iface->freq, NULL,
954 iface->hw_features,
955 iface->num_hw_features);
956 if (!pri_chan) {
957 wpa_printf(MSG_ERROR, "Primary frequency not present");
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700958 return 0;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800959 }
960 if (!hostapd_is_usable_chan(iface, pri_chan->freq, 1)) {
961 wpa_printf(MSG_ERROR, "Primary frequency not allowed");
962 return 0;
963 }
Hai Shalomc3565922019-10-28 11:58:20 -0700964 if (!hostapd_is_usable_edmg(iface))
965 return 0;
966
Sunil Ravi036cec52023-03-29 11:35:17 -0700967 if (!hostapd_is_usable_punct_bitmap(iface))
968 return 0;
969
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700970 if (!iface->conf->secondary_channel)
971 return 1;
972
Hai Shalomfdcde762020-04-02 11:19:20 -0700973 if (hostapd_is_usable_chan(iface, iface->freq +
Hai Shaloma20dcd72022-02-04 13:43:00 -0800974 iface->conf->secondary_channel * 20, 0)) {
975 if (iface->conf->secondary_channel == 1 &&
976 (pri_chan->allowed_bw & HOSTAPD_CHAN_WIDTH_40P))
977 return 1;
978 if (iface->conf->secondary_channel == -1 &&
979 (pri_chan->allowed_bw & HOSTAPD_CHAN_WIDTH_40M))
980 return 1;
981 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700982 if (!iface->conf->ht40_plus_minus_allowed)
Hai Shalomfdcde762020-04-02 11:19:20 -0700983 return 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700984
985 /* Both HT40+ and HT40- are set, pick a valid secondary channel */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800986 secondary_freq = iface->freq + 20;
987 if (hostapd_is_usable_chan(iface, secondary_freq, 0) &&
Hai Shalom74f70d42019-02-11 14:42:39 -0800988 (pri_chan->allowed_bw & HOSTAPD_CHAN_WIDTH_40P)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700989 iface->conf->secondary_channel = 1;
990 return 1;
991 }
992
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800993 secondary_freq = iface->freq - 20;
994 if (hostapd_is_usable_chan(iface, secondary_freq, 0) &&
Hai Shalom74f70d42019-02-11 14:42:39 -0800995 (pri_chan->allowed_bw & HOSTAPD_CHAN_WIDTH_40M)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700996 iface->conf->secondary_channel = -1;
997 return 1;
998 }
999
1000 return 0;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001001}
1002
1003
Hai Shalomfdcde762020-04-02 11:19:20 -07001004static void hostapd_determine_mode(struct hostapd_iface *iface)
1005{
1006 int i;
1007 enum hostapd_hw_mode target_mode;
1008
1009 if (iface->current_mode ||
1010 iface->conf->hw_mode != HOSTAPD_MODE_IEEE80211ANY)
1011 return;
1012
1013 if (iface->freq < 4000)
1014 target_mode = HOSTAPD_MODE_IEEE80211G;
1015 else if (iface->freq > 50000)
1016 target_mode = HOSTAPD_MODE_IEEE80211AD;
1017 else
1018 target_mode = HOSTAPD_MODE_IEEE80211A;
1019
1020 for (i = 0; i < iface->num_hw_features; i++) {
1021 struct hostapd_hw_modes *mode;
1022
1023 mode = &iface->hw_features[i];
1024 if (mode->mode == target_mode) {
1025 iface->current_mode = mode;
1026 iface->conf->hw_mode = mode->mode;
1027 break;
1028 }
1029 }
1030
1031 if (!iface->current_mode)
1032 wpa_printf(MSG_ERROR, "ACS: Cannot decide mode");
1033}
1034
1035
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001036static enum hostapd_chan_status
1037hostapd_check_chans(struct hostapd_iface *iface)
1038{
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001039 if (iface->freq) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001040 hostapd_determine_mode(iface);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001041 if (hostapd_is_usable_chans(iface))
1042 return HOSTAPD_CHAN_VALID;
1043 else
1044 return HOSTAPD_CHAN_INVALID;
1045 }
1046
1047 /*
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001048 * The user set channel=0 or channel=acs_survey
1049 * which is used to trigger ACS.
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001050 */
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001051
1052 switch (acs_init(iface)) {
1053 case HOSTAPD_CHAN_ACS:
1054 return HOSTAPD_CHAN_ACS;
1055 case HOSTAPD_CHAN_VALID:
1056 case HOSTAPD_CHAN_INVALID:
1057 default:
1058 return HOSTAPD_CHAN_INVALID;
1059 }
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001060}
1061
1062
1063static void hostapd_notify_bad_chans(struct hostapd_iface *iface)
1064{
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07001065 if (!iface->current_mode) {
1066 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
1067 HOSTAPD_LEVEL_WARNING,
1068 "Hardware does not support configured mode");
1069 return;
1070 }
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001071 hostapd_logger(iface->bss[0], NULL,
1072 HOSTAPD_MODULE_IEEE80211,
1073 HOSTAPD_LEVEL_WARNING,
Hai Shalom60840252021-02-19 19:02:11 -08001074 "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 -07001075 iface->conf->channel,
Hai Shalom60840252021-02-19 19:02:11 -08001076 iface->freq, iface->conf->secondary_channel,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001077 iface->current_mode->mode,
1078 hostapd_hw_mode_txt(iface->current_mode->mode));
1079 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
1080 HOSTAPD_LEVEL_WARNING,
1081 "Hardware does not support configured channel");
1082}
1083
1084
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07001085int hostapd_acs_completed(struct hostapd_iface *iface, int err)
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001086{
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07001087 int ret = -1;
1088
1089 if (err)
1090 goto out;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001091
1092 switch (hostapd_check_chans(iface)) {
1093 case HOSTAPD_CHAN_VALID:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001094 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO,
1095 ACS_EVENT_COMPLETED "freq=%d channel=%d",
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001096 iface->freq, iface->conf->channel);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001097 break;
1098 case HOSTAPD_CHAN_ACS:
1099 wpa_printf(MSG_ERROR, "ACS error - reported complete, but no result available");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001100 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, ACS_EVENT_FAILED);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001101 hostapd_notify_bad_chans(iface);
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07001102 goto out;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001103 case HOSTAPD_CHAN_INVALID:
1104 default:
1105 wpa_printf(MSG_ERROR, "ACS picked unusable channels");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001106 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, ACS_EVENT_FAILED);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001107 hostapd_notify_bad_chans(iface);
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07001108 goto out;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001109 }
1110
1111 ret = hostapd_check_ht_capab(iface);
1112 if (ret < 0)
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07001113 goto out;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001114 if (ret == 1) {
1115 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback");
1116 return 0;
1117 }
1118
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07001119 ret = 0;
1120out:
1121 return hostapd_setup_interface_complete(iface, ret);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001122}
1123
1124
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001125/**
1126 * hostapd_select_hw_mode - Select the hardware mode
1127 * @iface: Pointer to interface data.
Jouni Malinen87fd2792011-05-16 18:35:42 +03001128 * Returns: 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001129 *
1130 * Sets up the hardware mode, channel, rates, and passive scanning
1131 * based on the configuration.
1132 */
1133int hostapd_select_hw_mode(struct hostapd_iface *iface)
1134{
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001135 int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001136
1137 if (iface->num_hw_features < 1)
1138 return -1;
1139
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001140 if ((iface->conf->hw_mode == HOSTAPD_MODE_IEEE80211G ||
Hai Shalom81f62d82019-07-22 12:10:00 -07001141 iface->conf->ieee80211n || iface->conf->ieee80211ac ||
Sunil8cd6f4d2022-06-28 18:40:46 +00001142 iface->conf->ieee80211ax || iface->conf->ieee80211be) &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001143 iface->conf->channel == 14) {
Sunil8cd6f4d2022-06-28 18:40:46 +00001144 wpa_printf(MSG_INFO, "Disable OFDM/HT/VHT/HE/EHT on channel 14");
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001145 iface->conf->hw_mode = HOSTAPD_MODE_IEEE80211B;
1146 iface->conf->ieee80211n = 0;
1147 iface->conf->ieee80211ac = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -07001148 iface->conf->ieee80211ax = 0;
Sunil8cd6f4d2022-06-28 18:40:46 +00001149 iface->conf->ieee80211be = 0;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001150 }
1151
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001152 iface->current_mode = NULL;
1153 for (i = 0; i < iface->num_hw_features; i++) {
1154 struct hostapd_hw_modes *mode = &iface->hw_features[i];
Hai Shalom60840252021-02-19 19:02:11 -08001155 int chan;
1156
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001157 if (mode->mode == iface->conf->hw_mode) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001158 if (iface->freq > 0 &&
Hai Shalom60840252021-02-19 19:02:11 -08001159 !hw_mode_get_channel(mode, iface->freq, &chan))
Hai Shalomc3565922019-10-28 11:58:20 -07001160 continue;
Hai Shalom60840252021-02-19 19:02:11 -08001161
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001162 iface->current_mode = mode;
1163 break;
1164 }
1165 }
1166
1167 if (iface->current_mode == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001168 if ((iface->drv_flags & WPA_DRIVER_FLAGS_ACS_OFFLOAD) &&
1169 (iface->drv_flags & WPA_DRIVER_FLAGS_SUPPORT_HW_MODE_ANY)) {
1170 wpa_printf(MSG_DEBUG,
1171 "Using offloaded hw_mode=any ACS");
1172 } else if (!(iface->drv_flags & WPA_DRIVER_FLAGS_ACS_OFFLOAD) &&
1173 iface->conf->hw_mode == HOSTAPD_MODE_IEEE80211ANY) {
1174 wpa_printf(MSG_DEBUG,
1175 "Using internal ACS for hw_mode=any");
1176 } else {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07001177 wpa_printf(MSG_ERROR,
1178 "Hardware does not support configured mode");
1179 hostapd_logger(iface->bss[0], NULL,
1180 HOSTAPD_MODULE_IEEE80211,
1181 HOSTAPD_LEVEL_WARNING,
1182 "Hardware does not support configured mode (%d) (hw_mode in hostapd.conf)",
1183 (int) iface->conf->hw_mode);
1184 return -2;
1185 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001186 }
1187
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001188 switch (hostapd_check_chans(iface)) {
1189 case HOSTAPD_CHAN_VALID:
1190 return 0;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001191 case HOSTAPD_CHAN_ACS: /* ACS will run and later complete */
1192 return 1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001193 case HOSTAPD_CHAN_INVALID:
1194 default:
1195 hostapd_notify_bad_chans(iface);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001196 return -3;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001197 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001198}
1199
1200
1201const char * hostapd_hw_mode_txt(int mode)
1202{
1203 switch (mode) {
1204 case HOSTAPD_MODE_IEEE80211A:
1205 return "IEEE 802.11a";
1206 case HOSTAPD_MODE_IEEE80211B:
1207 return "IEEE 802.11b";
1208 case HOSTAPD_MODE_IEEE80211G:
1209 return "IEEE 802.11g";
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001210 case HOSTAPD_MODE_IEEE80211AD:
1211 return "IEEE 802.11ad";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001212 default:
1213 return "UNKNOWN";
1214 }
1215}
1216
1217
1218int hostapd_hw_get_freq(struct hostapd_data *hapd, int chan)
1219{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001220 return hw_get_freq(hapd->iface->current_mode, chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001221}
1222
1223
1224int hostapd_hw_get_channel(struct hostapd_data *hapd, int freq)
1225{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001226 int i, channel;
1227 struct hostapd_hw_modes *mode;
1228
Hai Shalom1dc4d202019-04-29 16:22:27 -07001229 if (hapd->iface->current_mode) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001230 channel = hw_get_chan(hapd->iface->current_mode->mode, freq,
1231 hapd->iface->hw_features,
1232 hapd->iface->num_hw_features);
Hai Shalom1dc4d202019-04-29 16:22:27 -07001233 if (channel)
1234 return channel;
1235 }
1236
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001237 /* Check other available modes since the channel list for the current
1238 * mode did not include the specified frequency. */
Hai Shalom1dc4d202019-04-29 16:22:27 -07001239 if (!hapd->iface->hw_features)
1240 return 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001241 for (i = 0; i < hapd->iface->num_hw_features; i++) {
1242 mode = &hapd->iface->hw_features[i];
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001243 channel = hw_get_chan(mode->mode, freq,
1244 hapd->iface->hw_features,
1245 hapd->iface->num_hw_features);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001246 if (channel)
1247 return channel;
1248 }
1249 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001250}
Hai Shalomfdcde762020-04-02 11:19:20 -07001251
1252
1253int hostapd_hw_skip_mode(struct hostapd_iface *iface,
1254 struct hostapd_hw_modes *mode)
1255{
1256 int i;
1257
1258 if (iface->current_mode)
1259 return mode != iface->current_mode;
1260 if (mode->mode != HOSTAPD_MODE_IEEE80211B)
1261 return 0;
1262 for (i = 0; i < iface->num_hw_features; i++) {
1263 if (iface->hw_features[i].mode == HOSTAPD_MODE_IEEE80211G)
1264 return 1;
1265 }
1266 return 0;
1267}