blob: 7c08e524d7ae56d2939a2c5666033336d5e57179 [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;
81
82 if (hostapd_drv_none(hapd))
83 return -1;
84 modes = hostapd_get_hw_feature_data(hapd, &num_modes, &flags);
85 if (modes == NULL) {
86 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
87 HOSTAPD_LEVEL_DEBUG,
88 "Fetching hardware channel/rate support not "
89 "supported.");
90 return -1;
91 }
92
93 iface->hw_flags = flags;
94
95 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
96 iface->hw_features = modes;
97 iface->num_hw_features = num_modes;
98
99 for (i = 0; i < num_modes; i++) {
100 struct hostapd_hw_modes *feature = &modes[i];
Dmitry Shmidt051af732013-10-22 13:52:46 -0700101 int dfs_enabled = hapd->iconf->ieee80211h &&
102 (iface->drv_flags & WPA_DRIVER_FLAGS_RADAR);
103
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700104 /* set flag for channels we can use in current regulatory
105 * domain */
106 for (j = 0; j < feature->num_channels; j++) {
Dmitry Shmidt051af732013-10-22 13:52:46 -0700107 int dfs = 0;
108
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700109 /*
110 * Disable all channels that are marked not to allow
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800111 * to initiate radiation (a.k.a. passive scan and no
112 * IBSS).
Dmitry Shmidt051af732013-10-22 13:52:46 -0700113 * Use radar channels only if the driver supports DFS.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700114 */
Dmitry Shmidt051af732013-10-22 13:52:46 -0700115 if ((feature->channels[j].flag &
116 HOSTAPD_CHAN_RADAR) && dfs_enabled) {
117 dfs = 1;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700118 } else if (((feature->channels[j].flag &
119 HOSTAPD_CHAN_RADAR) &&
120 !(iface->drv_flags &
121 WPA_DRIVER_FLAGS_DFS_OFFLOAD)) ||
122 (feature->channels[j].flag &
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800123 HOSTAPD_CHAN_NO_IR)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700124 feature->channels[j].flag |=
125 HOSTAPD_CHAN_DISABLED;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700126 }
127
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700128 if (feature->channels[j].flag & HOSTAPD_CHAN_DISABLED)
129 continue;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700130
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700131 wpa_printf(MSG_MSGDUMP, "Allowed channel: mode=%d "
Dmitry Shmidt051af732013-10-22 13:52:46 -0700132 "chan=%d freq=%d MHz max_tx_power=%d dBm%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700133 feature->mode,
134 feature->channels[j].chan,
135 feature->channels[j].freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700136 feature->channels[j].max_tx_power,
137 dfs ? dfs_info(&feature->channels[j]) : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700138 }
139 }
140
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800141 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700142}
143
144
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800145int hostapd_prepare_rates(struct hostapd_iface *iface,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700146 struct hostapd_hw_modes *mode)
147{
148 int i, num_basic_rates = 0;
149 int basic_rates_a[] = { 60, 120, 240, -1 };
150 int basic_rates_b[] = { 10, 20, -1 };
151 int basic_rates_g[] = { 10, 20, 55, 110, -1 };
152 int *basic_rates;
153
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800154 if (iface->conf->basic_rates)
155 basic_rates = iface->conf->basic_rates;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700156 else switch (mode->mode) {
157 case HOSTAPD_MODE_IEEE80211A:
158 basic_rates = basic_rates_a;
159 break;
160 case HOSTAPD_MODE_IEEE80211B:
161 basic_rates = basic_rates_b;
162 break;
163 case HOSTAPD_MODE_IEEE80211G:
164 basic_rates = basic_rates_g;
165 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800166 case HOSTAPD_MODE_IEEE80211AD:
167 return 0; /* No basic rates for 11ad */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700168 default:
169 return -1;
170 }
171
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800172 i = 0;
173 while (basic_rates[i] >= 0)
174 i++;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700175 if (i)
176 i++; /* -1 termination */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800177 os_free(iface->basic_rates);
178 iface->basic_rates = os_malloc(i * sizeof(int));
179 if (iface->basic_rates)
180 os_memcpy(iface->basic_rates, basic_rates, i * sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700181
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800182 os_free(iface->current_rates);
183 iface->num_rates = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700184
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800185 iface->current_rates =
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700186 os_calloc(mode->num_rates, sizeof(struct hostapd_rate_data));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800187 if (!iface->current_rates) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700188 wpa_printf(MSG_ERROR, "Failed to allocate memory for rate "
189 "table.");
190 return -1;
191 }
192
193 for (i = 0; i < mode->num_rates; i++) {
194 struct hostapd_rate_data *rate;
195
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800196 if (iface->conf->supported_rates &&
197 !hostapd_rate_found(iface->conf->supported_rates,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700198 mode->rates[i]))
199 continue;
200
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800201 rate = &iface->current_rates[iface->num_rates];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700202 rate->rate = mode->rates[i];
203 if (hostapd_rate_found(basic_rates, rate->rate)) {
204 rate->flags |= HOSTAPD_RATE_BASIC;
205 num_basic_rates++;
206 }
207 wpa_printf(MSG_DEBUG, "RATE[%d] rate=%d flags=0x%x",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800208 iface->num_rates, rate->rate, rate->flags);
209 iface->num_rates++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700210 }
211
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800212 if ((iface->num_rates == 0 || num_basic_rates == 0) &&
213 (!iface->conf->ieee80211n || !iface->conf->require_ht)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700214 wpa_printf(MSG_ERROR, "No rates remaining in supported/basic "
215 "rate sets (%d,%d).",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800216 iface->num_rates, num_basic_rates);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700217 return -1;
218 }
219
220 return 0;
221}
222
223
224#ifdef CONFIG_IEEE80211N
225static int ieee80211n_allowed_ht40_channel_pair(struct hostapd_iface *iface)
226{
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800227 int pri_chan, sec_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700228
229 if (!iface->conf->secondary_channel)
230 return 1; /* HT40 not used */
231
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800232 pri_chan = iface->conf->channel;
233 sec_chan = pri_chan + iface->conf->secondary_channel * 4;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700234
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800235 return allowed_ht40_channel_pair(iface->current_mode, pri_chan,
236 sec_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700237}
238
239
240static void ieee80211n_switch_pri_sec(struct hostapd_iface *iface)
241{
242 if (iface->conf->secondary_channel > 0) {
243 iface->conf->channel += 4;
244 iface->conf->secondary_channel = -1;
245 } else {
246 iface->conf->channel -= 4;
247 iface->conf->secondary_channel = 1;
248 }
249}
250
251
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700252static int ieee80211n_check_40mhz_5g(struct hostapd_iface *iface,
253 struct wpa_scan_results *scan_res)
254{
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800255 int pri_chan, sec_chan;
256 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700257
258 pri_chan = iface->conf->channel;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700259 sec_chan = pri_chan + iface->conf->secondary_channel * 4;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700260
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800261 res = check_40mhz_5g(iface->current_mode, scan_res, pri_chan, sec_chan);
262
263 if (res == 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700264 ieee80211n_switch_pri_sec(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700265
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800266 return !!res;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -0700267}
268
269
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700270static int ieee80211n_check_40mhz_2g4(struct hostapd_iface *iface,
271 struct wpa_scan_results *scan_res)
272{
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800273 int pri_chan, sec_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700274
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800275 pri_chan = iface->conf->channel;
276 sec_chan = pri_chan + iface->conf->secondary_channel * 4;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700277
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800278 return check_40mhz_2g4(iface->current_mode, scan_res, pri_chan,
279 sec_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700280}
281
282
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700283static void ieee80211n_check_scan(struct hostapd_iface *iface)
284{
285 struct wpa_scan_results *scan_res;
286 int oper40;
287 int res;
288
289 /* Check list of neighboring BSSes (from scan) to see whether 40 MHz is
Dmitry Shmidt04949592012-07-19 12:16:46 -0700290 * allowed per IEEE Std 802.11-2012, 10.15.3.2 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291
292 iface->scan_cb = NULL;
293
294 scan_res = hostapd_driver_get_scan_results(iface->bss[0]);
295 if (scan_res == NULL) {
296 hostapd_setup_interface_complete(iface, 1);
297 return;
298 }
299
300 if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A)
301 oper40 = ieee80211n_check_40mhz_5g(iface, scan_res);
302 else
303 oper40 = ieee80211n_check_40mhz_2g4(iface, scan_res);
304 wpa_scan_results_free(scan_res);
305
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700306 iface->secondary_ch = iface->conf->secondary_channel;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700307 if (!oper40) {
308 wpa_printf(MSG_INFO, "20/40 MHz operation not permitted on "
309 "channel pri=%d sec=%d based on overlapping BSSes",
310 iface->conf->channel,
311 iface->conf->channel +
312 iface->conf->secondary_channel * 4);
313 iface->conf->secondary_channel = 0;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700314 if (iface->drv_flags & WPA_DRIVER_FLAGS_HT_2040_COEX) {
315 /*
316 * TODO: Could consider scheduling another scan to check
317 * if channel width can be changed if no coex reports
318 * are received from associating stations.
319 */
320 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700321 }
322
323 res = ieee80211n_allowed_ht40_channel_pair(iface);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700324 if (!res) {
325 iface->conf->secondary_channel = 0;
326 wpa_printf(MSG_INFO, "Fallback to 20 MHz");
327 }
328
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700329 hostapd_setup_interface_complete(iface, !res);
330}
331
332
Dmitry Shmidt04949592012-07-19 12:16:46 -0700333static void ieee80211n_scan_channels_2g4(struct hostapd_iface *iface,
334 struct wpa_driver_scan_params *params)
335{
336 /* Scan only the affected frequency range */
337 int pri_freq, sec_freq;
338 int affected_start, affected_end;
339 int i, pos;
340 struct hostapd_hw_modes *mode;
341
342 if (iface->current_mode == NULL)
343 return;
344
345 pri_freq = hostapd_hw_get_freq(iface->bss[0], iface->conf->channel);
346 if (iface->conf->secondary_channel > 0)
347 sec_freq = pri_freq + 20;
348 else
349 sec_freq = pri_freq - 20;
350 affected_start = (pri_freq + sec_freq) / 2 - 25;
351 affected_end = (pri_freq + sec_freq) / 2 + 25;
352 wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
353 affected_start, affected_end);
354
355 mode = iface->current_mode;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700356 params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
Dmitry Shmidt04949592012-07-19 12:16:46 -0700357 if (params->freqs == NULL)
358 return;
359 pos = 0;
360
361 for (i = 0; i < mode->num_channels; i++) {
362 struct hostapd_channel_data *chan = &mode->channels[i];
363 if (chan->flag & HOSTAPD_CHAN_DISABLED)
364 continue;
365 if (chan->freq < affected_start ||
366 chan->freq > affected_end)
367 continue;
368 params->freqs[pos++] = chan->freq;
369 }
370}
371
372
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800373static void ieee80211n_scan_channels_5g(struct hostapd_iface *iface,
374 struct wpa_driver_scan_params *params)
375{
376 /* Scan only the affected frequency range */
377 int pri_freq;
378 int affected_start, affected_end;
379 int i, pos;
380 struct hostapd_hw_modes *mode;
381
382 if (iface->current_mode == NULL)
383 return;
384
385 pri_freq = hostapd_hw_get_freq(iface->bss[0], iface->conf->channel);
386 if (iface->conf->secondary_channel > 0) {
387 affected_start = pri_freq - 10;
388 affected_end = pri_freq + 30;
389 } else {
390 affected_start = pri_freq - 30;
391 affected_end = pri_freq + 10;
392 }
393 wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
394 affected_start, affected_end);
395
396 mode = iface->current_mode;
397 params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
398 if (params->freqs == NULL)
399 return;
400 pos = 0;
401
402 for (i = 0; i < mode->num_channels; i++) {
403 struct hostapd_channel_data *chan = &mode->channels[i];
404 if (chan->flag & HOSTAPD_CHAN_DISABLED)
405 continue;
406 if (chan->freq < affected_start ||
407 chan->freq > affected_end)
408 continue;
409 params->freqs[pos++] = chan->freq;
410 }
411}
412
413
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700414static void ap_ht40_scan_retry(void *eloop_data, void *user_data)
415{
416#define HT2040_COEX_SCAN_RETRY 15
417 struct hostapd_iface *iface = eloop_data;
418 struct wpa_driver_scan_params params;
419 int ret;
420
421 os_memset(&params, 0, sizeof(params));
422 if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
423 ieee80211n_scan_channels_2g4(iface, &params);
424 else
425 ieee80211n_scan_channels_5g(iface, &params);
426
427 ret = hostapd_driver_scan(iface->bss[0], &params);
428 iface->num_ht40_scan_tries++;
429 os_free(params.freqs);
430
431 if (ret == -EBUSY &&
432 iface->num_ht40_scan_tries < HT2040_COEX_SCAN_RETRY) {
433 wpa_printf(MSG_ERROR,
434 "Failed to request a scan of neighboring BSSes ret=%d (%s) - try to scan again (attempt %d)",
435 ret, strerror(-ret), iface->num_ht40_scan_tries);
436 eloop_register_timeout(1, 0, ap_ht40_scan_retry, iface, NULL);
437 return;
438 }
439
440 if (ret == 0) {
441 iface->scan_cb = ieee80211n_check_scan;
442 return;
443 }
444
445 wpa_printf(MSG_DEBUG,
446 "Failed to request a scan in device, bringing up in HT20 mode");
447 iface->conf->secondary_channel = 0;
448 iface->conf->ht_capab &= ~HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
449 hostapd_setup_interface_complete(iface, 0);
450}
451
452
453void hostapd_stop_setup_timers(struct hostapd_iface *iface)
454{
455 eloop_cancel_timeout(ap_ht40_scan_retry, iface, NULL);
456}
457
458
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700459static int ieee80211n_check_40mhz(struct hostapd_iface *iface)
460{
461 struct wpa_driver_scan_params params;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700462 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700463
464 if (!iface->conf->secondary_channel)
465 return 0; /* HT40 not used */
466
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800467 hostapd_set_state(iface, HAPD_IFACE_HT_SCAN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700468 wpa_printf(MSG_DEBUG, "Scan for neighboring BSSes prior to enabling "
469 "40 MHz channel");
470 os_memset(&params, 0, sizeof(params));
Dmitry Shmidt04949592012-07-19 12:16:46 -0700471 if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
472 ieee80211n_scan_channels_2g4(iface, &params);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800473 else
474 ieee80211n_scan_channels_5g(iface, &params);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700475
476 ret = hostapd_driver_scan(iface->bss[0], &params);
477 os_free(params.freqs);
478
479 if (ret == -EBUSY) {
480 wpa_printf(MSG_ERROR,
481 "Failed to request a scan of neighboring BSSes ret=%d (%s) - try to scan again",
482 ret, strerror(-ret));
483 iface->num_ht40_scan_tries = 1;
484 eloop_cancel_timeout(ap_ht40_scan_retry, iface, NULL);
485 eloop_register_timeout(1, 0, ap_ht40_scan_retry, iface, NULL);
486 return 1;
487 }
488
489 if (ret < 0) {
490 wpa_printf(MSG_ERROR,
491 "Failed to request a scan of neighboring BSSes ret=%d (%s)",
492 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700493 return -1;
494 }
495
496 iface->scan_cb = ieee80211n_check_scan;
497 return 1;
498}
499
500
501static int ieee80211n_supported_ht_capab(struct hostapd_iface *iface)
502{
503 u16 hw = iface->current_mode->ht_capab;
504 u16 conf = iface->conf->ht_capab;
505
506 if ((conf & HT_CAP_INFO_LDPC_CODING_CAP) &&
507 !(hw & HT_CAP_INFO_LDPC_CODING_CAP)) {
508 wpa_printf(MSG_ERROR, "Driver does not support configured "
509 "HT capability [LDPC]");
510 return 0;
511 }
512
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700513 /*
514 * Driver ACS chosen channel may not be HT40 due to internal driver
515 * restrictions.
516 */
517 if (!iface->conf->acs && (conf & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700518 !(hw & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
519 wpa_printf(MSG_ERROR, "Driver does not support configured "
520 "HT capability [HT40*]");
521 return 0;
522 }
523
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800524 switch (conf & HT_CAP_INFO_SMPS_MASK) {
525 case HT_CAP_INFO_SMPS_STATIC:
526 if (!(iface->smps_modes & WPA_DRIVER_SMPS_MODE_STATIC)) {
527 wpa_printf(MSG_ERROR,
528 "Driver does not support configured HT capability [SMPS-STATIC]");
529 return 0;
530 }
531 break;
532 case HT_CAP_INFO_SMPS_DYNAMIC:
533 if (!(iface->smps_modes & WPA_DRIVER_SMPS_MODE_DYNAMIC)) {
534 wpa_printf(MSG_ERROR,
535 "Driver does not support configured HT capability [SMPS-DYNAMIC]");
536 return 0;
537 }
538 break;
539 case HT_CAP_INFO_SMPS_DISABLED:
540 default:
541 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700542 }
543
544 if ((conf & HT_CAP_INFO_GREEN_FIELD) &&
545 !(hw & HT_CAP_INFO_GREEN_FIELD)) {
546 wpa_printf(MSG_ERROR, "Driver does not support configured "
547 "HT capability [GF]");
548 return 0;
549 }
550
551 if ((conf & HT_CAP_INFO_SHORT_GI20MHZ) &&
552 !(hw & HT_CAP_INFO_SHORT_GI20MHZ)) {
553 wpa_printf(MSG_ERROR, "Driver does not support configured "
554 "HT capability [SHORT-GI-20]");
555 return 0;
556 }
557
558 if ((conf & HT_CAP_INFO_SHORT_GI40MHZ) &&
559 !(hw & HT_CAP_INFO_SHORT_GI40MHZ)) {
560 wpa_printf(MSG_ERROR, "Driver does not support configured "
561 "HT capability [SHORT-GI-40]");
562 return 0;
563 }
564
565 if ((conf & HT_CAP_INFO_TX_STBC) && !(hw & HT_CAP_INFO_TX_STBC)) {
566 wpa_printf(MSG_ERROR, "Driver does not support configured "
567 "HT capability [TX-STBC]");
568 return 0;
569 }
570
571 if ((conf & HT_CAP_INFO_RX_STBC_MASK) >
572 (hw & HT_CAP_INFO_RX_STBC_MASK)) {
573 wpa_printf(MSG_ERROR, "Driver does not support configured "
574 "HT capability [RX-STBC*]");
575 return 0;
576 }
577
578 if ((conf & HT_CAP_INFO_DELAYED_BA) &&
579 !(hw & HT_CAP_INFO_DELAYED_BA)) {
580 wpa_printf(MSG_ERROR, "Driver does not support configured "
581 "HT capability [DELAYED-BA]");
582 return 0;
583 }
584
585 if ((conf & HT_CAP_INFO_MAX_AMSDU_SIZE) &&
586 !(hw & HT_CAP_INFO_MAX_AMSDU_SIZE)) {
587 wpa_printf(MSG_ERROR, "Driver does not support configured "
588 "HT capability [MAX-AMSDU-7935]");
589 return 0;
590 }
591
592 if ((conf & HT_CAP_INFO_DSSS_CCK40MHZ) &&
593 !(hw & HT_CAP_INFO_DSSS_CCK40MHZ)) {
594 wpa_printf(MSG_ERROR, "Driver does not support configured "
595 "HT capability [DSSS_CCK-40]");
596 return 0;
597 }
598
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700599 if ((conf & HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT) &&
600 !(hw & HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT)) {
601 wpa_printf(MSG_ERROR, "Driver does not support configured "
602 "HT capability [LSIG-TXOP-PROT]");
603 return 0;
604 }
605
606 return 1;
607}
608
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700609
610#ifdef CONFIG_IEEE80211AC
611
612static int ieee80211ac_cap_check(u32 hw, u32 conf, u32 cap, const char *name)
613{
614 u32 req_cap = conf & cap;
615
616 /*
617 * Make sure we support all requested capabilities.
618 * NOTE: We assume that 'cap' represents a capability mask,
619 * not a discrete value.
620 */
621 if ((hw & req_cap) != req_cap) {
622 wpa_printf(MSG_ERROR, "Driver does not support configured VHT capability [%s]",
623 name);
624 return 0;
625 }
626 return 1;
627}
628
629
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800630static int ieee80211ac_cap_check_max(u32 hw, u32 conf, u32 mask,
631 unsigned int shift,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700632 const char *name)
633{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800634 u32 hw_max = hw & mask;
635 u32 conf_val = conf & mask;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700636
637 if (conf_val > hw_max) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700638 wpa_printf(MSG_ERROR, "Configured VHT capability [%s] exceeds max value supported by the driver (%d > %d)",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800639 name, conf_val >> shift, hw_max >> shift);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700640 return 0;
641 }
642 return 1;
643}
644
645
646static int ieee80211ac_supported_vht_capab(struct hostapd_iface *iface)
647{
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800648 struct hostapd_hw_modes *mode = iface->current_mode;
649 u32 hw = mode->vht_capab;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700650 u32 conf = iface->conf->vht_capab;
651
652 wpa_printf(MSG_DEBUG, "hw vht capab: 0x%x, conf vht capab: 0x%x",
653 hw, conf);
654
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800655 if (mode->mode == HOSTAPD_MODE_IEEE80211G &&
656 iface->conf->bss[0]->vendor_vht &&
657 mode->vht_capab == 0 && iface->hw_features) {
658 int i;
659
660 for (i = 0; i < iface->num_hw_features; i++) {
661 if (iface->hw_features[i].mode ==
662 HOSTAPD_MODE_IEEE80211A) {
663 mode = &iface->hw_features[i];
664 hw = mode->vht_capab;
665 wpa_printf(MSG_DEBUG,
666 "update hw vht capab based on 5 GHz band: 0x%x",
667 hw);
668 break;
669 }
670 }
671 }
672
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700673#define VHT_CAP_CHECK(cap) \
674 do { \
675 if (!ieee80211ac_cap_check(hw, conf, cap, #cap)) \
676 return 0; \
677 } while (0)
678
679#define VHT_CAP_CHECK_MAX(cap) \
680 do { \
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800681 if (!ieee80211ac_cap_check_max(hw, conf, cap, cap ## _SHIFT, \
682 #cap)) \
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700683 return 0; \
684 } while (0)
685
686 VHT_CAP_CHECK_MAX(VHT_CAP_MAX_MPDU_LENGTH_MASK);
687 VHT_CAP_CHECK(VHT_CAP_SUPP_CHAN_WIDTH_160MHZ);
688 VHT_CAP_CHECK(VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ);
689 VHT_CAP_CHECK(VHT_CAP_RXLDPC);
690 VHT_CAP_CHECK(VHT_CAP_SHORT_GI_80);
691 VHT_CAP_CHECK(VHT_CAP_SHORT_GI_160);
692 VHT_CAP_CHECK(VHT_CAP_TXSTBC);
693 VHT_CAP_CHECK_MAX(VHT_CAP_RXSTBC_MASK);
694 VHT_CAP_CHECK(VHT_CAP_SU_BEAMFORMER_CAPABLE);
695 VHT_CAP_CHECK(VHT_CAP_SU_BEAMFORMEE_CAPABLE);
696 VHT_CAP_CHECK_MAX(VHT_CAP_BEAMFORMEE_STS_MAX);
697 VHT_CAP_CHECK_MAX(VHT_CAP_SOUNDING_DIMENSION_MAX);
698 VHT_CAP_CHECK(VHT_CAP_MU_BEAMFORMER_CAPABLE);
699 VHT_CAP_CHECK(VHT_CAP_MU_BEAMFORMEE_CAPABLE);
700 VHT_CAP_CHECK(VHT_CAP_VHT_TXOP_PS);
701 VHT_CAP_CHECK(VHT_CAP_HTC_VHT);
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800702 VHT_CAP_CHECK_MAX(VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700703 VHT_CAP_CHECK(VHT_CAP_VHT_LINK_ADAPTATION_VHT_UNSOL_MFB);
704 VHT_CAP_CHECK(VHT_CAP_VHT_LINK_ADAPTATION_VHT_MRQ_MFB);
705 VHT_CAP_CHECK(VHT_CAP_RX_ANTENNA_PATTERN);
706 VHT_CAP_CHECK(VHT_CAP_TX_ANTENNA_PATTERN);
707
708#undef VHT_CAP_CHECK
709#undef VHT_CAP_CHECK_MAX
710
711 return 1;
712}
713#endif /* CONFIG_IEEE80211AC */
714
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700715#endif /* CONFIG_IEEE80211N */
716
717
718int hostapd_check_ht_capab(struct hostapd_iface *iface)
719{
720#ifdef CONFIG_IEEE80211N
721 int ret;
722 if (!iface->conf->ieee80211n)
723 return 0;
724 if (!ieee80211n_supported_ht_capab(iface))
725 return -1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700726#ifdef CONFIG_IEEE80211AC
727 if (!ieee80211ac_supported_vht_capab(iface))
728 return -1;
729#endif /* CONFIG_IEEE80211AC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700730 ret = ieee80211n_check_40mhz(iface);
731 if (ret)
732 return ret;
733 if (!ieee80211n_allowed_ht40_channel_pair(iface))
734 return -1;
735#endif /* CONFIG_IEEE80211N */
736
737 return 0;
738}
739
740
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700741static int hostapd_is_usable_chan(struct hostapd_iface *iface,
742 int channel, int primary)
743{
744 int i;
745 struct hostapd_channel_data *chan;
746
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700747 if (!iface->current_mode)
748 return 0;
749
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700750 for (i = 0; i < iface->current_mode->num_channels; i++) {
751 chan = &iface->current_mode->channels[i];
752 if (chan->chan != channel)
753 continue;
754
755 if (!(chan->flag & HOSTAPD_CHAN_DISABLED))
756 return 1;
757
758 wpa_printf(MSG_DEBUG,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800759 "%schannel [%i] (%i) is disabled for use in AP mode, flags: 0x%x%s%s",
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700760 primary ? "" : "Configured HT40 secondary ",
761 i, chan->chan, chan->flag,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800762 chan->flag & HOSTAPD_CHAN_NO_IR ? " NO-IR" : "",
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700763 chan->flag & HOSTAPD_CHAN_RADAR ? " RADAR" : "");
764 }
765
766 return 0;
767}
768
769
770static int hostapd_is_usable_chans(struct hostapd_iface *iface)
771{
772 if (!hostapd_is_usable_chan(iface, iface->conf->channel, 1))
773 return 0;
774
775 if (!iface->conf->secondary_channel)
776 return 1;
777
778 return hostapd_is_usable_chan(iface, iface->conf->channel +
779 iface->conf->secondary_channel * 4, 0);
780}
781
782
783static enum hostapd_chan_status
784hostapd_check_chans(struct hostapd_iface *iface)
785{
786 if (iface->conf->channel) {
787 if (hostapd_is_usable_chans(iface))
788 return HOSTAPD_CHAN_VALID;
789 else
790 return HOSTAPD_CHAN_INVALID;
791 }
792
793 /*
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700794 * The user set channel=0 or channel=acs_survey
795 * which is used to trigger ACS.
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700796 */
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700797
798 switch (acs_init(iface)) {
799 case HOSTAPD_CHAN_ACS:
800 return HOSTAPD_CHAN_ACS;
801 case HOSTAPD_CHAN_VALID:
802 case HOSTAPD_CHAN_INVALID:
803 default:
804 return HOSTAPD_CHAN_INVALID;
805 }
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700806}
807
808
809static void hostapd_notify_bad_chans(struct hostapd_iface *iface)
810{
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700811 if (!iface->current_mode) {
812 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
813 HOSTAPD_LEVEL_WARNING,
814 "Hardware does not support configured mode");
815 return;
816 }
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700817 hostapd_logger(iface->bss[0], NULL,
818 HOSTAPD_MODULE_IEEE80211,
819 HOSTAPD_LEVEL_WARNING,
820 "Configured channel (%d) not found from the "
821 "channel list of current mode (%d) %s",
822 iface->conf->channel,
823 iface->current_mode->mode,
824 hostapd_hw_mode_txt(iface->current_mode->mode));
825 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
826 HOSTAPD_LEVEL_WARNING,
827 "Hardware does not support configured channel");
828}
829
830
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700831int hostapd_acs_completed(struct hostapd_iface *iface, int err)
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700832{
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700833 int ret = -1;
834
835 if (err)
836 goto out;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700837
838 switch (hostapd_check_chans(iface)) {
839 case HOSTAPD_CHAN_VALID:
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800840 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO,
841 ACS_EVENT_COMPLETED "freq=%d channel=%d",
842 hostapd_hw_get_freq(iface->bss[0],
843 iface->conf->channel),
844 iface->conf->channel);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700845 break;
846 case HOSTAPD_CHAN_ACS:
847 wpa_printf(MSG_ERROR, "ACS error - reported complete, but no result available");
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800848 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, ACS_EVENT_FAILED);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700849 hostapd_notify_bad_chans(iface);
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700850 goto out;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700851 case HOSTAPD_CHAN_INVALID:
852 default:
853 wpa_printf(MSG_ERROR, "ACS picked unusable channels");
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800854 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, ACS_EVENT_FAILED);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700855 hostapd_notify_bad_chans(iface);
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700856 goto out;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700857 }
858
859 ret = hostapd_check_ht_capab(iface);
860 if (ret < 0)
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700861 goto out;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700862 if (ret == 1) {
863 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback");
864 return 0;
865 }
866
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700867 ret = 0;
868out:
869 return hostapd_setup_interface_complete(iface, ret);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700870}
871
872
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700873/**
874 * hostapd_select_hw_mode - Select the hardware mode
875 * @iface: Pointer to interface data.
Jouni Malinen87fd2792011-05-16 18:35:42 +0300876 * Returns: 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700877 *
878 * Sets up the hardware mode, channel, rates, and passive scanning
879 * based on the configuration.
880 */
881int hostapd_select_hw_mode(struct hostapd_iface *iface)
882{
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700883 int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700884
885 if (iface->num_hw_features < 1)
886 return -1;
887
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800888 if ((iface->conf->hw_mode == HOSTAPD_MODE_IEEE80211G ||
889 iface->conf->ieee80211n || iface->conf->ieee80211ac) &&
890 iface->conf->channel == 14) {
891 wpa_printf(MSG_INFO, "Disable OFDM/HT/VHT on channel 14");
892 iface->conf->hw_mode = HOSTAPD_MODE_IEEE80211B;
893 iface->conf->ieee80211n = 0;
894 iface->conf->ieee80211ac = 0;
895 }
896
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700897 iface->current_mode = NULL;
898 for (i = 0; i < iface->num_hw_features; i++) {
899 struct hostapd_hw_modes *mode = &iface->hw_features[i];
900 if (mode->mode == iface->conf->hw_mode) {
901 iface->current_mode = mode;
902 break;
903 }
904 }
905
906 if (iface->current_mode == NULL) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -0700907 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_ACS_OFFLOAD) ||
908 !(iface->drv_flags & WPA_DRIVER_FLAGS_SUPPORT_HW_MODE_ANY))
909 {
910 wpa_printf(MSG_ERROR,
911 "Hardware does not support configured mode");
912 hostapd_logger(iface->bss[0], NULL,
913 HOSTAPD_MODULE_IEEE80211,
914 HOSTAPD_LEVEL_WARNING,
915 "Hardware does not support configured mode (%d) (hw_mode in hostapd.conf)",
916 (int) iface->conf->hw_mode);
917 return -2;
918 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700919 }
920
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700921 switch (hostapd_check_chans(iface)) {
922 case HOSTAPD_CHAN_VALID:
923 return 0;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700924 case HOSTAPD_CHAN_ACS: /* ACS will run and later complete */
925 return 1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700926 case HOSTAPD_CHAN_INVALID:
927 default:
928 hostapd_notify_bad_chans(iface);
Jouni Malinen87fd2792011-05-16 18:35:42 +0300929 return -3;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700930 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700931}
932
933
934const char * hostapd_hw_mode_txt(int mode)
935{
936 switch (mode) {
937 case HOSTAPD_MODE_IEEE80211A:
938 return "IEEE 802.11a";
939 case HOSTAPD_MODE_IEEE80211B:
940 return "IEEE 802.11b";
941 case HOSTAPD_MODE_IEEE80211G:
942 return "IEEE 802.11g";
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800943 case HOSTAPD_MODE_IEEE80211AD:
944 return "IEEE 802.11ad";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700945 default:
946 return "UNKNOWN";
947 }
948}
949
950
951int hostapd_hw_get_freq(struct hostapd_data *hapd, int chan)
952{
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800953 return hw_get_freq(hapd->iface->current_mode, chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700954}
955
956
957int hostapd_hw_get_channel(struct hostapd_data *hapd, int freq)
958{
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800959 return hw_get_chan(hapd->iface->current_mode, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700960}