blob: 54a79b09d95dda5414f088d018f7674eb615b529 [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 Shmidt8d520ff2011-05-09 14:06:53 -070018#include "hostapd.h"
19#include "ap_config.h"
20#include "ap_drv_ops.h"
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070021#include "acs.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070022#include "hw_features.h"
23
24
25void hostapd_free_hw_features(struct hostapd_hw_modes *hw_features,
26 size_t num_hw_features)
27{
28 size_t i;
29
30 if (hw_features == NULL)
31 return;
32
33 for (i = 0; i < num_hw_features; i++) {
34 os_free(hw_features[i].channels);
35 os_free(hw_features[i].rates);
36 }
37
38 os_free(hw_features);
39}
40
41
Dmitry Shmidt051af732013-10-22 13:52:46 -070042#ifndef CONFIG_NO_STDOUT_DEBUG
43static char * dfs_info(struct hostapd_channel_data *chan)
44{
45 static char info[256];
46 char *state;
47
48 switch (chan->flag & HOSTAPD_CHAN_DFS_MASK) {
49 case HOSTAPD_CHAN_DFS_UNKNOWN:
50 state = "unknown";
51 break;
52 case HOSTAPD_CHAN_DFS_USABLE:
53 state = "usable";
54 break;
55 case HOSTAPD_CHAN_DFS_UNAVAILABLE:
56 state = "unavailable";
57 break;
58 case HOSTAPD_CHAN_DFS_AVAILABLE:
59 state = "available";
60 break;
61 default:
62 return "";
63 }
64 os_snprintf(info, sizeof(info), " (DFS state = %s)", state);
65 info[sizeof(info) - 1] = '\0';
66
67 return info;
68}
69#endif /* CONFIG_NO_STDOUT_DEBUG */
70
71
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070072int hostapd_get_hw_features(struct hostapd_iface *iface)
73{
74 struct hostapd_data *hapd = iface->bss[0];
75 int ret = 0, i, j;
76 u16 num_modes, flags;
77 struct hostapd_hw_modes *modes;
78
79 if (hostapd_drv_none(hapd))
80 return -1;
81 modes = hostapd_get_hw_feature_data(hapd, &num_modes, &flags);
82 if (modes == NULL) {
83 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
84 HOSTAPD_LEVEL_DEBUG,
85 "Fetching hardware channel/rate support not "
86 "supported.");
87 return -1;
88 }
89
90 iface->hw_flags = flags;
91
92 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
93 iface->hw_features = modes;
94 iface->num_hw_features = num_modes;
95
96 for (i = 0; i < num_modes; i++) {
97 struct hostapd_hw_modes *feature = &modes[i];
Dmitry Shmidt051af732013-10-22 13:52:46 -070098 int dfs_enabled = hapd->iconf->ieee80211h &&
99 (iface->drv_flags & WPA_DRIVER_FLAGS_RADAR);
100
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700101 /* set flag for channels we can use in current regulatory
102 * domain */
103 for (j = 0; j < feature->num_channels; j++) {
Dmitry Shmidt051af732013-10-22 13:52:46 -0700104 int dfs = 0;
105
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700106 /*
107 * Disable all channels that are marked not to allow
Dmitry Shmidt051af732013-10-22 13:52:46 -0700108 * IBSS operation or active scanning.
109 * Use radar channels only if the driver supports DFS.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700110 */
Dmitry Shmidt051af732013-10-22 13:52:46 -0700111 if ((feature->channels[j].flag &
112 HOSTAPD_CHAN_RADAR) && dfs_enabled) {
113 dfs = 1;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700114 } else if (((feature->channels[j].flag &
115 HOSTAPD_CHAN_RADAR) &&
116 !(iface->drv_flags &
117 WPA_DRIVER_FLAGS_DFS_OFFLOAD)) ||
118 (feature->channels[j].flag &
119 (HOSTAPD_CHAN_NO_IBSS |
120 HOSTAPD_CHAN_PASSIVE_SCAN))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700121 feature->channels[j].flag |=
122 HOSTAPD_CHAN_DISABLED;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700123 }
124
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700125 if (feature->channels[j].flag & HOSTAPD_CHAN_DISABLED)
126 continue;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700127
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700128 wpa_printf(MSG_MSGDUMP, "Allowed channel: mode=%d "
Dmitry Shmidt051af732013-10-22 13:52:46 -0700129 "chan=%d freq=%d MHz max_tx_power=%d dBm%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700130 feature->mode,
131 feature->channels[j].chan,
132 feature->channels[j].freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700133 feature->channels[j].max_tx_power,
134 dfs ? dfs_info(&feature->channels[j]) : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700135 }
136 }
137
138 return ret;
139}
140
141
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800142int hostapd_prepare_rates(struct hostapd_iface *iface,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700143 struct hostapd_hw_modes *mode)
144{
145 int i, num_basic_rates = 0;
146 int basic_rates_a[] = { 60, 120, 240, -1 };
147 int basic_rates_b[] = { 10, 20, -1 };
148 int basic_rates_g[] = { 10, 20, 55, 110, -1 };
149 int *basic_rates;
150
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800151 if (iface->conf->basic_rates)
152 basic_rates = iface->conf->basic_rates;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700153 else switch (mode->mode) {
154 case HOSTAPD_MODE_IEEE80211A:
155 basic_rates = basic_rates_a;
156 break;
157 case HOSTAPD_MODE_IEEE80211B:
158 basic_rates = basic_rates_b;
159 break;
160 case HOSTAPD_MODE_IEEE80211G:
161 basic_rates = basic_rates_g;
162 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800163 case HOSTAPD_MODE_IEEE80211AD:
164 return 0; /* No basic rates for 11ad */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700165 default:
166 return -1;
167 }
168
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800169 i = 0;
170 while (basic_rates[i] >= 0)
171 i++;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700172 if (i)
173 i++; /* -1 termination */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800174 os_free(iface->basic_rates);
175 iface->basic_rates = os_malloc(i * sizeof(int));
176 if (iface->basic_rates)
177 os_memcpy(iface->basic_rates, basic_rates, i * sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700178
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800179 os_free(iface->current_rates);
180 iface->num_rates = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700181
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800182 iface->current_rates =
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700183 os_calloc(mode->num_rates, sizeof(struct hostapd_rate_data));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800184 if (!iface->current_rates) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700185 wpa_printf(MSG_ERROR, "Failed to allocate memory for rate "
186 "table.");
187 return -1;
188 }
189
190 for (i = 0; i < mode->num_rates; i++) {
191 struct hostapd_rate_data *rate;
192
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800193 if (iface->conf->supported_rates &&
194 !hostapd_rate_found(iface->conf->supported_rates,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700195 mode->rates[i]))
196 continue;
197
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800198 rate = &iface->current_rates[iface->num_rates];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700199 rate->rate = mode->rates[i];
200 if (hostapd_rate_found(basic_rates, rate->rate)) {
201 rate->flags |= HOSTAPD_RATE_BASIC;
202 num_basic_rates++;
203 }
204 wpa_printf(MSG_DEBUG, "RATE[%d] rate=%d flags=0x%x",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800205 iface->num_rates, rate->rate, rate->flags);
206 iface->num_rates++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700207 }
208
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800209 if ((iface->num_rates == 0 || num_basic_rates == 0) &&
210 (!iface->conf->ieee80211n || !iface->conf->require_ht)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700211 wpa_printf(MSG_ERROR, "No rates remaining in supported/basic "
212 "rate sets (%d,%d).",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800213 iface->num_rates, num_basic_rates);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700214 return -1;
215 }
216
217 return 0;
218}
219
220
221#ifdef CONFIG_IEEE80211N
222static int ieee80211n_allowed_ht40_channel_pair(struct hostapd_iface *iface)
223{
224 int sec_chan, ok, j, first;
225 int allowed[] = { 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157,
226 184, 192 };
227 size_t k;
228
229 if (!iface->conf->secondary_channel)
230 return 1; /* HT40 not used */
231
232 sec_chan = iface->conf->channel + iface->conf->secondary_channel * 4;
233 wpa_printf(MSG_DEBUG, "HT40: control channel: %d "
234 "secondary channel: %d",
235 iface->conf->channel, sec_chan);
236
237 /* Verify that HT40 secondary channel is an allowed 20 MHz
238 * channel */
239 ok = 0;
240 for (j = 0; j < iface->current_mode->num_channels; j++) {
241 struct hostapd_channel_data *chan =
242 &iface->current_mode->channels[j];
243 if (!(chan->flag & HOSTAPD_CHAN_DISABLED) &&
244 chan->chan == sec_chan) {
245 ok = 1;
246 break;
247 }
248 }
249 if (!ok) {
250 wpa_printf(MSG_ERROR, "HT40 secondary channel %d not allowed",
251 sec_chan);
252 return 0;
253 }
254
255 /*
256 * Verify that HT40 primary,secondary channel pair is allowed per
257 * IEEE 802.11n Annex J. This is only needed for 5 GHz band since
258 * 2.4 GHz rules allow all cases where the secondary channel fits into
259 * the list of allowed channels (already checked above).
260 */
261 if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A)
262 return 1;
263
264 if (iface->conf->secondary_channel > 0)
265 first = iface->conf->channel;
266 else
267 first = sec_chan;
268
269 ok = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700270 for (k = 0; k < ARRAY_SIZE(allowed); k++) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700271 if (first == allowed[k]) {
272 ok = 1;
273 break;
274 }
275 }
276 if (!ok) {
277 wpa_printf(MSG_ERROR, "HT40 channel pair (%d, %d) not allowed",
278 iface->conf->channel,
279 iface->conf->secondary_channel);
280 return 0;
281 }
282
283 return 1;
284}
285
286
287static void ieee80211n_switch_pri_sec(struct hostapd_iface *iface)
288{
289 if (iface->conf->secondary_channel > 0) {
290 iface->conf->channel += 4;
291 iface->conf->secondary_channel = -1;
292 } else {
293 iface->conf->channel -= 4;
294 iface->conf->secondary_channel = 1;
295 }
296}
297
298
299static void ieee80211n_get_pri_sec_chan(struct wpa_scan_res *bss,
300 int *pri_chan, int *sec_chan)
301{
302 struct ieee80211_ht_operation *oper;
303 struct ieee802_11_elems elems;
304
305 *pri_chan = *sec_chan = 0;
306
307 ieee802_11_parse_elems((u8 *) (bss + 1), bss->ie_len, &elems, 0);
308 if (elems.ht_operation &&
309 elems.ht_operation_len >= sizeof(*oper)) {
310 oper = (struct ieee80211_ht_operation *) elems.ht_operation;
311 *pri_chan = oper->control_chan;
312 if (oper->ht_param & HT_INFO_HT_PARAM_REC_TRANS_CHNL_WIDTH) {
313 int sec = oper->ht_param &
314 HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK;
315 if (sec == HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
316 *sec_chan = *pri_chan + 4;
317 else if (sec == HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
318 *sec_chan = *pri_chan - 4;
319 }
320 }
321}
322
323
324static int ieee80211n_check_40mhz_5g(struct hostapd_iface *iface,
325 struct wpa_scan_results *scan_res)
326{
327 int pri_chan, sec_chan, pri_freq, sec_freq, pri_bss, sec_bss;
328 int bss_pri_chan, bss_sec_chan;
329 size_t i;
330 int match;
331
332 pri_chan = iface->conf->channel;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700333 sec_chan = pri_chan + iface->conf->secondary_channel * 4;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700334 pri_freq = hostapd_hw_get_freq(iface->bss[0], pri_chan);
335 if (iface->conf->secondary_channel > 0)
336 sec_freq = pri_freq + 20;
337 else
338 sec_freq = pri_freq - 20;
339
340 /*
341 * Switch PRI/SEC channels if Beacons were detected on selected SEC
342 * channel, but not on selected PRI channel.
343 */
344 pri_bss = sec_bss = 0;
345 for (i = 0; i < scan_res->num; i++) {
346 struct wpa_scan_res *bss = scan_res->res[i];
347 if (bss->freq == pri_freq)
348 pri_bss++;
349 else if (bss->freq == sec_freq)
350 sec_bss++;
351 }
352 if (sec_bss && !pri_bss) {
353 wpa_printf(MSG_INFO, "Switch own primary and secondary "
354 "channel to get secondary channel with no Beacons "
355 "from other BSSes");
356 ieee80211n_switch_pri_sec(iface);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700357 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700358 }
359
360 /*
361 * Match PRI/SEC channel with any existing HT40 BSS on the same
362 * channels that we are about to use (if already mixed order in
363 * existing BSSes, use own preference).
364 */
365 match = 0;
366 for (i = 0; i < scan_res->num; i++) {
367 struct wpa_scan_res *bss = scan_res->res[i];
368 ieee80211n_get_pri_sec_chan(bss, &bss_pri_chan, &bss_sec_chan);
369 if (pri_chan == bss_pri_chan &&
370 sec_chan == bss_sec_chan) {
371 match = 1;
372 break;
373 }
374 }
375 if (!match) {
376 for (i = 0; i < scan_res->num; i++) {
377 struct wpa_scan_res *bss = scan_res->res[i];
378 ieee80211n_get_pri_sec_chan(bss, &bss_pri_chan,
379 &bss_sec_chan);
380 if (pri_chan == bss_sec_chan &&
381 sec_chan == bss_pri_chan) {
382 wpa_printf(MSG_INFO, "Switch own primary and "
383 "secondary channel due to BSS "
384 "overlap with " MACSTR,
385 MAC2STR(bss->bssid));
386 ieee80211n_switch_pri_sec(iface);
387 break;
388 }
389 }
390 }
391
392 return 1;
393}
394
395
396static int ieee80211n_check_40mhz_2g4(struct hostapd_iface *iface,
397 struct wpa_scan_results *scan_res)
398{
399 int pri_freq, sec_freq;
400 int affected_start, affected_end;
401 size_t i;
402
403 pri_freq = hostapd_hw_get_freq(iface->bss[0], iface->conf->channel);
404 if (iface->conf->secondary_channel > 0)
405 sec_freq = pri_freq + 20;
406 else
407 sec_freq = pri_freq - 20;
408 affected_start = (pri_freq + sec_freq) / 2 - 25;
409 affected_end = (pri_freq + sec_freq) / 2 + 25;
410 wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
411 affected_start, affected_end);
412 for (i = 0; i < scan_res->num; i++) {
413 struct wpa_scan_res *bss = scan_res->res[i];
414 int pri = bss->freq;
415 int sec = pri;
416 int sec_chan, pri_chan;
417
418 ieee80211n_get_pri_sec_chan(bss, &pri_chan, &sec_chan);
419
420 if (sec_chan) {
421 if (sec_chan < pri_chan)
422 sec = pri - 20;
423 else
424 sec = pri + 20;
425 }
426
427 if ((pri < affected_start || pri > affected_end) &&
428 (sec < affected_start || sec > affected_end))
429 continue; /* not within affected channel range */
430
431 wpa_printf(MSG_DEBUG, "Neighboring BSS: " MACSTR
432 " freq=%d pri=%d sec=%d",
433 MAC2STR(bss->bssid), bss->freq, pri_chan, sec_chan);
434
435 if (sec_chan) {
436 if (pri_freq != pri || sec_freq != sec) {
437 wpa_printf(MSG_DEBUG, "40 MHz pri/sec "
438 "mismatch with BSS " MACSTR
439 " <%d,%d> (chan=%d%c) vs. <%d,%d>",
440 MAC2STR(bss->bssid),
441 pri, sec, pri_chan,
442 sec > pri ? '+' : '-',
443 pri_freq, sec_freq);
444 return 0;
445 }
446 }
447
448 /* TODO: 40 MHz intolerant */
449 }
450
451 return 1;
452}
453
454
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700455static void ieee80211n_check_scan(struct hostapd_iface *iface)
456{
457 struct wpa_scan_results *scan_res;
458 int oper40;
459 int res;
460
461 /* Check list of neighboring BSSes (from scan) to see whether 40 MHz is
Dmitry Shmidt04949592012-07-19 12:16:46 -0700462 * allowed per IEEE Std 802.11-2012, 10.15.3.2 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700463
464 iface->scan_cb = NULL;
465
466 scan_res = hostapd_driver_get_scan_results(iface->bss[0]);
467 if (scan_res == NULL) {
468 hostapd_setup_interface_complete(iface, 1);
469 return;
470 }
471
472 if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A)
473 oper40 = ieee80211n_check_40mhz_5g(iface, scan_res);
474 else
475 oper40 = ieee80211n_check_40mhz_2g4(iface, scan_res);
476 wpa_scan_results_free(scan_res);
477
478 if (!oper40) {
479 wpa_printf(MSG_INFO, "20/40 MHz operation not permitted on "
480 "channel pri=%d sec=%d based on overlapping BSSes",
481 iface->conf->channel,
482 iface->conf->channel +
483 iface->conf->secondary_channel * 4);
484 iface->conf->secondary_channel = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700485 }
486
487 res = ieee80211n_allowed_ht40_channel_pair(iface);
488 hostapd_setup_interface_complete(iface, !res);
489}
490
491
Dmitry Shmidt04949592012-07-19 12:16:46 -0700492static void ieee80211n_scan_channels_2g4(struct hostapd_iface *iface,
493 struct wpa_driver_scan_params *params)
494{
495 /* Scan only the affected frequency range */
496 int pri_freq, sec_freq;
497 int affected_start, affected_end;
498 int i, pos;
499 struct hostapd_hw_modes *mode;
500
501 if (iface->current_mode == NULL)
502 return;
503
504 pri_freq = hostapd_hw_get_freq(iface->bss[0], iface->conf->channel);
505 if (iface->conf->secondary_channel > 0)
506 sec_freq = pri_freq + 20;
507 else
508 sec_freq = pri_freq - 20;
509 affected_start = (pri_freq + sec_freq) / 2 - 25;
510 affected_end = (pri_freq + sec_freq) / 2 + 25;
511 wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
512 affected_start, affected_end);
513
514 mode = iface->current_mode;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700515 params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
Dmitry Shmidt04949592012-07-19 12:16:46 -0700516 if (params->freqs == NULL)
517 return;
518 pos = 0;
519
520 for (i = 0; i < mode->num_channels; i++) {
521 struct hostapd_channel_data *chan = &mode->channels[i];
522 if (chan->flag & HOSTAPD_CHAN_DISABLED)
523 continue;
524 if (chan->freq < affected_start ||
525 chan->freq > affected_end)
526 continue;
527 params->freqs[pos++] = chan->freq;
528 }
529}
530
531
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800532static void ieee80211n_scan_channels_5g(struct hostapd_iface *iface,
533 struct wpa_driver_scan_params *params)
534{
535 /* Scan only the affected frequency range */
536 int pri_freq;
537 int affected_start, affected_end;
538 int i, pos;
539 struct hostapd_hw_modes *mode;
540
541 if (iface->current_mode == NULL)
542 return;
543
544 pri_freq = hostapd_hw_get_freq(iface->bss[0], iface->conf->channel);
545 if (iface->conf->secondary_channel > 0) {
546 affected_start = pri_freq - 10;
547 affected_end = pri_freq + 30;
548 } else {
549 affected_start = pri_freq - 30;
550 affected_end = pri_freq + 10;
551 }
552 wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
553 affected_start, affected_end);
554
555 mode = iface->current_mode;
556 params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
557 if (params->freqs == NULL)
558 return;
559 pos = 0;
560
561 for (i = 0; i < mode->num_channels; i++) {
562 struct hostapd_channel_data *chan = &mode->channels[i];
563 if (chan->flag & HOSTAPD_CHAN_DISABLED)
564 continue;
565 if (chan->freq < affected_start ||
566 chan->freq > affected_end)
567 continue;
568 params->freqs[pos++] = chan->freq;
569 }
570}
571
572
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700573static int ieee80211n_check_40mhz(struct hostapd_iface *iface)
574{
575 struct wpa_driver_scan_params params;
576
577 if (!iface->conf->secondary_channel)
578 return 0; /* HT40 not used */
579
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800580 hostapd_set_state(iface, HAPD_IFACE_HT_SCAN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700581 wpa_printf(MSG_DEBUG, "Scan for neighboring BSSes prior to enabling "
582 "40 MHz channel");
583 os_memset(&params, 0, sizeof(params));
Dmitry Shmidt04949592012-07-19 12:16:46 -0700584 if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
585 ieee80211n_scan_channels_2g4(iface, &params);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800586 else
587 ieee80211n_scan_channels_5g(iface, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700588 if (hostapd_driver_scan(iface->bss[0], &params) < 0) {
589 wpa_printf(MSG_ERROR, "Failed to request a scan of "
590 "neighboring BSSes");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700591 os_free(params.freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700592 return -1;
593 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700594 os_free(params.freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700595
596 iface->scan_cb = ieee80211n_check_scan;
597 return 1;
598}
599
600
601static int ieee80211n_supported_ht_capab(struct hostapd_iface *iface)
602{
603 u16 hw = iface->current_mode->ht_capab;
604 u16 conf = iface->conf->ht_capab;
605
606 if ((conf & HT_CAP_INFO_LDPC_CODING_CAP) &&
607 !(hw & HT_CAP_INFO_LDPC_CODING_CAP)) {
608 wpa_printf(MSG_ERROR, "Driver does not support configured "
609 "HT capability [LDPC]");
610 return 0;
611 }
612
613 if ((conf & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) &&
614 !(hw & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
615 wpa_printf(MSG_ERROR, "Driver does not support configured "
616 "HT capability [HT40*]");
617 return 0;
618 }
619
620 if ((conf & HT_CAP_INFO_SMPS_MASK) != (hw & HT_CAP_INFO_SMPS_MASK) &&
621 (conf & HT_CAP_INFO_SMPS_MASK) != HT_CAP_INFO_SMPS_DISABLED) {
622 wpa_printf(MSG_ERROR, "Driver does not support configured "
623 "HT capability [SMPS-*]");
624 return 0;
625 }
626
627 if ((conf & HT_CAP_INFO_GREEN_FIELD) &&
628 !(hw & HT_CAP_INFO_GREEN_FIELD)) {
629 wpa_printf(MSG_ERROR, "Driver does not support configured "
630 "HT capability [GF]");
631 return 0;
632 }
633
634 if ((conf & HT_CAP_INFO_SHORT_GI20MHZ) &&
635 !(hw & HT_CAP_INFO_SHORT_GI20MHZ)) {
636 wpa_printf(MSG_ERROR, "Driver does not support configured "
637 "HT capability [SHORT-GI-20]");
638 return 0;
639 }
640
641 if ((conf & HT_CAP_INFO_SHORT_GI40MHZ) &&
642 !(hw & HT_CAP_INFO_SHORT_GI40MHZ)) {
643 wpa_printf(MSG_ERROR, "Driver does not support configured "
644 "HT capability [SHORT-GI-40]");
645 return 0;
646 }
647
648 if ((conf & HT_CAP_INFO_TX_STBC) && !(hw & HT_CAP_INFO_TX_STBC)) {
649 wpa_printf(MSG_ERROR, "Driver does not support configured "
650 "HT capability [TX-STBC]");
651 return 0;
652 }
653
654 if ((conf & HT_CAP_INFO_RX_STBC_MASK) >
655 (hw & HT_CAP_INFO_RX_STBC_MASK)) {
656 wpa_printf(MSG_ERROR, "Driver does not support configured "
657 "HT capability [RX-STBC*]");
658 return 0;
659 }
660
661 if ((conf & HT_CAP_INFO_DELAYED_BA) &&
662 !(hw & HT_CAP_INFO_DELAYED_BA)) {
663 wpa_printf(MSG_ERROR, "Driver does not support configured "
664 "HT capability [DELAYED-BA]");
665 return 0;
666 }
667
668 if ((conf & HT_CAP_INFO_MAX_AMSDU_SIZE) &&
669 !(hw & HT_CAP_INFO_MAX_AMSDU_SIZE)) {
670 wpa_printf(MSG_ERROR, "Driver does not support configured "
671 "HT capability [MAX-AMSDU-7935]");
672 return 0;
673 }
674
675 if ((conf & HT_CAP_INFO_DSSS_CCK40MHZ) &&
676 !(hw & HT_CAP_INFO_DSSS_CCK40MHZ)) {
677 wpa_printf(MSG_ERROR, "Driver does not support configured "
678 "HT capability [DSSS_CCK-40]");
679 return 0;
680 }
681
682 if ((conf & HT_CAP_INFO_PSMP_SUPP) && !(hw & HT_CAP_INFO_PSMP_SUPP)) {
683 wpa_printf(MSG_ERROR, "Driver does not support configured "
684 "HT capability [PSMP]");
685 return 0;
686 }
687
688 if ((conf & HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT) &&
689 !(hw & HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT)) {
690 wpa_printf(MSG_ERROR, "Driver does not support configured "
691 "HT capability [LSIG-TXOP-PROT]");
692 return 0;
693 }
694
695 return 1;
696}
697
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700698
699#ifdef CONFIG_IEEE80211AC
700
701static int ieee80211ac_cap_check(u32 hw, u32 conf, u32 cap, const char *name)
702{
703 u32 req_cap = conf & cap;
704
705 /*
706 * Make sure we support all requested capabilities.
707 * NOTE: We assume that 'cap' represents a capability mask,
708 * not a discrete value.
709 */
710 if ((hw & req_cap) != req_cap) {
711 wpa_printf(MSG_ERROR, "Driver does not support configured VHT capability [%s]",
712 name);
713 return 0;
714 }
715 return 1;
716}
717
718
719static int ieee80211ac_cap_check_max(u32 hw, u32 conf, u32 cap,
720 const char *name)
721{
722 u32 hw_max = hw & cap;
723 u32 conf_val = conf & cap;
724
725 if (conf_val > hw_max) {
726 int offset = find_first_bit(cap);
727 wpa_printf(MSG_ERROR, "Configured VHT capability [%s] exceeds max value supported by the driver (%d > %d)",
728 name, conf_val >> offset, hw_max >> offset);
729 return 0;
730 }
731 return 1;
732}
733
734
735static int ieee80211ac_supported_vht_capab(struct hostapd_iface *iface)
736{
737 u32 hw = iface->current_mode->vht_capab;
738 u32 conf = iface->conf->vht_capab;
739
740 wpa_printf(MSG_DEBUG, "hw vht capab: 0x%x, conf vht capab: 0x%x",
741 hw, conf);
742
743#define VHT_CAP_CHECK(cap) \
744 do { \
745 if (!ieee80211ac_cap_check(hw, conf, cap, #cap)) \
746 return 0; \
747 } while (0)
748
749#define VHT_CAP_CHECK_MAX(cap) \
750 do { \
751 if (!ieee80211ac_cap_check_max(hw, conf, cap, #cap)) \
752 return 0; \
753 } while (0)
754
755 VHT_CAP_CHECK_MAX(VHT_CAP_MAX_MPDU_LENGTH_MASK);
756 VHT_CAP_CHECK(VHT_CAP_SUPP_CHAN_WIDTH_160MHZ);
757 VHT_CAP_CHECK(VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ);
758 VHT_CAP_CHECK(VHT_CAP_RXLDPC);
759 VHT_CAP_CHECK(VHT_CAP_SHORT_GI_80);
760 VHT_CAP_CHECK(VHT_CAP_SHORT_GI_160);
761 VHT_CAP_CHECK(VHT_CAP_TXSTBC);
762 VHT_CAP_CHECK_MAX(VHT_CAP_RXSTBC_MASK);
763 VHT_CAP_CHECK(VHT_CAP_SU_BEAMFORMER_CAPABLE);
764 VHT_CAP_CHECK(VHT_CAP_SU_BEAMFORMEE_CAPABLE);
765 VHT_CAP_CHECK_MAX(VHT_CAP_BEAMFORMEE_STS_MAX);
766 VHT_CAP_CHECK_MAX(VHT_CAP_SOUNDING_DIMENSION_MAX);
767 VHT_CAP_CHECK(VHT_CAP_MU_BEAMFORMER_CAPABLE);
768 VHT_CAP_CHECK(VHT_CAP_MU_BEAMFORMEE_CAPABLE);
769 VHT_CAP_CHECK(VHT_CAP_VHT_TXOP_PS);
770 VHT_CAP_CHECK(VHT_CAP_HTC_VHT);
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800771 VHT_CAP_CHECK_MAX(VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700772 VHT_CAP_CHECK(VHT_CAP_VHT_LINK_ADAPTATION_VHT_UNSOL_MFB);
773 VHT_CAP_CHECK(VHT_CAP_VHT_LINK_ADAPTATION_VHT_MRQ_MFB);
774 VHT_CAP_CHECK(VHT_CAP_RX_ANTENNA_PATTERN);
775 VHT_CAP_CHECK(VHT_CAP_TX_ANTENNA_PATTERN);
776
777#undef VHT_CAP_CHECK
778#undef VHT_CAP_CHECK_MAX
779
780 return 1;
781}
782#endif /* CONFIG_IEEE80211AC */
783
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700784#endif /* CONFIG_IEEE80211N */
785
786
787int hostapd_check_ht_capab(struct hostapd_iface *iface)
788{
789#ifdef CONFIG_IEEE80211N
790 int ret;
791 if (!iface->conf->ieee80211n)
792 return 0;
793 if (!ieee80211n_supported_ht_capab(iface))
794 return -1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700795#ifdef CONFIG_IEEE80211AC
796 if (!ieee80211ac_supported_vht_capab(iface))
797 return -1;
798#endif /* CONFIG_IEEE80211AC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700799 ret = ieee80211n_check_40mhz(iface);
800 if (ret)
801 return ret;
802 if (!ieee80211n_allowed_ht40_channel_pair(iface))
803 return -1;
804#endif /* CONFIG_IEEE80211N */
805
806 return 0;
807}
808
809
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700810static int hostapd_is_usable_chan(struct hostapd_iface *iface,
811 int channel, int primary)
812{
813 int i;
814 struct hostapd_channel_data *chan;
815
816 for (i = 0; i < iface->current_mode->num_channels; i++) {
817 chan = &iface->current_mode->channels[i];
818 if (chan->chan != channel)
819 continue;
820
821 if (!(chan->flag & HOSTAPD_CHAN_DISABLED))
822 return 1;
823
824 wpa_printf(MSG_DEBUG,
825 "%schannel [%i] (%i) is disabled for use in AP mode, flags: 0x%x%s%s%s",
826 primary ? "" : "Configured HT40 secondary ",
827 i, chan->chan, chan->flag,
828 chan->flag & HOSTAPD_CHAN_NO_IBSS ? " NO-IBSS" : "",
829 chan->flag & HOSTAPD_CHAN_PASSIVE_SCAN ?
830 " PASSIVE-SCAN" : "",
831 chan->flag & HOSTAPD_CHAN_RADAR ? " RADAR" : "");
832 }
833
834 return 0;
835}
836
837
838static int hostapd_is_usable_chans(struct hostapd_iface *iface)
839{
840 if (!hostapd_is_usable_chan(iface, iface->conf->channel, 1))
841 return 0;
842
843 if (!iface->conf->secondary_channel)
844 return 1;
845
846 return hostapd_is_usable_chan(iface, iface->conf->channel +
847 iface->conf->secondary_channel * 4, 0);
848}
849
850
851static enum hostapd_chan_status
852hostapd_check_chans(struct hostapd_iface *iface)
853{
854 if (iface->conf->channel) {
855 if (hostapd_is_usable_chans(iface))
856 return HOSTAPD_CHAN_VALID;
857 else
858 return HOSTAPD_CHAN_INVALID;
859 }
860
861 /*
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700862 * The user set channel=0 or channel=acs_survey
863 * which is used to trigger ACS.
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700864 */
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700865
866 switch (acs_init(iface)) {
867 case HOSTAPD_CHAN_ACS:
868 return HOSTAPD_CHAN_ACS;
869 case HOSTAPD_CHAN_VALID:
870 case HOSTAPD_CHAN_INVALID:
871 default:
872 return HOSTAPD_CHAN_INVALID;
873 }
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700874}
875
876
877static void hostapd_notify_bad_chans(struct hostapd_iface *iface)
878{
879 hostapd_logger(iface->bss[0], NULL,
880 HOSTAPD_MODULE_IEEE80211,
881 HOSTAPD_LEVEL_WARNING,
882 "Configured channel (%d) not found from the "
883 "channel list of current mode (%d) %s",
884 iface->conf->channel,
885 iface->current_mode->mode,
886 hostapd_hw_mode_txt(iface->current_mode->mode));
887 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
888 HOSTAPD_LEVEL_WARNING,
889 "Hardware does not support configured channel");
890}
891
892
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700893int hostapd_acs_completed(struct hostapd_iface *iface, int err)
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700894{
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700895 int ret = -1;
896
897 if (err)
898 goto out;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700899
900 switch (hostapd_check_chans(iface)) {
901 case HOSTAPD_CHAN_VALID:
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800902 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO,
903 ACS_EVENT_COMPLETED "freq=%d channel=%d",
904 hostapd_hw_get_freq(iface->bss[0],
905 iface->conf->channel),
906 iface->conf->channel);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700907 break;
908 case HOSTAPD_CHAN_ACS:
909 wpa_printf(MSG_ERROR, "ACS error - reported complete, but no result available");
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800910 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, ACS_EVENT_FAILED);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700911 hostapd_notify_bad_chans(iface);
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700912 goto out;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700913 case HOSTAPD_CHAN_INVALID:
914 default:
915 wpa_printf(MSG_ERROR, "ACS picked unusable channels");
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800916 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, ACS_EVENT_FAILED);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700917 hostapd_notify_bad_chans(iface);
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700918 goto out;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700919 }
920
921 ret = hostapd_check_ht_capab(iface);
922 if (ret < 0)
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700923 goto out;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700924 if (ret == 1) {
925 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback");
926 return 0;
927 }
928
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700929 ret = 0;
930out:
931 return hostapd_setup_interface_complete(iface, ret);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700932}
933
934
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700935/**
936 * hostapd_select_hw_mode - Select the hardware mode
937 * @iface: Pointer to interface data.
Jouni Malinen87fd2792011-05-16 18:35:42 +0300938 * Returns: 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700939 *
940 * Sets up the hardware mode, channel, rates, and passive scanning
941 * based on the configuration.
942 */
943int hostapd_select_hw_mode(struct hostapd_iface *iface)
944{
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700945 int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700946
947 if (iface->num_hw_features < 1)
948 return -1;
949
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800950 if ((iface->conf->hw_mode == HOSTAPD_MODE_IEEE80211G ||
951 iface->conf->ieee80211n || iface->conf->ieee80211ac) &&
952 iface->conf->channel == 14) {
953 wpa_printf(MSG_INFO, "Disable OFDM/HT/VHT on channel 14");
954 iface->conf->hw_mode = HOSTAPD_MODE_IEEE80211B;
955 iface->conf->ieee80211n = 0;
956 iface->conf->ieee80211ac = 0;
957 }
958
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700959 iface->current_mode = NULL;
960 for (i = 0; i < iface->num_hw_features; i++) {
961 struct hostapd_hw_modes *mode = &iface->hw_features[i];
962 if (mode->mode == iface->conf->hw_mode) {
963 iface->current_mode = mode;
964 break;
965 }
966 }
967
968 if (iface->current_mode == NULL) {
969 wpa_printf(MSG_ERROR, "Hardware does not support configured "
970 "mode");
971 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
972 HOSTAPD_LEVEL_WARNING,
973 "Hardware does not support configured mode "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800974 "(%d) (hw_mode in hostapd.conf)",
975 (int) iface->conf->hw_mode);
Jouni Malinen87fd2792011-05-16 18:35:42 +0300976 return -2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700977 }
978
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700979 switch (hostapd_check_chans(iface)) {
980 case HOSTAPD_CHAN_VALID:
981 return 0;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700982 case HOSTAPD_CHAN_ACS: /* ACS will run and later complete */
983 return 1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700984 case HOSTAPD_CHAN_INVALID:
985 default:
986 hostapd_notify_bad_chans(iface);
Jouni Malinen87fd2792011-05-16 18:35:42 +0300987 return -3;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700988 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700989}
990
991
992const char * hostapd_hw_mode_txt(int mode)
993{
994 switch (mode) {
995 case HOSTAPD_MODE_IEEE80211A:
996 return "IEEE 802.11a";
997 case HOSTAPD_MODE_IEEE80211B:
998 return "IEEE 802.11b";
999 case HOSTAPD_MODE_IEEE80211G:
1000 return "IEEE 802.11g";
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001001 case HOSTAPD_MODE_IEEE80211AD:
1002 return "IEEE 802.11ad";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001003 default:
1004 return "UNKNOWN";
1005 }
1006}
1007
1008
1009int hostapd_hw_get_freq(struct hostapd_data *hapd, int chan)
1010{
1011 int i;
1012
1013 if (!hapd->iface->current_mode)
1014 return 0;
1015
1016 for (i = 0; i < hapd->iface->current_mode->num_channels; i++) {
1017 struct hostapd_channel_data *ch =
1018 &hapd->iface->current_mode->channels[i];
1019 if (ch->chan == chan)
1020 return ch->freq;
1021 }
1022
1023 return 0;
1024}
1025
1026
1027int hostapd_hw_get_channel(struct hostapd_data *hapd, int freq)
1028{
1029 int i;
1030
1031 if (!hapd->iface->current_mode)
1032 return 0;
1033
1034 for (i = 0; i < hapd->iface->current_mode->num_channels; i++) {
1035 struct hostapd_channel_data *ch =
1036 &hapd->iface->current_mode->channels[i];
1037 if (ch->freq == freq)
1038 return ch->chan;
1039 }
1040
1041 return 0;
1042}