blob: 5279abca1f1faef03f4f84126f40a17f7466e32d [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
227#ifdef CONFIG_IEEE80211N
228static int ieee80211n_allowed_ht40_channel_pair(struct hostapd_iface *iface)
229{
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800230 int pri_chan, sec_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700231
232 if (!iface->conf->secondary_channel)
233 return 1; /* HT40 not used */
234
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800235 pri_chan = iface->conf->channel;
236 sec_chan = pri_chan + iface->conf->secondary_channel * 4;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700237
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800238 return allowed_ht40_channel_pair(iface->current_mode, pri_chan,
239 sec_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700240}
241
242
243static void ieee80211n_switch_pri_sec(struct hostapd_iface *iface)
244{
245 if (iface->conf->secondary_channel > 0) {
246 iface->conf->channel += 4;
247 iface->conf->secondary_channel = -1;
248 } else {
249 iface->conf->channel -= 4;
250 iface->conf->secondary_channel = 1;
251 }
252}
253
254
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700255static int ieee80211n_check_40mhz_5g(struct hostapd_iface *iface,
256 struct wpa_scan_results *scan_res)
257{
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800258 int pri_chan, sec_chan;
259 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700260
261 pri_chan = iface->conf->channel;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700262 sec_chan = pri_chan + iface->conf->secondary_channel * 4;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700263
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800264 res = check_40mhz_5g(iface->current_mode, scan_res, pri_chan, sec_chan);
265
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800266 if (res == 2) {
267 if (iface->conf->no_pri_sec_switch) {
268 wpa_printf(MSG_DEBUG,
269 "Cannot switch PRI/SEC channels due to local constraint");
270 } else {
271 ieee80211n_switch_pri_sec(iface);
272 }
273 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700274
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800275 return !!res;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -0700276}
277
278
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700279static int ieee80211n_check_40mhz_2g4(struct hostapd_iface *iface,
280 struct wpa_scan_results *scan_res)
281{
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800282 int pri_chan, sec_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700283
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800284 pri_chan = iface->conf->channel;
285 sec_chan = pri_chan + iface->conf->secondary_channel * 4;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700286
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800287 return check_40mhz_2g4(iface->current_mode, scan_res, pri_chan,
288 sec_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700289}
290
291
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700292static void ieee80211n_check_scan(struct hostapd_iface *iface)
293{
294 struct wpa_scan_results *scan_res;
295 int oper40;
296 int res;
297
298 /* Check list of neighboring BSSes (from scan) to see whether 40 MHz is
Dmitry Shmidt04949592012-07-19 12:16:46 -0700299 * allowed per IEEE Std 802.11-2012, 10.15.3.2 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700300
301 iface->scan_cb = NULL;
302
303 scan_res = hostapd_driver_get_scan_results(iface->bss[0]);
304 if (scan_res == NULL) {
305 hostapd_setup_interface_complete(iface, 1);
306 return;
307 }
308
309 if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A)
310 oper40 = ieee80211n_check_40mhz_5g(iface, scan_res);
311 else
312 oper40 = ieee80211n_check_40mhz_2g4(iface, scan_res);
313 wpa_scan_results_free(scan_res);
314
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700315 iface->secondary_ch = iface->conf->secondary_channel;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700316 if (!oper40) {
317 wpa_printf(MSG_INFO, "20/40 MHz operation not permitted on "
318 "channel pri=%d sec=%d based on overlapping BSSes",
319 iface->conf->channel,
320 iface->conf->channel +
321 iface->conf->secondary_channel * 4);
322 iface->conf->secondary_channel = 0;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700323 if (iface->drv_flags & WPA_DRIVER_FLAGS_HT_2040_COEX) {
324 /*
325 * TODO: Could consider scheduling another scan to check
326 * if channel width can be changed if no coex reports
327 * are received from associating stations.
328 */
329 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700330 }
331
332 res = ieee80211n_allowed_ht40_channel_pair(iface);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700333 if (!res) {
334 iface->conf->secondary_channel = 0;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800335 iface->conf->vht_oper_centr_freq_seg0_idx = 0;
336 iface->conf->vht_oper_centr_freq_seg1_idx = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700337 iface->conf->vht_oper_chwidth = VHT_CHANWIDTH_USE_HT;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800338 res = 1;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700339 wpa_printf(MSG_INFO, "Fallback to 20 MHz");
340 }
341
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700342 hostapd_setup_interface_complete(iface, !res);
343}
344
345
Dmitry Shmidt04949592012-07-19 12:16:46 -0700346static void ieee80211n_scan_channels_2g4(struct hostapd_iface *iface,
347 struct wpa_driver_scan_params *params)
348{
349 /* Scan only the affected frequency range */
350 int pri_freq, sec_freq;
351 int affected_start, affected_end;
352 int i, pos;
353 struct hostapd_hw_modes *mode;
354
355 if (iface->current_mode == NULL)
356 return;
357
358 pri_freq = hostapd_hw_get_freq(iface->bss[0], iface->conf->channel);
359 if (iface->conf->secondary_channel > 0)
360 sec_freq = pri_freq + 20;
361 else
362 sec_freq = pri_freq - 20;
Dmitry Shmidt41712582015-06-29 11:02:15 -0700363 /*
364 * Note: Need to find the PRI channel also in cases where the affected
365 * channel is the SEC channel of a 40 MHz BSS, so need to include the
366 * scanning coverage here to be 40 MHz from the center frequency.
367 */
368 affected_start = (pri_freq + sec_freq) / 2 - 40;
369 affected_end = (pri_freq + sec_freq) / 2 + 40;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700370 wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
371 affected_start, affected_end);
372
373 mode = iface->current_mode;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700374 params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
Dmitry Shmidt04949592012-07-19 12:16:46 -0700375 if (params->freqs == NULL)
376 return;
377 pos = 0;
378
379 for (i = 0; i < mode->num_channels; i++) {
380 struct hostapd_channel_data *chan = &mode->channels[i];
381 if (chan->flag & HOSTAPD_CHAN_DISABLED)
382 continue;
383 if (chan->freq < affected_start ||
384 chan->freq > affected_end)
385 continue;
386 params->freqs[pos++] = chan->freq;
387 }
388}
389
390
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800391static void ieee80211n_scan_channels_5g(struct hostapd_iface *iface,
392 struct wpa_driver_scan_params *params)
393{
394 /* Scan only the affected frequency range */
395 int pri_freq;
396 int affected_start, affected_end;
397 int i, pos;
398 struct hostapd_hw_modes *mode;
399
400 if (iface->current_mode == NULL)
401 return;
402
403 pri_freq = hostapd_hw_get_freq(iface->bss[0], iface->conf->channel);
404 if (iface->conf->secondary_channel > 0) {
405 affected_start = pri_freq - 10;
406 affected_end = pri_freq + 30;
407 } else {
408 affected_start = pri_freq - 30;
409 affected_end = pri_freq + 10;
410 }
411 wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
412 affected_start, affected_end);
413
414 mode = iface->current_mode;
415 params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
416 if (params->freqs == NULL)
417 return;
418 pos = 0;
419
420 for (i = 0; i < mode->num_channels; i++) {
421 struct hostapd_channel_data *chan = &mode->channels[i];
422 if (chan->flag & HOSTAPD_CHAN_DISABLED)
423 continue;
424 if (chan->freq < affected_start ||
425 chan->freq > affected_end)
426 continue;
427 params->freqs[pos++] = chan->freq;
428 }
429}
430
431
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700432static void ap_ht40_scan_retry(void *eloop_data, void *user_data)
433{
434#define HT2040_COEX_SCAN_RETRY 15
435 struct hostapd_iface *iface = eloop_data;
436 struct wpa_driver_scan_params params;
437 int ret;
438
439 os_memset(&params, 0, sizeof(params));
440 if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
441 ieee80211n_scan_channels_2g4(iface, &params);
442 else
443 ieee80211n_scan_channels_5g(iface, &params);
444
445 ret = hostapd_driver_scan(iface->bss[0], &params);
446 iface->num_ht40_scan_tries++;
447 os_free(params.freqs);
448
449 if (ret == -EBUSY &&
450 iface->num_ht40_scan_tries < HT2040_COEX_SCAN_RETRY) {
451 wpa_printf(MSG_ERROR,
452 "Failed to request a scan of neighboring BSSes ret=%d (%s) - try to scan again (attempt %d)",
453 ret, strerror(-ret), iface->num_ht40_scan_tries);
454 eloop_register_timeout(1, 0, ap_ht40_scan_retry, iface, NULL);
455 return;
456 }
457
458 if (ret == 0) {
459 iface->scan_cb = ieee80211n_check_scan;
460 return;
461 }
462
463 wpa_printf(MSG_DEBUG,
464 "Failed to request a scan in device, bringing up in HT20 mode");
465 iface->conf->secondary_channel = 0;
466 iface->conf->ht_capab &= ~HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
467 hostapd_setup_interface_complete(iface, 0);
468}
469
470
471void hostapd_stop_setup_timers(struct hostapd_iface *iface)
472{
473 eloop_cancel_timeout(ap_ht40_scan_retry, iface, NULL);
474}
475
476
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700477static int ieee80211n_check_40mhz(struct hostapd_iface *iface)
478{
479 struct wpa_driver_scan_params params;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700480 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700481
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800482 /* Check that HT40 is used and PRI / SEC switch is allowed */
483 if (!iface->conf->secondary_channel || iface->conf->no_pri_sec_switch)
484 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700485
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800486 hostapd_set_state(iface, HAPD_IFACE_HT_SCAN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700487 wpa_printf(MSG_DEBUG, "Scan for neighboring BSSes prior to enabling "
488 "40 MHz channel");
489 os_memset(&params, 0, sizeof(params));
Dmitry Shmidt04949592012-07-19 12:16:46 -0700490 if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
491 ieee80211n_scan_channels_2g4(iface, &params);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800492 else
493 ieee80211n_scan_channels_5g(iface, &params);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700494
495 ret = hostapd_driver_scan(iface->bss[0], &params);
496 os_free(params.freqs);
497
498 if (ret == -EBUSY) {
499 wpa_printf(MSG_ERROR,
500 "Failed to request a scan of neighboring BSSes ret=%d (%s) - try to scan again",
501 ret, strerror(-ret));
502 iface->num_ht40_scan_tries = 1;
503 eloop_cancel_timeout(ap_ht40_scan_retry, iface, NULL);
504 eloop_register_timeout(1, 0, ap_ht40_scan_retry, iface, NULL);
505 return 1;
506 }
507
508 if (ret < 0) {
509 wpa_printf(MSG_ERROR,
510 "Failed to request a scan of neighboring BSSes ret=%d (%s)",
511 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700512 return -1;
513 }
514
515 iface->scan_cb = ieee80211n_check_scan;
516 return 1;
517}
518
519
520static int ieee80211n_supported_ht_capab(struct hostapd_iface *iface)
521{
522 u16 hw = iface->current_mode->ht_capab;
523 u16 conf = iface->conf->ht_capab;
524
525 if ((conf & HT_CAP_INFO_LDPC_CODING_CAP) &&
526 !(hw & HT_CAP_INFO_LDPC_CODING_CAP)) {
527 wpa_printf(MSG_ERROR, "Driver does not support configured "
528 "HT capability [LDPC]");
529 return 0;
530 }
531
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700532 /*
533 * Driver ACS chosen channel may not be HT40 due to internal driver
534 * restrictions.
535 */
536 if (!iface->conf->acs && (conf & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700537 !(hw & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
538 wpa_printf(MSG_ERROR, "Driver does not support configured "
539 "HT capability [HT40*]");
540 return 0;
541 }
542
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800543 switch (conf & HT_CAP_INFO_SMPS_MASK) {
544 case HT_CAP_INFO_SMPS_STATIC:
545 if (!(iface->smps_modes & WPA_DRIVER_SMPS_MODE_STATIC)) {
546 wpa_printf(MSG_ERROR,
547 "Driver does not support configured HT capability [SMPS-STATIC]");
548 return 0;
549 }
550 break;
551 case HT_CAP_INFO_SMPS_DYNAMIC:
552 if (!(iface->smps_modes & WPA_DRIVER_SMPS_MODE_DYNAMIC)) {
553 wpa_printf(MSG_ERROR,
554 "Driver does not support configured HT capability [SMPS-DYNAMIC]");
555 return 0;
556 }
557 break;
558 case HT_CAP_INFO_SMPS_DISABLED:
559 default:
560 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700561 }
562
563 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
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700661#endif /* CONFIG_IEEE80211N */
662
663
664int hostapd_check_ht_capab(struct hostapd_iface *iface)
665{
666#ifdef CONFIG_IEEE80211N
667 int ret;
668 if (!iface->conf->ieee80211n)
669 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800670
671 if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211B &&
672 iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G &&
673 (iface->conf->ht_capab & HT_CAP_INFO_DSSS_CCK40MHZ)) {
674 wpa_printf(MSG_DEBUG,
675 "Disable HT capability [DSSS_CCK-40] on 5 GHz band");
676 iface->conf->ht_capab &= ~HT_CAP_INFO_DSSS_CCK40MHZ;
677 }
678
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700679 if (!ieee80211n_supported_ht_capab(iface))
680 return -1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700681#ifdef CONFIG_IEEE80211AC
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800682 if (iface->conf->ieee80211ac &&
683 !ieee80211ac_supported_vht_capab(iface))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700684 return -1;
685#endif /* CONFIG_IEEE80211AC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700686 ret = ieee80211n_check_40mhz(iface);
687 if (ret)
688 return ret;
689 if (!ieee80211n_allowed_ht40_channel_pair(iface))
690 return -1;
691#endif /* CONFIG_IEEE80211N */
692
693 return 0;
694}
695
696
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700697static int hostapd_is_usable_chan(struct hostapd_iface *iface,
698 int channel, int primary)
699{
700 int i;
701 struct hostapd_channel_data *chan;
702
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700703 if (!iface->current_mode)
704 return 0;
705
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700706 for (i = 0; i < iface->current_mode->num_channels; i++) {
707 chan = &iface->current_mode->channels[i];
708 if (chan->chan != channel)
709 continue;
710
711 if (!(chan->flag & HOSTAPD_CHAN_DISABLED))
712 return 1;
713
714 wpa_printf(MSG_DEBUG,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800715 "%schannel [%i] (%i) is disabled for use in AP mode, flags: 0x%x%s%s",
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700716 primary ? "" : "Configured HT40 secondary ",
717 i, chan->chan, chan->flag,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800718 chan->flag & HOSTAPD_CHAN_NO_IR ? " NO-IR" : "",
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700719 chan->flag & HOSTAPD_CHAN_RADAR ? " RADAR" : "");
720 }
721
Dmitry Shmidt29333592017-01-09 12:27:11 -0800722 wpa_printf(MSG_INFO, "Channel %d (%s) not allowed for AP mode",
723 channel, primary ? "primary" : "secondary");
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700724 return 0;
725}
726
727
728static int hostapd_is_usable_chans(struct hostapd_iface *iface)
729{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700730 int secondary_chan;
731
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700732 if (!hostapd_is_usable_chan(iface, iface->conf->channel, 1))
733 return 0;
734
735 if (!iface->conf->secondary_channel)
736 return 1;
737
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700738 if (!iface->conf->ht40_plus_minus_allowed)
739 return hostapd_is_usable_chan(
740 iface, iface->conf->channel +
741 iface->conf->secondary_channel * 4, 0);
742
743 /* Both HT40+ and HT40- are set, pick a valid secondary channel */
744 secondary_chan = iface->conf->channel + 4;
745 if (hostapd_is_usable_chan(iface, secondary_chan, 0)) {
746 iface->conf->secondary_channel = 1;
747 return 1;
748 }
749
750 secondary_chan = iface->conf->channel - 4;
751 if (hostapd_is_usable_chan(iface, secondary_chan, 0)) {
752 iface->conf->secondary_channel = -1;
753 return 1;
754 }
755
756 return 0;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700757}
758
759
760static enum hostapd_chan_status
761hostapd_check_chans(struct hostapd_iface *iface)
762{
763 if (iface->conf->channel) {
764 if (hostapd_is_usable_chans(iface))
765 return HOSTAPD_CHAN_VALID;
766 else
767 return HOSTAPD_CHAN_INVALID;
768 }
769
770 /*
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700771 * The user set channel=0 or channel=acs_survey
772 * which is used to trigger ACS.
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700773 */
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700774
775 switch (acs_init(iface)) {
776 case HOSTAPD_CHAN_ACS:
777 return HOSTAPD_CHAN_ACS;
778 case HOSTAPD_CHAN_VALID:
779 case HOSTAPD_CHAN_INVALID:
780 default:
781 return HOSTAPD_CHAN_INVALID;
782 }
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700783}
784
785
786static void hostapd_notify_bad_chans(struct hostapd_iface *iface)
787{
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700788 if (!iface->current_mode) {
789 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
790 HOSTAPD_LEVEL_WARNING,
791 "Hardware does not support configured mode");
792 return;
793 }
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700794 hostapd_logger(iface->bss[0], NULL,
795 HOSTAPD_MODULE_IEEE80211,
796 HOSTAPD_LEVEL_WARNING,
797 "Configured channel (%d) not found from the "
798 "channel list of current mode (%d) %s",
799 iface->conf->channel,
800 iface->current_mode->mode,
801 hostapd_hw_mode_txt(iface->current_mode->mode));
802 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
803 HOSTAPD_LEVEL_WARNING,
804 "Hardware does not support configured channel");
805}
806
807
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700808int hostapd_acs_completed(struct hostapd_iface *iface, int err)
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700809{
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700810 int ret = -1;
811
812 if (err)
813 goto out;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700814
815 switch (hostapd_check_chans(iface)) {
816 case HOSTAPD_CHAN_VALID:
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800817 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO,
818 ACS_EVENT_COMPLETED "freq=%d channel=%d",
819 hostapd_hw_get_freq(iface->bss[0],
820 iface->conf->channel),
821 iface->conf->channel);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700822 break;
823 case HOSTAPD_CHAN_ACS:
824 wpa_printf(MSG_ERROR, "ACS error - reported complete, but no result available");
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800825 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, ACS_EVENT_FAILED);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700826 hostapd_notify_bad_chans(iface);
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700827 goto out;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700828 case HOSTAPD_CHAN_INVALID:
829 default:
830 wpa_printf(MSG_ERROR, "ACS picked unusable channels");
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800831 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, ACS_EVENT_FAILED);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700832 hostapd_notify_bad_chans(iface);
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700833 goto out;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700834 }
835
836 ret = hostapd_check_ht_capab(iface);
837 if (ret < 0)
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700838 goto out;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700839 if (ret == 1) {
840 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback");
841 return 0;
842 }
843
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700844 ret = 0;
845out:
846 return hostapd_setup_interface_complete(iface, ret);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700847}
848
849
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700850/**
851 * hostapd_select_hw_mode - Select the hardware mode
852 * @iface: Pointer to interface data.
Jouni Malinen87fd2792011-05-16 18:35:42 +0300853 * Returns: 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700854 *
855 * Sets up the hardware mode, channel, rates, and passive scanning
856 * based on the configuration.
857 */
858int hostapd_select_hw_mode(struct hostapd_iface *iface)
859{
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700860 int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700861
862 if (iface->num_hw_features < 1)
863 return -1;
864
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800865 if ((iface->conf->hw_mode == HOSTAPD_MODE_IEEE80211G ||
866 iface->conf->ieee80211n || iface->conf->ieee80211ac) &&
867 iface->conf->channel == 14) {
868 wpa_printf(MSG_INFO, "Disable OFDM/HT/VHT on channel 14");
869 iface->conf->hw_mode = HOSTAPD_MODE_IEEE80211B;
870 iface->conf->ieee80211n = 0;
871 iface->conf->ieee80211ac = 0;
872 }
873
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700874 iface->current_mode = NULL;
875 for (i = 0; i < iface->num_hw_features; i++) {
876 struct hostapd_hw_modes *mode = &iface->hw_features[i];
877 if (mode->mode == iface->conf->hw_mode) {
878 iface->current_mode = mode;
879 break;
880 }
881 }
882
883 if (iface->current_mode == NULL) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -0700884 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_ACS_OFFLOAD) ||
885 !(iface->drv_flags & WPA_DRIVER_FLAGS_SUPPORT_HW_MODE_ANY))
886 {
887 wpa_printf(MSG_ERROR,
888 "Hardware does not support configured mode");
889 hostapd_logger(iface->bss[0], NULL,
890 HOSTAPD_MODULE_IEEE80211,
891 HOSTAPD_LEVEL_WARNING,
892 "Hardware does not support configured mode (%d) (hw_mode in hostapd.conf)",
893 (int) iface->conf->hw_mode);
894 return -2;
895 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700896 }
897
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700898 switch (hostapd_check_chans(iface)) {
899 case HOSTAPD_CHAN_VALID:
900 return 0;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700901 case HOSTAPD_CHAN_ACS: /* ACS will run and later complete */
902 return 1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700903 case HOSTAPD_CHAN_INVALID:
904 default:
905 hostapd_notify_bad_chans(iface);
Jouni Malinen87fd2792011-05-16 18:35:42 +0300906 return -3;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700907 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700908}
909
910
911const char * hostapd_hw_mode_txt(int mode)
912{
913 switch (mode) {
914 case HOSTAPD_MODE_IEEE80211A:
915 return "IEEE 802.11a";
916 case HOSTAPD_MODE_IEEE80211B:
917 return "IEEE 802.11b";
918 case HOSTAPD_MODE_IEEE80211G:
919 return "IEEE 802.11g";
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800920 case HOSTAPD_MODE_IEEE80211AD:
921 return "IEEE 802.11ad";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700922 default:
923 return "UNKNOWN";
924 }
925}
926
927
928int hostapd_hw_get_freq(struct hostapd_data *hapd, int chan)
929{
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800930 return hw_get_freq(hapd->iface->current_mode, chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700931}
932
933
934int hostapd_hw_get_channel(struct hostapd_data *hapd, int freq)
935{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700936 int i, channel;
937 struct hostapd_hw_modes *mode;
938
939 channel = hw_get_chan(hapd->iface->current_mode, freq);
940 if (channel)
941 return channel;
942 /* Check other available modes since the channel list for the current
943 * mode did not include the specified frequency. */
944 for (i = 0; i < hapd->iface->num_hw_features; i++) {
945 mode = &hapd->iface->hw_features[i];
946 channel = hw_get_chan(mode, freq);
947 if (channel)
948 return channel;
949 }
950 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700951}