blob: f6e69030d7670f5c9913f56b6868f99c5a844820 [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;
316 int res;
317
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
352 res = ieee80211n_allowed_ht40_channel_pair(iface);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700353 if (!res) {
354 iface->conf->secondary_channel = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -0700355 hostapd_set_oper_centr_freq_seg0_idx(iface->conf, 0);
356 hostapd_set_oper_centr_freq_seg1_idx(iface->conf, 0);
357 hostapd_set_oper_chwidth(iface->conf, CHANWIDTH_USE_HT);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800358 res = 1;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700359 wpa_printf(MSG_INFO, "Fallback to 20 MHz");
360 }
361
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700362 hostapd_setup_interface_complete(iface, !res);
363}
364
365
Dmitry Shmidt04949592012-07-19 12:16:46 -0700366static void ieee80211n_scan_channels_2g4(struct hostapd_iface *iface,
367 struct wpa_driver_scan_params *params)
368{
369 /* Scan only the affected frequency range */
370 int pri_freq, sec_freq;
371 int affected_start, affected_end;
372 int i, pos;
373 struct hostapd_hw_modes *mode;
374
375 if (iface->current_mode == NULL)
376 return;
377
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800378 pri_freq = iface->freq;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700379 if (iface->conf->secondary_channel > 0)
380 sec_freq = pri_freq + 20;
381 else
382 sec_freq = pri_freq - 20;
Dmitry Shmidt41712582015-06-29 11:02:15 -0700383 /*
384 * Note: Need to find the PRI channel also in cases where the affected
385 * channel is the SEC channel of a 40 MHz BSS, so need to include the
386 * scanning coverage here to be 40 MHz from the center frequency.
387 */
388 affected_start = (pri_freq + sec_freq) / 2 - 40;
389 affected_end = (pri_freq + sec_freq) / 2 + 40;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700390 wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
391 affected_start, affected_end);
392
393 mode = iface->current_mode;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700394 params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
Dmitry Shmidt04949592012-07-19 12:16:46 -0700395 if (params->freqs == NULL)
396 return;
397 pos = 0;
398
399 for (i = 0; i < mode->num_channels; i++) {
400 struct hostapd_channel_data *chan = &mode->channels[i];
401 if (chan->flag & HOSTAPD_CHAN_DISABLED)
402 continue;
403 if (chan->freq < affected_start ||
404 chan->freq > affected_end)
405 continue;
406 params->freqs[pos++] = chan->freq;
407 }
408}
409
410
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800411static void ieee80211n_scan_channels_5g(struct hostapd_iface *iface,
412 struct wpa_driver_scan_params *params)
413{
414 /* Scan only the affected frequency range */
415 int pri_freq;
416 int affected_start, affected_end;
417 int i, pos;
418 struct hostapd_hw_modes *mode;
419
420 if (iface->current_mode == NULL)
421 return;
422
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800423 pri_freq = iface->freq;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800424 if (iface->conf->secondary_channel > 0) {
425 affected_start = pri_freq - 10;
426 affected_end = pri_freq + 30;
427 } else {
428 affected_start = pri_freq - 30;
429 affected_end = pri_freq + 10;
430 }
431 wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
432 affected_start, affected_end);
433
434 mode = iface->current_mode;
435 params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
436 if (params->freqs == NULL)
437 return;
438 pos = 0;
439
440 for (i = 0; i < mode->num_channels; i++) {
441 struct hostapd_channel_data *chan = &mode->channels[i];
442 if (chan->flag & HOSTAPD_CHAN_DISABLED)
443 continue;
444 if (chan->freq < affected_start ||
445 chan->freq > affected_end)
446 continue;
447 params->freqs[pos++] = chan->freq;
448 }
449}
450
451
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700452static void ap_ht40_scan_retry(void *eloop_data, void *user_data)
453{
454#define HT2040_COEX_SCAN_RETRY 15
455 struct hostapd_iface *iface = eloop_data;
456 struct wpa_driver_scan_params params;
457 int ret;
458
459 os_memset(&params, 0, sizeof(params));
460 if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
461 ieee80211n_scan_channels_2g4(iface, &params);
462 else
463 ieee80211n_scan_channels_5g(iface, &params);
464
465 ret = hostapd_driver_scan(iface->bss[0], &params);
466 iface->num_ht40_scan_tries++;
467 os_free(params.freqs);
468
469 if (ret == -EBUSY &&
470 iface->num_ht40_scan_tries < HT2040_COEX_SCAN_RETRY) {
471 wpa_printf(MSG_ERROR,
472 "Failed to request a scan of neighboring BSSes ret=%d (%s) - try to scan again (attempt %d)",
473 ret, strerror(-ret), iface->num_ht40_scan_tries);
474 eloop_register_timeout(1, 0, ap_ht40_scan_retry, iface, NULL);
475 return;
476 }
477
478 if (ret == 0) {
479 iface->scan_cb = ieee80211n_check_scan;
480 return;
481 }
482
483 wpa_printf(MSG_DEBUG,
484 "Failed to request a scan in device, bringing up in HT20 mode");
485 iface->conf->secondary_channel = 0;
486 iface->conf->ht_capab &= ~HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
487 hostapd_setup_interface_complete(iface, 0);
488}
489
490
491void hostapd_stop_setup_timers(struct hostapd_iface *iface)
492{
493 eloop_cancel_timeout(ap_ht40_scan_retry, iface, NULL);
494}
495
496
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700497static int ieee80211n_check_40mhz(struct hostapd_iface *iface)
498{
499 struct wpa_driver_scan_params params;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700500 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700501
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800502 /* Check that HT40 is used and PRI / SEC switch is allowed */
503 if (!iface->conf->secondary_channel || iface->conf->no_pri_sec_switch)
504 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700505
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800506 hostapd_set_state(iface, HAPD_IFACE_HT_SCAN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700507 wpa_printf(MSG_DEBUG, "Scan for neighboring BSSes prior to enabling "
508 "40 MHz channel");
509 os_memset(&params, 0, sizeof(params));
Dmitry Shmidt04949592012-07-19 12:16:46 -0700510 if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
511 ieee80211n_scan_channels_2g4(iface, &params);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800512 else
513 ieee80211n_scan_channels_5g(iface, &params);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700514
515 ret = hostapd_driver_scan(iface->bss[0], &params);
516 os_free(params.freqs);
517
518 if (ret == -EBUSY) {
519 wpa_printf(MSG_ERROR,
520 "Failed to request a scan of neighboring BSSes ret=%d (%s) - try to scan again",
521 ret, strerror(-ret));
522 iface->num_ht40_scan_tries = 1;
523 eloop_cancel_timeout(ap_ht40_scan_retry, iface, NULL);
524 eloop_register_timeout(1, 0, ap_ht40_scan_retry, iface, NULL);
525 return 1;
526 }
527
528 if (ret < 0) {
529 wpa_printf(MSG_ERROR,
530 "Failed to request a scan of neighboring BSSes ret=%d (%s)",
531 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700532 return -1;
533 }
534
535 iface->scan_cb = ieee80211n_check_scan;
536 return 1;
537}
538
539
540static int ieee80211n_supported_ht_capab(struct hostapd_iface *iface)
541{
542 u16 hw = iface->current_mode->ht_capab;
543 u16 conf = iface->conf->ht_capab;
544
545 if ((conf & HT_CAP_INFO_LDPC_CODING_CAP) &&
546 !(hw & HT_CAP_INFO_LDPC_CODING_CAP)) {
547 wpa_printf(MSG_ERROR, "Driver does not support configured "
548 "HT capability [LDPC]");
549 return 0;
550 }
551
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700552 /*
553 * Driver ACS chosen channel may not be HT40 due to internal driver
554 * restrictions.
555 */
556 if (!iface->conf->acs && (conf & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700557 !(hw & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
558 wpa_printf(MSG_ERROR, "Driver does not support configured "
559 "HT capability [HT40*]");
560 return 0;
561 }
562
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700563 if ((conf & HT_CAP_INFO_GREEN_FIELD) &&
564 !(hw & HT_CAP_INFO_GREEN_FIELD)) {
565 wpa_printf(MSG_ERROR, "Driver does not support configured "
566 "HT capability [GF]");
567 return 0;
568 }
569
570 if ((conf & HT_CAP_INFO_SHORT_GI20MHZ) &&
571 !(hw & HT_CAP_INFO_SHORT_GI20MHZ)) {
572 wpa_printf(MSG_ERROR, "Driver does not support configured "
573 "HT capability [SHORT-GI-20]");
574 return 0;
575 }
576
577 if ((conf & HT_CAP_INFO_SHORT_GI40MHZ) &&
578 !(hw & HT_CAP_INFO_SHORT_GI40MHZ)) {
579 wpa_printf(MSG_ERROR, "Driver does not support configured "
580 "HT capability [SHORT-GI-40]");
581 return 0;
582 }
583
584 if ((conf & HT_CAP_INFO_TX_STBC) && !(hw & HT_CAP_INFO_TX_STBC)) {
585 wpa_printf(MSG_ERROR, "Driver does not support configured "
586 "HT capability [TX-STBC]");
587 return 0;
588 }
589
590 if ((conf & HT_CAP_INFO_RX_STBC_MASK) >
591 (hw & HT_CAP_INFO_RX_STBC_MASK)) {
592 wpa_printf(MSG_ERROR, "Driver does not support configured "
593 "HT capability [RX-STBC*]");
594 return 0;
595 }
596
597 if ((conf & HT_CAP_INFO_DELAYED_BA) &&
598 !(hw & HT_CAP_INFO_DELAYED_BA)) {
599 wpa_printf(MSG_ERROR, "Driver does not support configured "
600 "HT capability [DELAYED-BA]");
601 return 0;
602 }
603
604 if ((conf & HT_CAP_INFO_MAX_AMSDU_SIZE) &&
605 !(hw & HT_CAP_INFO_MAX_AMSDU_SIZE)) {
606 wpa_printf(MSG_ERROR, "Driver does not support configured "
607 "HT capability [MAX-AMSDU-7935]");
608 return 0;
609 }
610
611 if ((conf & HT_CAP_INFO_DSSS_CCK40MHZ) &&
612 !(hw & HT_CAP_INFO_DSSS_CCK40MHZ)) {
613 wpa_printf(MSG_ERROR, "Driver does not support configured "
614 "HT capability [DSSS_CCK-40]");
615 return 0;
616 }
617
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700618 if ((conf & HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT) &&
619 !(hw & HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT)) {
620 wpa_printf(MSG_ERROR, "Driver does not support configured "
621 "HT capability [LSIG-TXOP-PROT]");
622 return 0;
623 }
624
625 return 1;
626}
627
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700628
629#ifdef CONFIG_IEEE80211AC
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700630static int ieee80211ac_supported_vht_capab(struct hostapd_iface *iface)
631{
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800632 struct hostapd_hw_modes *mode = iface->current_mode;
633 u32 hw = mode->vht_capab;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700634 u32 conf = iface->conf->vht_capab;
635
636 wpa_printf(MSG_DEBUG, "hw vht capab: 0x%x, conf vht capab: 0x%x",
637 hw, conf);
638
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800639 if (mode->mode == HOSTAPD_MODE_IEEE80211G &&
640 iface->conf->bss[0]->vendor_vht &&
641 mode->vht_capab == 0 && iface->hw_features) {
642 int i;
643
644 for (i = 0; i < iface->num_hw_features; i++) {
645 if (iface->hw_features[i].mode ==
646 HOSTAPD_MODE_IEEE80211A) {
647 mode = &iface->hw_features[i];
648 hw = mode->vht_capab;
649 wpa_printf(MSG_DEBUG,
650 "update hw vht capab based on 5 GHz band: 0x%x",
651 hw);
652 break;
653 }
654 }
655 }
656
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800657 return ieee80211ac_cap_check(hw, conf);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700658}
659#endif /* CONFIG_IEEE80211AC */
660
Hai Shalom81f62d82019-07-22 12:10:00 -0700661
662#ifdef CONFIG_IEEE80211AX
663static int ieee80211ax_supported_he_capab(struct hostapd_iface *iface)
664{
665 return 1;
666}
667#endif /* CONFIG_IEEE80211AX */
668
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700669
670int hostapd_check_ht_capab(struct hostapd_iface *iface)
671{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700672 int ret;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800673
674 if (is_6ghz_freq(iface->freq))
675 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700676 if (!iface->conf->ieee80211n)
677 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800678
679 if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211B &&
680 iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G &&
681 (iface->conf->ht_capab & HT_CAP_INFO_DSSS_CCK40MHZ)) {
682 wpa_printf(MSG_DEBUG,
683 "Disable HT capability [DSSS_CCK-40] on 5 GHz band");
684 iface->conf->ht_capab &= ~HT_CAP_INFO_DSSS_CCK40MHZ;
685 }
686
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700687 if (!ieee80211n_supported_ht_capab(iface))
688 return -1;
Hai Shalom81f62d82019-07-22 12:10:00 -0700689#ifdef CONFIG_IEEE80211AX
690 if (iface->conf->ieee80211ax &&
691 !ieee80211ax_supported_he_capab(iface))
692 return -1;
693#endif /* CONFIG_IEEE80211AX */
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700694#ifdef CONFIG_IEEE80211AC
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800695 if (iface->conf->ieee80211ac &&
696 !ieee80211ac_supported_vht_capab(iface))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700697 return -1;
698#endif /* CONFIG_IEEE80211AC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700699 ret = ieee80211n_check_40mhz(iface);
700 if (ret)
701 return ret;
702 if (!ieee80211n_allowed_ht40_channel_pair(iface))
703 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700704
705 return 0;
706}
707
708
Hai Shalomc3565922019-10-28 11:58:20 -0700709int hostapd_check_edmg_capab(struct hostapd_iface *iface)
710{
711 struct hostapd_hw_modes *mode = iface->hw_features;
712 struct ieee80211_edmg_config edmg;
713
714 if (!iface->conf->enable_edmg)
715 return 0;
716
717 hostapd_encode_edmg_chan(iface->conf->enable_edmg,
718 iface->conf->edmg_channel,
719 iface->conf->channel,
720 &edmg);
721
722 if (mode->edmg.channels && ieee802_edmg_is_allowed(mode->edmg, edmg))
723 return 0;
724
725 wpa_printf(MSG_WARNING, "Requested EDMG configuration is not valid");
726 wpa_printf(MSG_INFO, "EDMG capab: channels 0x%x, bw_config %d",
727 mode->edmg.channels, mode->edmg.bw_config);
728 wpa_printf(MSG_INFO,
729 "Requested EDMG configuration: channels 0x%x, bw_config %d",
730 edmg.channels, edmg.bw_config);
731 return -1;
732}
733
734
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700735static int hostapd_is_usable_chan(struct hostapd_iface *iface,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800736 int frequency, int primary)
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700737{
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700738 struct hostapd_channel_data *chan;
739
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700740 if (!iface->current_mode)
741 return 0;
742
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800743 chan = hw_get_channel_freq(iface->current_mode->mode, frequency, NULL,
744 iface->hw_features, iface->num_hw_features);
Hai Shalom74f70d42019-02-11 14:42:39 -0800745 if (!chan)
746 return 0;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700747
Hai Shalom74f70d42019-02-11 14:42:39 -0800748 if ((primary && chan_pri_allowed(chan)) ||
749 (!primary && !(chan->flag & HOSTAPD_CHAN_DISABLED)))
750 return 1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700751
Hai Shalom74f70d42019-02-11 14:42:39 -0800752 wpa_printf(MSG_INFO,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800753 "Frequency %d (%s) not allowed for AP mode, flags: 0x%x%s%s",
754 frequency, primary ? "primary" : "secondary",
Hai Shalom74f70d42019-02-11 14:42:39 -0800755 chan->flag,
756 chan->flag & HOSTAPD_CHAN_NO_IR ? " NO-IR" : "",
757 chan->flag & HOSTAPD_CHAN_RADAR ? " RADAR" : "");
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700758 return 0;
759}
760
761
Hai Shalomc3565922019-10-28 11:58:20 -0700762static int hostapd_is_usable_edmg(struct hostapd_iface *iface)
763{
764 int i, contiguous = 0;
765 int num_of_enabled = 0;
766 int max_contiguous = 0;
767 struct ieee80211_edmg_config edmg;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800768 struct hostapd_channel_data *pri_chan;
Hai Shalomc3565922019-10-28 11:58:20 -0700769
770 if (!iface->conf->enable_edmg)
771 return 1;
772
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800773 if (!iface->current_mode)
774 return 0;
775 pri_chan = hw_get_channel_freq(iface->current_mode->mode,
776 iface->freq, NULL,
777 iface->hw_features,
778 iface->num_hw_features);
Hai Shalomc3565922019-10-28 11:58:20 -0700779 hostapd_encode_edmg_chan(iface->conf->enable_edmg,
780 iface->conf->edmg_channel,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800781 pri_chan->chan,
Hai Shalomc3565922019-10-28 11:58:20 -0700782 &edmg);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800783 if (!(edmg.channels & BIT(pri_chan->chan - 1)))
Hai Shalomc3565922019-10-28 11:58:20 -0700784 return 0;
785
786 /* 60 GHz channels 1..6 */
787 for (i = 0; i < 6; i++) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700788 int freq = 56160 + 2160 * (i + 1);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800789
Hai Shalomc3565922019-10-28 11:58:20 -0700790 if (edmg.channels & BIT(i)) {
791 contiguous++;
792 num_of_enabled++;
793 } else {
794 contiguous = 0;
795 continue;
796 }
797
798 /* P802.11ay defines that the total number of subfields
799 * set to one does not exceed 4.
800 */
801 if (num_of_enabled > 4)
802 return 0;
803
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800804 if (!hostapd_is_usable_chan(iface, freq, 1))
Hai Shalomc3565922019-10-28 11:58:20 -0700805 return 0;
806
807 if (contiguous > max_contiguous)
808 max_contiguous = contiguous;
809 }
810
811 /* Check if the EDMG configuration is valid under the limitations
812 * of P802.11ay.
813 */
814 /* check bw_config against contiguous EDMG channels */
815 switch (edmg.bw_config) {
816 case EDMG_BW_CONFIG_4:
817 if (!max_contiguous)
818 return 0;
819 break;
820 case EDMG_BW_CONFIG_5:
821 if (max_contiguous < 2)
822 return 0;
823 break;
824 default:
825 return 0;
826 }
827
828 return 1;
829}
830
831
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700832static int hostapd_is_usable_chans(struct hostapd_iface *iface)
833{
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800834 int secondary_freq;
Hai Shalom74f70d42019-02-11 14:42:39 -0800835 struct hostapd_channel_data *pri_chan;
836
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800837 if (!iface->current_mode)
Hai Shalom74f70d42019-02-11 14:42:39 -0800838 return 0;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800839 pri_chan = hw_get_channel_freq(iface->current_mode->mode,
840 iface->freq, NULL,
841 iface->hw_features,
842 iface->num_hw_features);
843 if (!pri_chan) {
844 wpa_printf(MSG_ERROR, "Primary frequency not present");
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700845 return 0;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800846 }
847 if (!hostapd_is_usable_chan(iface, pri_chan->freq, 1)) {
848 wpa_printf(MSG_ERROR, "Primary frequency not allowed");
849 return 0;
850 }
Hai Shalomc3565922019-10-28 11:58:20 -0700851 if (!hostapd_is_usable_edmg(iface))
852 return 0;
853
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700854 if (!iface->conf->secondary_channel)
855 return 1;
856
Hai Shalomfdcde762020-04-02 11:19:20 -0700857 if (hostapd_is_usable_chan(iface, iface->freq +
858 iface->conf->secondary_channel * 20, 0))
859 return 1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700860 if (!iface->conf->ht40_plus_minus_allowed)
Hai Shalomfdcde762020-04-02 11:19:20 -0700861 return 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700862
863 /* Both HT40+ and HT40- are set, pick a valid secondary channel */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800864 secondary_freq = iface->freq + 20;
865 if (hostapd_is_usable_chan(iface, secondary_freq, 0) &&
Hai Shalom74f70d42019-02-11 14:42:39 -0800866 (pri_chan->allowed_bw & HOSTAPD_CHAN_WIDTH_40P)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700867 iface->conf->secondary_channel = 1;
868 return 1;
869 }
870
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800871 secondary_freq = iface->freq - 20;
872 if (hostapd_is_usable_chan(iface, secondary_freq, 0) &&
Hai Shalom74f70d42019-02-11 14:42:39 -0800873 (pri_chan->allowed_bw & HOSTAPD_CHAN_WIDTH_40M)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700874 iface->conf->secondary_channel = -1;
875 return 1;
876 }
877
878 return 0;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700879}
880
881
Hai Shalomfdcde762020-04-02 11:19:20 -0700882static void hostapd_determine_mode(struct hostapd_iface *iface)
883{
884 int i;
885 enum hostapd_hw_mode target_mode;
886
887 if (iface->current_mode ||
888 iface->conf->hw_mode != HOSTAPD_MODE_IEEE80211ANY)
889 return;
890
891 if (iface->freq < 4000)
892 target_mode = HOSTAPD_MODE_IEEE80211G;
893 else if (iface->freq > 50000)
894 target_mode = HOSTAPD_MODE_IEEE80211AD;
895 else
896 target_mode = HOSTAPD_MODE_IEEE80211A;
897
898 for (i = 0; i < iface->num_hw_features; i++) {
899 struct hostapd_hw_modes *mode;
900
901 mode = &iface->hw_features[i];
902 if (mode->mode == target_mode) {
903 iface->current_mode = mode;
904 iface->conf->hw_mode = mode->mode;
905 break;
906 }
907 }
908
909 if (!iface->current_mode)
910 wpa_printf(MSG_ERROR, "ACS: Cannot decide mode");
911}
912
913
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700914static enum hostapd_chan_status
915hostapd_check_chans(struct hostapd_iface *iface)
916{
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800917 if (iface->freq) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700918 hostapd_determine_mode(iface);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700919 if (hostapd_is_usable_chans(iface))
920 return HOSTAPD_CHAN_VALID;
921 else
922 return HOSTAPD_CHAN_INVALID;
923 }
924
925 /*
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700926 * The user set channel=0 or channel=acs_survey
927 * which is used to trigger ACS.
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700928 */
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700929
930 switch (acs_init(iface)) {
931 case HOSTAPD_CHAN_ACS:
932 return HOSTAPD_CHAN_ACS;
933 case HOSTAPD_CHAN_VALID:
934 case HOSTAPD_CHAN_INVALID:
935 default:
936 return HOSTAPD_CHAN_INVALID;
937 }
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700938}
939
940
941static void hostapd_notify_bad_chans(struct hostapd_iface *iface)
942{
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700943 if (!iface->current_mode) {
944 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
945 HOSTAPD_LEVEL_WARNING,
946 "Hardware does not support configured mode");
947 return;
948 }
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700949 hostapd_logger(iface->bss[0], NULL,
950 HOSTAPD_MODULE_IEEE80211,
951 HOSTAPD_LEVEL_WARNING,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800952 "Configured channel (%d) or frequency (%d) not found from the channel list of the current mode (%d) %s",
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700953 iface->conf->channel,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800954 iface->freq,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700955 iface->current_mode->mode,
956 hostapd_hw_mode_txt(iface->current_mode->mode));
957 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
958 HOSTAPD_LEVEL_WARNING,
959 "Hardware does not support configured channel");
960}
961
962
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700963int hostapd_acs_completed(struct hostapd_iface *iface, int err)
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700964{
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700965 int ret = -1;
966
967 if (err)
968 goto out;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700969
970 switch (hostapd_check_chans(iface)) {
971 case HOSTAPD_CHAN_VALID:
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800972 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO,
973 ACS_EVENT_COMPLETED "freq=%d channel=%d",
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800974 iface->freq, iface->conf->channel);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700975 break;
976 case HOSTAPD_CHAN_ACS:
977 wpa_printf(MSG_ERROR, "ACS error - reported complete, but no result available");
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800978 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, ACS_EVENT_FAILED);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700979 hostapd_notify_bad_chans(iface);
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700980 goto out;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700981 case HOSTAPD_CHAN_INVALID:
982 default:
983 wpa_printf(MSG_ERROR, "ACS picked unusable channels");
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800984 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, ACS_EVENT_FAILED);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700985 hostapd_notify_bad_chans(iface);
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700986 goto out;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700987 }
988
989 ret = hostapd_check_ht_capab(iface);
990 if (ret < 0)
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700991 goto out;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700992 if (ret == 1) {
993 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback");
994 return 0;
995 }
996
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700997 ret = 0;
998out:
999 return hostapd_setup_interface_complete(iface, ret);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001000}
1001
1002
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001003/**
1004 * hostapd_select_hw_mode - Select the hardware mode
1005 * @iface: Pointer to interface data.
Jouni Malinen87fd2792011-05-16 18:35:42 +03001006 * Returns: 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001007 *
1008 * Sets up the hardware mode, channel, rates, and passive scanning
1009 * based on the configuration.
1010 */
1011int hostapd_select_hw_mode(struct hostapd_iface *iface)
1012{
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001013 int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001014
1015 if (iface->num_hw_features < 1)
1016 return -1;
1017
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001018 if ((iface->conf->hw_mode == HOSTAPD_MODE_IEEE80211G ||
Hai Shalom81f62d82019-07-22 12:10:00 -07001019 iface->conf->ieee80211n || iface->conf->ieee80211ac ||
1020 iface->conf->ieee80211ax) &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001021 iface->conf->channel == 14) {
Hai Shalom81f62d82019-07-22 12:10:00 -07001022 wpa_printf(MSG_INFO, "Disable OFDM/HT/VHT/HE on channel 14");
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001023 iface->conf->hw_mode = HOSTAPD_MODE_IEEE80211B;
1024 iface->conf->ieee80211n = 0;
1025 iface->conf->ieee80211ac = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -07001026 iface->conf->ieee80211ax = 0;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001027 }
1028
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001029 iface->current_mode = NULL;
1030 for (i = 0; i < iface->num_hw_features; i++) {
1031 struct hostapd_hw_modes *mode = &iface->hw_features[i];
1032 if (mode->mode == iface->conf->hw_mode) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001033 if (iface->freq > 0 &&
1034 !hw_get_chan(mode->mode, iface->freq,
1035 iface->hw_features,
1036 iface->num_hw_features))
Hai Shalomc3565922019-10-28 11:58:20 -07001037 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001038 iface->current_mode = mode;
1039 break;
1040 }
1041 }
1042
1043 if (iface->current_mode == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001044 if ((iface->drv_flags & WPA_DRIVER_FLAGS_ACS_OFFLOAD) &&
1045 (iface->drv_flags & WPA_DRIVER_FLAGS_SUPPORT_HW_MODE_ANY)) {
1046 wpa_printf(MSG_DEBUG,
1047 "Using offloaded hw_mode=any ACS");
1048 } else if (!(iface->drv_flags & WPA_DRIVER_FLAGS_ACS_OFFLOAD) &&
1049 iface->conf->hw_mode == HOSTAPD_MODE_IEEE80211ANY) {
1050 wpa_printf(MSG_DEBUG,
1051 "Using internal ACS for hw_mode=any");
1052 } else {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07001053 wpa_printf(MSG_ERROR,
1054 "Hardware does not support configured mode");
1055 hostapd_logger(iface->bss[0], NULL,
1056 HOSTAPD_MODULE_IEEE80211,
1057 HOSTAPD_LEVEL_WARNING,
1058 "Hardware does not support configured mode (%d) (hw_mode in hostapd.conf)",
1059 (int) iface->conf->hw_mode);
1060 return -2;
1061 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001062 }
1063
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001064 switch (hostapd_check_chans(iface)) {
1065 case HOSTAPD_CHAN_VALID:
1066 return 0;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001067 case HOSTAPD_CHAN_ACS: /* ACS will run and later complete */
1068 return 1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001069 case HOSTAPD_CHAN_INVALID:
1070 default:
1071 hostapd_notify_bad_chans(iface);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001072 return -3;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001073 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001074}
1075
1076
1077const char * hostapd_hw_mode_txt(int mode)
1078{
1079 switch (mode) {
1080 case HOSTAPD_MODE_IEEE80211A:
1081 return "IEEE 802.11a";
1082 case HOSTAPD_MODE_IEEE80211B:
1083 return "IEEE 802.11b";
1084 case HOSTAPD_MODE_IEEE80211G:
1085 return "IEEE 802.11g";
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001086 case HOSTAPD_MODE_IEEE80211AD:
1087 return "IEEE 802.11ad";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001088 default:
1089 return "UNKNOWN";
1090 }
1091}
1092
1093
1094int hostapd_hw_get_freq(struct hostapd_data *hapd, int chan)
1095{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001096 return hw_get_freq(hapd->iface->current_mode, chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001097}
1098
1099
1100int hostapd_hw_get_channel(struct hostapd_data *hapd, int freq)
1101{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001102 int i, channel;
1103 struct hostapd_hw_modes *mode;
1104
Hai Shalom1dc4d202019-04-29 16:22:27 -07001105 if (hapd->iface->current_mode) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001106 channel = hw_get_chan(hapd->iface->current_mode->mode, freq,
1107 hapd->iface->hw_features,
1108 hapd->iface->num_hw_features);
Hai Shalom1dc4d202019-04-29 16:22:27 -07001109 if (channel)
1110 return channel;
1111 }
1112
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001113 /* Check other available modes since the channel list for the current
1114 * mode did not include the specified frequency. */
Hai Shalom1dc4d202019-04-29 16:22:27 -07001115 if (!hapd->iface->hw_features)
1116 return 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001117 for (i = 0; i < hapd->iface->num_hw_features; i++) {
1118 mode = &hapd->iface->hw_features[i];
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001119 channel = hw_get_chan(mode->mode, freq,
1120 hapd->iface->hw_features,
1121 hapd->iface->num_hw_features);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001122 if (channel)
1123 return channel;
1124 }
1125 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001126}
Hai Shalomfdcde762020-04-02 11:19:20 -07001127
1128
1129int hostapd_hw_skip_mode(struct hostapd_iface *iface,
1130 struct hostapd_hw_modes *mode)
1131{
1132 int i;
1133
1134 if (iface->current_mode)
1135 return mode != iface->current_mode;
1136 if (mode->mode != HOSTAPD_MODE_IEEE80211B)
1137 return 0;
1138 for (i = 0; i < iface->num_hw_features; i++) {
1139 if (iface->hw_features[i].mode == HOSTAPD_MODE_IEEE80211G)
1140 return 1;
1141 }
1142 return 0;
1143}