blob: d319ce007ce35baf5e62b336461ea0d78c72d8d6 [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;
114 } else if (feature->channels[j].flag &
115 (HOSTAPD_CHAN_NO_IBSS |
116 HOSTAPD_CHAN_PASSIVE_SCAN |
117 HOSTAPD_CHAN_RADAR)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700118 feature->channels[j].flag |=
119 HOSTAPD_CHAN_DISABLED;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700120 }
121
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700122 if (feature->channels[j].flag & HOSTAPD_CHAN_DISABLED)
123 continue;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700124
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700125 wpa_printf(MSG_MSGDUMP, "Allowed channel: mode=%d "
Dmitry Shmidt051af732013-10-22 13:52:46 -0700126 "chan=%d freq=%d MHz max_tx_power=%d dBm%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700127 feature->mode,
128 feature->channels[j].chan,
129 feature->channels[j].freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700130 feature->channels[j].max_tx_power,
131 dfs ? dfs_info(&feature->channels[j]) : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700132 }
133 }
134
135 return ret;
136}
137
138
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800139int hostapd_prepare_rates(struct hostapd_iface *iface,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700140 struct hostapd_hw_modes *mode)
141{
142 int i, num_basic_rates = 0;
143 int basic_rates_a[] = { 60, 120, 240, -1 };
144 int basic_rates_b[] = { 10, 20, -1 };
145 int basic_rates_g[] = { 10, 20, 55, 110, -1 };
146 int *basic_rates;
147
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800148 if (iface->conf->basic_rates)
149 basic_rates = iface->conf->basic_rates;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700150 else switch (mode->mode) {
151 case HOSTAPD_MODE_IEEE80211A:
152 basic_rates = basic_rates_a;
153 break;
154 case HOSTAPD_MODE_IEEE80211B:
155 basic_rates = basic_rates_b;
156 break;
157 case HOSTAPD_MODE_IEEE80211G:
158 basic_rates = basic_rates_g;
159 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800160 case HOSTAPD_MODE_IEEE80211AD:
161 return 0; /* No basic rates for 11ad */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700162 default:
163 return -1;
164 }
165
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800166 i = 0;
167 while (basic_rates[i] >= 0)
168 i++;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700169 if (i)
170 i++; /* -1 termination */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800171 os_free(iface->basic_rates);
172 iface->basic_rates = os_malloc(i * sizeof(int));
173 if (iface->basic_rates)
174 os_memcpy(iface->basic_rates, basic_rates, i * sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700175
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800176 os_free(iface->current_rates);
177 iface->num_rates = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700178
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800179 iface->current_rates =
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700180 os_calloc(mode->num_rates, sizeof(struct hostapd_rate_data));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800181 if (!iface->current_rates) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700182 wpa_printf(MSG_ERROR, "Failed to allocate memory for rate "
183 "table.");
184 return -1;
185 }
186
187 for (i = 0; i < mode->num_rates; i++) {
188 struct hostapd_rate_data *rate;
189
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800190 if (iface->conf->supported_rates &&
191 !hostapd_rate_found(iface->conf->supported_rates,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700192 mode->rates[i]))
193 continue;
194
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800195 rate = &iface->current_rates[iface->num_rates];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700196 rate->rate = mode->rates[i];
197 if (hostapd_rate_found(basic_rates, rate->rate)) {
198 rate->flags |= HOSTAPD_RATE_BASIC;
199 num_basic_rates++;
200 }
201 wpa_printf(MSG_DEBUG, "RATE[%d] rate=%d flags=0x%x",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800202 iface->num_rates, rate->rate, rate->flags);
203 iface->num_rates++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700204 }
205
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800206 if ((iface->num_rates == 0 || num_basic_rates == 0) &&
207 (!iface->conf->ieee80211n || !iface->conf->require_ht)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700208 wpa_printf(MSG_ERROR, "No rates remaining in supported/basic "
209 "rate sets (%d,%d).",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800210 iface->num_rates, num_basic_rates);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700211 return -1;
212 }
213
214 return 0;
215}
216
217
218#ifdef CONFIG_IEEE80211N
219static int ieee80211n_allowed_ht40_channel_pair(struct hostapd_iface *iface)
220{
221 int sec_chan, ok, j, first;
222 int allowed[] = { 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157,
223 184, 192 };
224 size_t k;
225
226 if (!iface->conf->secondary_channel)
227 return 1; /* HT40 not used */
228
229 sec_chan = iface->conf->channel + iface->conf->secondary_channel * 4;
230 wpa_printf(MSG_DEBUG, "HT40: control channel: %d "
231 "secondary channel: %d",
232 iface->conf->channel, sec_chan);
233
234 /* Verify that HT40 secondary channel is an allowed 20 MHz
235 * channel */
236 ok = 0;
237 for (j = 0; j < iface->current_mode->num_channels; j++) {
238 struct hostapd_channel_data *chan =
239 &iface->current_mode->channels[j];
240 if (!(chan->flag & HOSTAPD_CHAN_DISABLED) &&
241 chan->chan == sec_chan) {
242 ok = 1;
243 break;
244 }
245 }
246 if (!ok) {
247 wpa_printf(MSG_ERROR, "HT40 secondary channel %d not allowed",
248 sec_chan);
249 return 0;
250 }
251
252 /*
253 * Verify that HT40 primary,secondary channel pair is allowed per
254 * IEEE 802.11n Annex J. This is only needed for 5 GHz band since
255 * 2.4 GHz rules allow all cases where the secondary channel fits into
256 * the list of allowed channels (already checked above).
257 */
258 if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A)
259 return 1;
260
261 if (iface->conf->secondary_channel > 0)
262 first = iface->conf->channel;
263 else
264 first = sec_chan;
265
266 ok = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700267 for (k = 0; k < ARRAY_SIZE(allowed); k++) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700268 if (first == allowed[k]) {
269 ok = 1;
270 break;
271 }
272 }
273 if (!ok) {
274 wpa_printf(MSG_ERROR, "HT40 channel pair (%d, %d) not allowed",
275 iface->conf->channel,
276 iface->conf->secondary_channel);
277 return 0;
278 }
279
280 return 1;
281}
282
283
284static void ieee80211n_switch_pri_sec(struct hostapd_iface *iface)
285{
286 if (iface->conf->secondary_channel > 0) {
287 iface->conf->channel += 4;
288 iface->conf->secondary_channel = -1;
289 } else {
290 iface->conf->channel -= 4;
291 iface->conf->secondary_channel = 1;
292 }
293}
294
295
296static void ieee80211n_get_pri_sec_chan(struct wpa_scan_res *bss,
297 int *pri_chan, int *sec_chan)
298{
299 struct ieee80211_ht_operation *oper;
300 struct ieee802_11_elems elems;
301
302 *pri_chan = *sec_chan = 0;
303
304 ieee802_11_parse_elems((u8 *) (bss + 1), bss->ie_len, &elems, 0);
305 if (elems.ht_operation &&
306 elems.ht_operation_len >= sizeof(*oper)) {
307 oper = (struct ieee80211_ht_operation *) elems.ht_operation;
308 *pri_chan = oper->control_chan;
309 if (oper->ht_param & HT_INFO_HT_PARAM_REC_TRANS_CHNL_WIDTH) {
310 int sec = oper->ht_param &
311 HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK;
312 if (sec == HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
313 *sec_chan = *pri_chan + 4;
314 else if (sec == HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
315 *sec_chan = *pri_chan - 4;
316 }
317 }
318}
319
320
321static int ieee80211n_check_40mhz_5g(struct hostapd_iface *iface,
322 struct wpa_scan_results *scan_res)
323{
324 int pri_chan, sec_chan, pri_freq, sec_freq, pri_bss, sec_bss;
325 int bss_pri_chan, bss_sec_chan;
326 size_t i;
327 int match;
328
329 pri_chan = iface->conf->channel;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700330 sec_chan = pri_chan + iface->conf->secondary_channel * 4;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700331 pri_freq = hostapd_hw_get_freq(iface->bss[0], pri_chan);
332 if (iface->conf->secondary_channel > 0)
333 sec_freq = pri_freq + 20;
334 else
335 sec_freq = pri_freq - 20;
336
337 /*
338 * Switch PRI/SEC channels if Beacons were detected on selected SEC
339 * channel, but not on selected PRI channel.
340 */
341 pri_bss = sec_bss = 0;
342 for (i = 0; i < scan_res->num; i++) {
343 struct wpa_scan_res *bss = scan_res->res[i];
344 if (bss->freq == pri_freq)
345 pri_bss++;
346 else if (bss->freq == sec_freq)
347 sec_bss++;
348 }
349 if (sec_bss && !pri_bss) {
350 wpa_printf(MSG_INFO, "Switch own primary and secondary "
351 "channel to get secondary channel with no Beacons "
352 "from other BSSes");
353 ieee80211n_switch_pri_sec(iface);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700354 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700355 }
356
357 /*
358 * Match PRI/SEC channel with any existing HT40 BSS on the same
359 * channels that we are about to use (if already mixed order in
360 * existing BSSes, use own preference).
361 */
362 match = 0;
363 for (i = 0; i < scan_res->num; i++) {
364 struct wpa_scan_res *bss = scan_res->res[i];
365 ieee80211n_get_pri_sec_chan(bss, &bss_pri_chan, &bss_sec_chan);
366 if (pri_chan == bss_pri_chan &&
367 sec_chan == bss_sec_chan) {
368 match = 1;
369 break;
370 }
371 }
372 if (!match) {
373 for (i = 0; i < scan_res->num; i++) {
374 struct wpa_scan_res *bss = scan_res->res[i];
375 ieee80211n_get_pri_sec_chan(bss, &bss_pri_chan,
376 &bss_sec_chan);
377 if (pri_chan == bss_sec_chan &&
378 sec_chan == bss_pri_chan) {
379 wpa_printf(MSG_INFO, "Switch own primary and "
380 "secondary channel due to BSS "
381 "overlap with " MACSTR,
382 MAC2STR(bss->bssid));
383 ieee80211n_switch_pri_sec(iface);
384 break;
385 }
386 }
387 }
388
389 return 1;
390}
391
392
393static int ieee80211n_check_40mhz_2g4(struct hostapd_iface *iface,
394 struct wpa_scan_results *scan_res)
395{
396 int pri_freq, sec_freq;
397 int affected_start, affected_end;
398 size_t i;
399
400 pri_freq = hostapd_hw_get_freq(iface->bss[0], iface->conf->channel);
401 if (iface->conf->secondary_channel > 0)
402 sec_freq = pri_freq + 20;
403 else
404 sec_freq = pri_freq - 20;
405 affected_start = (pri_freq + sec_freq) / 2 - 25;
406 affected_end = (pri_freq + sec_freq) / 2 + 25;
407 wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
408 affected_start, affected_end);
409 for (i = 0; i < scan_res->num; i++) {
410 struct wpa_scan_res *bss = scan_res->res[i];
411 int pri = bss->freq;
412 int sec = pri;
413 int sec_chan, pri_chan;
414
415 ieee80211n_get_pri_sec_chan(bss, &pri_chan, &sec_chan);
416
417 if (sec_chan) {
418 if (sec_chan < pri_chan)
419 sec = pri - 20;
420 else
421 sec = pri + 20;
422 }
423
424 if ((pri < affected_start || pri > affected_end) &&
425 (sec < affected_start || sec > affected_end))
426 continue; /* not within affected channel range */
427
428 wpa_printf(MSG_DEBUG, "Neighboring BSS: " MACSTR
429 " freq=%d pri=%d sec=%d",
430 MAC2STR(bss->bssid), bss->freq, pri_chan, sec_chan);
431
432 if (sec_chan) {
433 if (pri_freq != pri || sec_freq != sec) {
434 wpa_printf(MSG_DEBUG, "40 MHz pri/sec "
435 "mismatch with BSS " MACSTR
436 " <%d,%d> (chan=%d%c) vs. <%d,%d>",
437 MAC2STR(bss->bssid),
438 pri, sec, pri_chan,
439 sec > pri ? '+' : '-',
440 pri_freq, sec_freq);
441 return 0;
442 }
443 }
444
445 /* TODO: 40 MHz intolerant */
446 }
447
448 return 1;
449}
450
451
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700452static void ieee80211n_check_scan(struct hostapd_iface *iface)
453{
454 struct wpa_scan_results *scan_res;
455 int oper40;
456 int res;
457
458 /* Check list of neighboring BSSes (from scan) to see whether 40 MHz is
Dmitry Shmidt04949592012-07-19 12:16:46 -0700459 * allowed per IEEE Std 802.11-2012, 10.15.3.2 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700460
461 iface->scan_cb = NULL;
462
463 scan_res = hostapd_driver_get_scan_results(iface->bss[0]);
464 if (scan_res == NULL) {
465 hostapd_setup_interface_complete(iface, 1);
466 return;
467 }
468
469 if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A)
470 oper40 = ieee80211n_check_40mhz_5g(iface, scan_res);
471 else
472 oper40 = ieee80211n_check_40mhz_2g4(iface, scan_res);
473 wpa_scan_results_free(scan_res);
474
475 if (!oper40) {
476 wpa_printf(MSG_INFO, "20/40 MHz operation not permitted on "
477 "channel pri=%d sec=%d based on overlapping BSSes",
478 iface->conf->channel,
479 iface->conf->channel +
480 iface->conf->secondary_channel * 4);
481 iface->conf->secondary_channel = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700482 }
483
484 res = ieee80211n_allowed_ht40_channel_pair(iface);
485 hostapd_setup_interface_complete(iface, !res);
486}
487
488
Dmitry Shmidt04949592012-07-19 12:16:46 -0700489static void ieee80211n_scan_channels_2g4(struct hostapd_iface *iface,
490 struct wpa_driver_scan_params *params)
491{
492 /* Scan only the affected frequency range */
493 int pri_freq, sec_freq;
494 int affected_start, affected_end;
495 int i, pos;
496 struct hostapd_hw_modes *mode;
497
498 if (iface->current_mode == NULL)
499 return;
500
501 pri_freq = hostapd_hw_get_freq(iface->bss[0], iface->conf->channel);
502 if (iface->conf->secondary_channel > 0)
503 sec_freq = pri_freq + 20;
504 else
505 sec_freq = pri_freq - 20;
506 affected_start = (pri_freq + sec_freq) / 2 - 25;
507 affected_end = (pri_freq + sec_freq) / 2 + 25;
508 wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
509 affected_start, affected_end);
510
511 mode = iface->current_mode;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700512 params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
Dmitry Shmidt04949592012-07-19 12:16:46 -0700513 if (params->freqs == NULL)
514 return;
515 pos = 0;
516
517 for (i = 0; i < mode->num_channels; i++) {
518 struct hostapd_channel_data *chan = &mode->channels[i];
519 if (chan->flag & HOSTAPD_CHAN_DISABLED)
520 continue;
521 if (chan->freq < affected_start ||
522 chan->freq > affected_end)
523 continue;
524 params->freqs[pos++] = chan->freq;
525 }
526}
527
528
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800529static void ieee80211n_scan_channels_5g(struct hostapd_iface *iface,
530 struct wpa_driver_scan_params *params)
531{
532 /* Scan only the affected frequency range */
533 int pri_freq;
534 int affected_start, affected_end;
535 int i, pos;
536 struct hostapd_hw_modes *mode;
537
538 if (iface->current_mode == NULL)
539 return;
540
541 pri_freq = hostapd_hw_get_freq(iface->bss[0], iface->conf->channel);
542 if (iface->conf->secondary_channel > 0) {
543 affected_start = pri_freq - 10;
544 affected_end = pri_freq + 30;
545 } else {
546 affected_start = pri_freq - 30;
547 affected_end = pri_freq + 10;
548 }
549 wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
550 affected_start, affected_end);
551
552 mode = iface->current_mode;
553 params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
554 if (params->freqs == NULL)
555 return;
556 pos = 0;
557
558 for (i = 0; i < mode->num_channels; i++) {
559 struct hostapd_channel_data *chan = &mode->channels[i];
560 if (chan->flag & HOSTAPD_CHAN_DISABLED)
561 continue;
562 if (chan->freq < affected_start ||
563 chan->freq > affected_end)
564 continue;
565 params->freqs[pos++] = chan->freq;
566 }
567}
568
569
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700570static int ieee80211n_check_40mhz(struct hostapd_iface *iface)
571{
572 struct wpa_driver_scan_params params;
573
574 if (!iface->conf->secondary_channel)
575 return 0; /* HT40 not used */
576
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800577 hostapd_set_state(iface, HAPD_IFACE_HT_SCAN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700578 wpa_printf(MSG_DEBUG, "Scan for neighboring BSSes prior to enabling "
579 "40 MHz channel");
580 os_memset(&params, 0, sizeof(params));
Dmitry Shmidt04949592012-07-19 12:16:46 -0700581 if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
582 ieee80211n_scan_channels_2g4(iface, &params);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800583 else
584 ieee80211n_scan_channels_5g(iface, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700585 if (hostapd_driver_scan(iface->bss[0], &params) < 0) {
586 wpa_printf(MSG_ERROR, "Failed to request a scan of "
587 "neighboring BSSes");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700588 os_free(params.freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700589 return -1;
590 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700591 os_free(params.freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700592
593 iface->scan_cb = ieee80211n_check_scan;
594 return 1;
595}
596
597
598static int ieee80211n_supported_ht_capab(struct hostapd_iface *iface)
599{
600 u16 hw = iface->current_mode->ht_capab;
601 u16 conf = iface->conf->ht_capab;
602
603 if ((conf & HT_CAP_INFO_LDPC_CODING_CAP) &&
604 !(hw & HT_CAP_INFO_LDPC_CODING_CAP)) {
605 wpa_printf(MSG_ERROR, "Driver does not support configured "
606 "HT capability [LDPC]");
607 return 0;
608 }
609
610 if ((conf & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) &&
611 !(hw & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
612 wpa_printf(MSG_ERROR, "Driver does not support configured "
613 "HT capability [HT40*]");
614 return 0;
615 }
616
617 if ((conf & HT_CAP_INFO_SMPS_MASK) != (hw & HT_CAP_INFO_SMPS_MASK) &&
618 (conf & HT_CAP_INFO_SMPS_MASK) != HT_CAP_INFO_SMPS_DISABLED) {
619 wpa_printf(MSG_ERROR, "Driver does not support configured "
620 "HT capability [SMPS-*]");
621 return 0;
622 }
623
624 if ((conf & HT_CAP_INFO_GREEN_FIELD) &&
625 !(hw & HT_CAP_INFO_GREEN_FIELD)) {
626 wpa_printf(MSG_ERROR, "Driver does not support configured "
627 "HT capability [GF]");
628 return 0;
629 }
630
631 if ((conf & HT_CAP_INFO_SHORT_GI20MHZ) &&
632 !(hw & HT_CAP_INFO_SHORT_GI20MHZ)) {
633 wpa_printf(MSG_ERROR, "Driver does not support configured "
634 "HT capability [SHORT-GI-20]");
635 return 0;
636 }
637
638 if ((conf & HT_CAP_INFO_SHORT_GI40MHZ) &&
639 !(hw & HT_CAP_INFO_SHORT_GI40MHZ)) {
640 wpa_printf(MSG_ERROR, "Driver does not support configured "
641 "HT capability [SHORT-GI-40]");
642 return 0;
643 }
644
645 if ((conf & HT_CAP_INFO_TX_STBC) && !(hw & HT_CAP_INFO_TX_STBC)) {
646 wpa_printf(MSG_ERROR, "Driver does not support configured "
647 "HT capability [TX-STBC]");
648 return 0;
649 }
650
651 if ((conf & HT_CAP_INFO_RX_STBC_MASK) >
652 (hw & HT_CAP_INFO_RX_STBC_MASK)) {
653 wpa_printf(MSG_ERROR, "Driver does not support configured "
654 "HT capability [RX-STBC*]");
655 return 0;
656 }
657
658 if ((conf & HT_CAP_INFO_DELAYED_BA) &&
659 !(hw & HT_CAP_INFO_DELAYED_BA)) {
660 wpa_printf(MSG_ERROR, "Driver does not support configured "
661 "HT capability [DELAYED-BA]");
662 return 0;
663 }
664
665 if ((conf & HT_CAP_INFO_MAX_AMSDU_SIZE) &&
666 !(hw & HT_CAP_INFO_MAX_AMSDU_SIZE)) {
667 wpa_printf(MSG_ERROR, "Driver does not support configured "
668 "HT capability [MAX-AMSDU-7935]");
669 return 0;
670 }
671
672 if ((conf & HT_CAP_INFO_DSSS_CCK40MHZ) &&
673 !(hw & HT_CAP_INFO_DSSS_CCK40MHZ)) {
674 wpa_printf(MSG_ERROR, "Driver does not support configured "
675 "HT capability [DSSS_CCK-40]");
676 return 0;
677 }
678
679 if ((conf & HT_CAP_INFO_PSMP_SUPP) && !(hw & HT_CAP_INFO_PSMP_SUPP)) {
680 wpa_printf(MSG_ERROR, "Driver does not support configured "
681 "HT capability [PSMP]");
682 return 0;
683 }
684
685 if ((conf & HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT) &&
686 !(hw & HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT)) {
687 wpa_printf(MSG_ERROR, "Driver does not support configured "
688 "HT capability [LSIG-TXOP-PROT]");
689 return 0;
690 }
691
692 return 1;
693}
694
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700695
696#ifdef CONFIG_IEEE80211AC
697
698static int ieee80211ac_cap_check(u32 hw, u32 conf, u32 cap, const char *name)
699{
700 u32 req_cap = conf & cap;
701
702 /*
703 * Make sure we support all requested capabilities.
704 * NOTE: We assume that 'cap' represents a capability mask,
705 * not a discrete value.
706 */
707 if ((hw & req_cap) != req_cap) {
708 wpa_printf(MSG_ERROR, "Driver does not support configured VHT capability [%s]",
709 name);
710 return 0;
711 }
712 return 1;
713}
714
715
716static int ieee80211ac_cap_check_max(u32 hw, u32 conf, u32 cap,
717 const char *name)
718{
719 u32 hw_max = hw & cap;
720 u32 conf_val = conf & cap;
721
722 if (conf_val > hw_max) {
723 int offset = find_first_bit(cap);
724 wpa_printf(MSG_ERROR, "Configured VHT capability [%s] exceeds max value supported by the driver (%d > %d)",
725 name, conf_val >> offset, hw_max >> offset);
726 return 0;
727 }
728 return 1;
729}
730
731
732static int ieee80211ac_supported_vht_capab(struct hostapd_iface *iface)
733{
734 u32 hw = iface->current_mode->vht_capab;
735 u32 conf = iface->conf->vht_capab;
736
737 wpa_printf(MSG_DEBUG, "hw vht capab: 0x%x, conf vht capab: 0x%x",
738 hw, conf);
739
740#define VHT_CAP_CHECK(cap) \
741 do { \
742 if (!ieee80211ac_cap_check(hw, conf, cap, #cap)) \
743 return 0; \
744 } while (0)
745
746#define VHT_CAP_CHECK_MAX(cap) \
747 do { \
748 if (!ieee80211ac_cap_check_max(hw, conf, cap, #cap)) \
749 return 0; \
750 } while (0)
751
752 VHT_CAP_CHECK_MAX(VHT_CAP_MAX_MPDU_LENGTH_MASK);
753 VHT_CAP_CHECK(VHT_CAP_SUPP_CHAN_WIDTH_160MHZ);
754 VHT_CAP_CHECK(VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ);
755 VHT_CAP_CHECK(VHT_CAP_RXLDPC);
756 VHT_CAP_CHECK(VHT_CAP_SHORT_GI_80);
757 VHT_CAP_CHECK(VHT_CAP_SHORT_GI_160);
758 VHT_CAP_CHECK(VHT_CAP_TXSTBC);
759 VHT_CAP_CHECK_MAX(VHT_CAP_RXSTBC_MASK);
760 VHT_CAP_CHECK(VHT_CAP_SU_BEAMFORMER_CAPABLE);
761 VHT_CAP_CHECK(VHT_CAP_SU_BEAMFORMEE_CAPABLE);
762 VHT_CAP_CHECK_MAX(VHT_CAP_BEAMFORMEE_STS_MAX);
763 VHT_CAP_CHECK_MAX(VHT_CAP_SOUNDING_DIMENSION_MAX);
764 VHT_CAP_CHECK(VHT_CAP_MU_BEAMFORMER_CAPABLE);
765 VHT_CAP_CHECK(VHT_CAP_MU_BEAMFORMEE_CAPABLE);
766 VHT_CAP_CHECK(VHT_CAP_VHT_TXOP_PS);
767 VHT_CAP_CHECK(VHT_CAP_HTC_VHT);
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800768 VHT_CAP_CHECK_MAX(VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700769 VHT_CAP_CHECK(VHT_CAP_VHT_LINK_ADAPTATION_VHT_UNSOL_MFB);
770 VHT_CAP_CHECK(VHT_CAP_VHT_LINK_ADAPTATION_VHT_MRQ_MFB);
771 VHT_CAP_CHECK(VHT_CAP_RX_ANTENNA_PATTERN);
772 VHT_CAP_CHECK(VHT_CAP_TX_ANTENNA_PATTERN);
773
774#undef VHT_CAP_CHECK
775#undef VHT_CAP_CHECK_MAX
776
777 return 1;
778}
779#endif /* CONFIG_IEEE80211AC */
780
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700781#endif /* CONFIG_IEEE80211N */
782
783
784int hostapd_check_ht_capab(struct hostapd_iface *iface)
785{
786#ifdef CONFIG_IEEE80211N
787 int ret;
788 if (!iface->conf->ieee80211n)
789 return 0;
790 if (!ieee80211n_supported_ht_capab(iface))
791 return -1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700792#ifdef CONFIG_IEEE80211AC
793 if (!ieee80211ac_supported_vht_capab(iface))
794 return -1;
795#endif /* CONFIG_IEEE80211AC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700796 ret = ieee80211n_check_40mhz(iface);
797 if (ret)
798 return ret;
799 if (!ieee80211n_allowed_ht40_channel_pair(iface))
800 return -1;
801#endif /* CONFIG_IEEE80211N */
802
803 return 0;
804}
805
806
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700807static int hostapd_is_usable_chan(struct hostapd_iface *iface,
808 int channel, int primary)
809{
810 int i;
811 struct hostapd_channel_data *chan;
812
813 for (i = 0; i < iface->current_mode->num_channels; i++) {
814 chan = &iface->current_mode->channels[i];
815 if (chan->chan != channel)
816 continue;
817
818 if (!(chan->flag & HOSTAPD_CHAN_DISABLED))
819 return 1;
820
821 wpa_printf(MSG_DEBUG,
822 "%schannel [%i] (%i) is disabled for use in AP mode, flags: 0x%x%s%s%s",
823 primary ? "" : "Configured HT40 secondary ",
824 i, chan->chan, chan->flag,
825 chan->flag & HOSTAPD_CHAN_NO_IBSS ? " NO-IBSS" : "",
826 chan->flag & HOSTAPD_CHAN_PASSIVE_SCAN ?
827 " PASSIVE-SCAN" : "",
828 chan->flag & HOSTAPD_CHAN_RADAR ? " RADAR" : "");
829 }
830
831 return 0;
832}
833
834
835static int hostapd_is_usable_chans(struct hostapd_iface *iface)
836{
837 if (!hostapd_is_usable_chan(iface, iface->conf->channel, 1))
838 return 0;
839
840 if (!iface->conf->secondary_channel)
841 return 1;
842
843 return hostapd_is_usable_chan(iface, iface->conf->channel +
844 iface->conf->secondary_channel * 4, 0);
845}
846
847
848static enum hostapd_chan_status
849hostapd_check_chans(struct hostapd_iface *iface)
850{
851 if (iface->conf->channel) {
852 if (hostapd_is_usable_chans(iface))
853 return HOSTAPD_CHAN_VALID;
854 else
855 return HOSTAPD_CHAN_INVALID;
856 }
857
858 /*
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700859 * The user set channel=0 or channel=acs_survey
860 * which is used to trigger ACS.
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700861 */
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700862
863 switch (acs_init(iface)) {
864 case HOSTAPD_CHAN_ACS:
865 return HOSTAPD_CHAN_ACS;
866 case HOSTAPD_CHAN_VALID:
867 case HOSTAPD_CHAN_INVALID:
868 default:
869 return HOSTAPD_CHAN_INVALID;
870 }
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700871}
872
873
874static void hostapd_notify_bad_chans(struct hostapd_iface *iface)
875{
876 hostapd_logger(iface->bss[0], NULL,
877 HOSTAPD_MODULE_IEEE80211,
878 HOSTAPD_LEVEL_WARNING,
879 "Configured channel (%d) not found from the "
880 "channel list of current mode (%d) %s",
881 iface->conf->channel,
882 iface->current_mode->mode,
883 hostapd_hw_mode_txt(iface->current_mode->mode));
884 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
885 HOSTAPD_LEVEL_WARNING,
886 "Hardware does not support configured channel");
887}
888
889
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700890int hostapd_acs_completed(struct hostapd_iface *iface, int err)
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700891{
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700892 int ret = -1;
893
894 if (err)
895 goto out;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700896
897 switch (hostapd_check_chans(iface)) {
898 case HOSTAPD_CHAN_VALID:
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800899 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO,
900 ACS_EVENT_COMPLETED "freq=%d channel=%d",
901 hostapd_hw_get_freq(iface->bss[0],
902 iface->conf->channel),
903 iface->conf->channel);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700904 break;
905 case HOSTAPD_CHAN_ACS:
906 wpa_printf(MSG_ERROR, "ACS error - reported complete, but no result available");
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800907 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, ACS_EVENT_FAILED);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700908 hostapd_notify_bad_chans(iface);
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700909 goto out;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700910 case HOSTAPD_CHAN_INVALID:
911 default:
912 wpa_printf(MSG_ERROR, "ACS picked unusable channels");
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800913 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, ACS_EVENT_FAILED);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700914 hostapd_notify_bad_chans(iface);
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700915 goto out;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700916 }
917
918 ret = hostapd_check_ht_capab(iface);
919 if (ret < 0)
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700920 goto out;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700921 if (ret == 1) {
922 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback");
923 return 0;
924 }
925
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700926 ret = 0;
927out:
928 return hostapd_setup_interface_complete(iface, ret);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700929}
930
931
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700932/**
933 * hostapd_select_hw_mode - Select the hardware mode
934 * @iface: Pointer to interface data.
Jouni Malinen87fd2792011-05-16 18:35:42 +0300935 * Returns: 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700936 *
937 * Sets up the hardware mode, channel, rates, and passive scanning
938 * based on the configuration.
939 */
940int hostapd_select_hw_mode(struct hostapd_iface *iface)
941{
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700942 int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700943
944 if (iface->num_hw_features < 1)
945 return -1;
946
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800947 if ((iface->conf->hw_mode == HOSTAPD_MODE_IEEE80211G ||
948 iface->conf->ieee80211n || iface->conf->ieee80211ac) &&
949 iface->conf->channel == 14) {
950 wpa_printf(MSG_INFO, "Disable OFDM/HT/VHT on channel 14");
951 iface->conf->hw_mode = HOSTAPD_MODE_IEEE80211B;
952 iface->conf->ieee80211n = 0;
953 iface->conf->ieee80211ac = 0;
954 }
955
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700956 iface->current_mode = NULL;
957 for (i = 0; i < iface->num_hw_features; i++) {
958 struct hostapd_hw_modes *mode = &iface->hw_features[i];
959 if (mode->mode == iface->conf->hw_mode) {
960 iface->current_mode = mode;
961 break;
962 }
963 }
964
965 if (iface->current_mode == NULL) {
966 wpa_printf(MSG_ERROR, "Hardware does not support configured "
967 "mode");
968 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
969 HOSTAPD_LEVEL_WARNING,
970 "Hardware does not support configured mode "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800971 "(%d) (hw_mode in hostapd.conf)",
972 (int) iface->conf->hw_mode);
Jouni Malinen87fd2792011-05-16 18:35:42 +0300973 return -2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700974 }
975
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700976 switch (hostapd_check_chans(iface)) {
977 case HOSTAPD_CHAN_VALID:
978 return 0;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700979 case HOSTAPD_CHAN_ACS: /* ACS will run and later complete */
980 return 1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700981 case HOSTAPD_CHAN_INVALID:
982 default:
983 hostapd_notify_bad_chans(iface);
Jouni Malinen87fd2792011-05-16 18:35:42 +0300984 return -3;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700985 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700986}
987
988
989const char * hostapd_hw_mode_txt(int mode)
990{
991 switch (mode) {
992 case HOSTAPD_MODE_IEEE80211A:
993 return "IEEE 802.11a";
994 case HOSTAPD_MODE_IEEE80211B:
995 return "IEEE 802.11b";
996 case HOSTAPD_MODE_IEEE80211G:
997 return "IEEE 802.11g";
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800998 case HOSTAPD_MODE_IEEE80211AD:
999 return "IEEE 802.11ad";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001000 default:
1001 return "UNKNOWN";
1002 }
1003}
1004
1005
1006int hostapd_hw_get_freq(struct hostapd_data *hapd, int chan)
1007{
1008 int i;
1009
1010 if (!hapd->iface->current_mode)
1011 return 0;
1012
1013 for (i = 0; i < hapd->iface->current_mode->num_channels; i++) {
1014 struct hostapd_channel_data *ch =
1015 &hapd->iface->current_mode->channels[i];
1016 if (ch->chan == chan)
1017 return ch->freq;
1018 }
1019
1020 return 0;
1021}
1022
1023
1024int hostapd_hw_get_channel(struct hostapd_data *hapd, int freq)
1025{
1026 int i;
1027
1028 if (!hapd->iface->current_mode)
1029 return 0;
1030
1031 for (i = 0; i < hapd->iface->current_mode->num_channels; i++) {
1032 struct hostapd_channel_data *ch =
1033 &hapd->iface->current_mode->channels[i];
1034 if (ch->freq == freq)
1035 return ch->chan;
1036 }
1037
1038 return 0;
1039}