]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/ap/hw_features.c
hostapd: Add AP-ENABLED/DISABLED ctrl_iface events
[thirdparty/hostap.git] / src / ap / hw_features.c
CommitLineData
6fc6879b
JM
1/*
2 * hostapd / Hardware feature query and different modes
3 * Copyright 2002-2003, Instant802 Networks, Inc.
4 * Copyright 2005-2006, Devicescape Software, Inc.
7fa56233 5 * Copyright (c) 2008-2012, Jouni Malinen <j@w1.fi>
6fc6879b
JM
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * Alternatively, this software may be distributed under the terms of BSD
12 * license.
13 *
14 * See README and COPYING for more details.
15 */
16
1057d78e 17#include "utils/includes.h"
6fc6879b 18
1057d78e
JM
19#include "utils/common.h"
20#include "utils/eloop.h"
90973fb2
JM
21#include "common/ieee802_11_defs.h"
22#include "common/ieee802_11_common.h"
ae134e1d 23#include "common/wpa_ctrl.h"
6e6e8c31 24#include "drivers/driver.h"
6226e38d
JM
25#include "hostapd.h"
26#include "ap_config.h"
6e6e8c31 27#include "ap_drv_ops.h"
50f4f2a0 28#include "acs.h"
6226e38d 29#include "hw_features.h"
6fc6879b
JM
30
31
32void hostapd_free_hw_features(struct hostapd_hw_modes *hw_features,
33 size_t num_hw_features)
34{
35 size_t i;
36
37 if (hw_features == NULL)
38 return;
39
40 for (i = 0; i < num_hw_features; i++) {
41 os_free(hw_features[i].channels);
42 os_free(hw_features[i].rates);
43 }
44
45 os_free(hw_features);
46}
47
48
e76da505
JD
49#ifndef CONFIG_NO_STDOUT_DEBUG
50static char * dfs_info(struct hostapd_channel_data *chan)
51{
52 static char info[256];
53 char *state;
54
55 switch (chan->flag & HOSTAPD_CHAN_DFS_MASK) {
56 case HOSTAPD_CHAN_DFS_UNKNOWN:
57 state = "unknown";
58 break;
59 case HOSTAPD_CHAN_DFS_USABLE:
60 state = "usable";
61 break;
62 case HOSTAPD_CHAN_DFS_UNAVAILABLE:
63 state = "unavailable";
64 break;
65 case HOSTAPD_CHAN_DFS_AVAILABLE:
66 state = "available";
67 break;
68 default:
69 return "";
70 }
71 os_snprintf(info, sizeof(info), " (DFS state = %s)", state);
72 info[sizeof(info) - 1] = '\0';
73
74 return info;
75}
76#endif /* CONFIG_NO_STDOUT_DEBUG */
77
78
6fc6879b
JM
79int hostapd_get_hw_features(struct hostapd_iface *iface)
80{
81 struct hostapd_data *hapd = iface->bss[0];
82 int ret = 0, i, j;
83 u16 num_modes, flags;
84 struct hostapd_hw_modes *modes;
85
85141289
JM
86 if (hostapd_drv_none(hapd))
87 return -1;
6fc6879b
JM
88 modes = hostapd_get_hw_feature_data(hapd, &num_modes, &flags);
89 if (modes == NULL) {
90 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
91 HOSTAPD_LEVEL_DEBUG,
92 "Fetching hardware channel/rate support not "
93 "supported.");
94 return -1;
95 }
96
97 iface->hw_flags = flags;
98
99 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
100 iface->hw_features = modes;
101 iface->num_hw_features = num_modes;
102
103 for (i = 0; i < num_modes; i++) {
104 struct hostapd_hw_modes *feature = &modes[i];
e76da505
JD
105 int dfs_enabled = hapd->iconf->ieee80211h &&
106 (iface->drv_flags & WPA_DRIVER_FLAGS_RADAR);
107
6fc6879b
JM
108 /* set flag for channels we can use in current regulatory
109 * domain */
110 for (j = 0; j < feature->num_channels; j++) {
e76da505
JD
111 int dfs = 0;
112
10b83bd7
JM
113 /*
114 * Disable all channels that are marked not to allow
e76da505
JD
115 * IBSS operation or active scanning.
116 * Use radar channels only if the driver supports DFS.
10b83bd7 117 */
e76da505
JD
118 if ((feature->channels[j].flag &
119 HOSTAPD_CHAN_RADAR) && dfs_enabled) {
120 dfs = 1;
121 } else if (feature->channels[j].flag &
122 (HOSTAPD_CHAN_NO_IBSS |
123 HOSTAPD_CHAN_PASSIVE_SCAN |
124 HOSTAPD_CHAN_RADAR)) {
6fc6879b 125 feature->channels[j].flag |=
10b83bd7 126 HOSTAPD_CHAN_DISABLED;
e76da505
JD
127 }
128
10b83bd7
JM
129 if (feature->channels[j].flag & HOSTAPD_CHAN_DISABLED)
130 continue;
e76da505 131
10b83bd7 132 wpa_printf(MSG_MSGDUMP, "Allowed channel: mode=%d "
e76da505 133 "chan=%d freq=%d MHz max_tx_power=%d dBm%s",
10b83bd7
JM
134 feature->mode,
135 feature->channels[j].chan,
bf01d8bc 136 feature->channels[j].freq,
e76da505
JD
137 feature->channels[j].max_tx_power,
138 dfs ? dfs_info(&feature->channels[j]) : "");
6fc6879b
JM
139 }
140 }
141
142 return ret;
143}
144
145
34445d12 146int hostapd_prepare_rates(struct hostapd_iface *iface,
5a5009dc 147 struct hostapd_hw_modes *mode)
6fc6879b
JM
148{
149 int i, num_basic_rates = 0;
150 int basic_rates_a[] = { 60, 120, 240, -1 };
151 int basic_rates_b[] = { 10, 20, -1 };
152 int basic_rates_g[] = { 10, 20, 55, 110, -1 };
153 int *basic_rates;
154
34445d12
JM
155 if (iface->conf->basic_rates)
156 basic_rates = iface->conf->basic_rates;
6fc6879b
JM
157 else switch (mode->mode) {
158 case HOSTAPD_MODE_IEEE80211A:
159 basic_rates = basic_rates_a;
160 break;
161 case HOSTAPD_MODE_IEEE80211B:
162 basic_rates = basic_rates_b;
163 break;
164 case HOSTAPD_MODE_IEEE80211G:
165 basic_rates = basic_rates_g;
166 break;
7829894c
VK
167 case HOSTAPD_MODE_IEEE80211AD:
168 return 0; /* No basic rates for 11ad */
6fc6879b
JM
169 default:
170 return -1;
171 }
172
e5693c47
JM
173 i = 0;
174 while (basic_rates[i] >= 0)
175 i++;
f0898e95
JM
176 if (i)
177 i++; /* -1 termination */
34445d12 178 os_free(iface->basic_rates);
78018ae9 179 iface->basic_rates = os_malloc(i * sizeof(int));
34445d12 180 if (iface->basic_rates)
78018ae9 181 os_memcpy(iface->basic_rates, basic_rates, i * sizeof(int));
6fc6879b 182
34445d12
JM
183 os_free(iface->current_rates);
184 iface->num_rates = 0;
6fc6879b 185
34445d12 186 iface->current_rates =
f9884c09 187 os_calloc(mode->num_rates, sizeof(struct hostapd_rate_data));
34445d12 188 if (!iface->current_rates) {
6fc6879b
JM
189 wpa_printf(MSG_ERROR, "Failed to allocate memory for rate "
190 "table.");
191 return -1;
192 }
193
194 for (i = 0; i < mode->num_rates; i++) {
195 struct hostapd_rate_data *rate;
196
34445d12
JM
197 if (iface->conf->supported_rates &&
198 !hostapd_rate_found(iface->conf->supported_rates,
fb7842aa 199 mode->rates[i]))
6fc6879b
JM
200 continue;
201
34445d12 202 rate = &iface->current_rates[iface->num_rates];
fb7842aa 203 rate->rate = mode->rates[i];
6fc6879b
JM
204 if (hostapd_rate_found(basic_rates, rate->rate)) {
205 rate->flags |= HOSTAPD_RATE_BASIC;
206 num_basic_rates++;
fb7842aa 207 }
6fc6879b 208 wpa_printf(MSG_DEBUG, "RATE[%d] rate=%d flags=0x%x",
34445d12
JM
209 iface->num_rates, rate->rate, rate->flags);
210 iface->num_rates++;
6fc6879b
JM
211 }
212
34445d12
JM
213 if ((iface->num_rates == 0 || num_basic_rates == 0) &&
214 (!iface->conf->ieee80211n || !iface->conf->require_ht)) {
6fc6879b
JM
215 wpa_printf(MSG_ERROR, "No rates remaining in supported/basic "
216 "rate sets (%d,%d).",
34445d12 217 iface->num_rates, num_basic_rates);
6fc6879b
JM
218 return -1;
219 }
220
221 return 0;
222}
223
224
3e0cb2c5
JM
225#ifdef CONFIG_IEEE80211N
226static int ieee80211n_allowed_ht40_channel_pair(struct hostapd_iface *iface)
227{
228 int sec_chan, ok, j, first;
229 int allowed[] = { 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157,
230 184, 192 };
231 size_t k;
232
233 if (!iface->conf->secondary_channel)
234 return 1; /* HT40 not used */
235
236 sec_chan = iface->conf->channel + iface->conf->secondary_channel * 4;
237 wpa_printf(MSG_DEBUG, "HT40: control channel: %d "
238 "secondary channel: %d",
239 iface->conf->channel, sec_chan);
240
241 /* Verify that HT40 secondary channel is an allowed 20 MHz
242 * channel */
243 ok = 0;
244 for (j = 0; j < iface->current_mode->num_channels; j++) {
245 struct hostapd_channel_data *chan =
246 &iface->current_mode->channels[j];
247 if (!(chan->flag & HOSTAPD_CHAN_DISABLED) &&
248 chan->chan == sec_chan) {
249 ok = 1;
250 break;
251 }
252 }
253 if (!ok) {
254 wpa_printf(MSG_ERROR, "HT40 secondary channel %d not allowed",
255 sec_chan);
256 return 0;
257 }
258
259 /*
260 * Verify that HT40 primary,secondary channel pair is allowed per
261 * IEEE 802.11n Annex J. This is only needed for 5 GHz band since
262 * 2.4 GHz rules allow all cases where the secondary channel fits into
263 * the list of allowed channels (already checked above).
264 */
265 if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A)
266 return 1;
267
268 if (iface->conf->secondary_channel > 0)
269 first = iface->conf->channel;
270 else
271 first = sec_chan;
272
273 ok = 0;
e7ecab4a 274 for (k = 0; k < ARRAY_SIZE(allowed); k++) {
3e0cb2c5
JM
275 if (first == allowed[k]) {
276 ok = 1;
277 break;
278 }
279 }
280 if (!ok) {
281 wpa_printf(MSG_ERROR, "HT40 channel pair (%d, %d) not allowed",
282 iface->conf->channel,
283 iface->conf->secondary_channel);
284 return 0;
285 }
286
287 return 1;
288}
30fd5f14
JM
289
290
5eb4e3d0
JM
291static void ieee80211n_switch_pri_sec(struct hostapd_iface *iface)
292{
293 if (iface->conf->secondary_channel > 0) {
294 iface->conf->channel += 4;
295 iface->conf->secondary_channel = -1;
296 } else {
297 iface->conf->channel -= 4;
298 iface->conf->secondary_channel = 1;
299 }
300}
301
302
ad1e68e6
JM
303static void ieee80211n_get_pri_sec_chan(struct wpa_scan_res *bss,
304 int *pri_chan, int *sec_chan)
305{
306 struct ieee80211_ht_operation *oper;
307 struct ieee802_11_elems elems;
308
309 *pri_chan = *sec_chan = 0;
310
311 ieee802_11_parse_elems((u8 *) (bss + 1), bss->ie_len, &elems, 0);
312 if (elems.ht_operation &&
313 elems.ht_operation_len >= sizeof(*oper)) {
314 oper = (struct ieee80211_ht_operation *) elems.ht_operation;
315 *pri_chan = oper->control_chan;
316 if (oper->ht_param & HT_INFO_HT_PARAM_REC_TRANS_CHNL_WIDTH) {
41619262
JM
317 int sec = oper->ht_param &
318 HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK;
319 if (sec == HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
ad1e68e6 320 *sec_chan = *pri_chan + 4;
41619262 321 else if (sec == HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
ad1e68e6
JM
322 *sec_chan = *pri_chan - 4;
323 }
324 }
325}
326
327
328static int ieee80211n_check_40mhz_5g(struct hostapd_iface *iface,
329 struct wpa_scan_results *scan_res)
5eb4e3d0
JM
330{
331 int pri_chan, sec_chan, pri_freq, sec_freq, pri_bss, sec_bss;
ad1e68e6
JM
332 int bss_pri_chan, bss_sec_chan;
333 size_t i;
5eb4e3d0
JM
334 int match;
335
336 pri_chan = iface->conf->channel;
337 sec_chan = iface->conf->secondary_channel * 4;
338 pri_freq = hostapd_hw_get_freq(iface->bss[0], pri_chan);
339 if (iface->conf->secondary_channel > 0)
340 sec_freq = pri_freq + 20;
341 else
342 sec_freq = pri_freq - 20;
343
5eb4e3d0
JM
344 /*
345 * Switch PRI/SEC channels if Beacons were detected on selected SEC
346 * channel, but not on selected PRI channel.
347 */
348 pri_bss = sec_bss = 0;
ad1e68e6
JM
349 for (i = 0; i < scan_res->num; i++) {
350 struct wpa_scan_res *bss = scan_res->res[i];
351 if (bss->freq == pri_freq)
5eb4e3d0 352 pri_bss++;
ad1e68e6 353 else if (bss->freq == sec_freq)
5eb4e3d0
JM
354 sec_bss++;
355 }
356 if (sec_bss && !pri_bss) {
357 wpa_printf(MSG_INFO, "Switch own primary and secondary "
358 "channel to get secondary channel with no Beacons "
359 "from other BSSes");
360 ieee80211n_switch_pri_sec(iface);
361 }
362
363 /*
364 * Match PRI/SEC channel with any existing HT40 BSS on the same
365 * channels that we are about to use (if already mixed order in
366 * existing BSSes, use own preference).
367 */
368 match = 0;
ad1e68e6
JM
369 for (i = 0; i < scan_res->num; i++) {
370 struct wpa_scan_res *bss = scan_res->res[i];
371 ieee80211n_get_pri_sec_chan(bss, &bss_pri_chan, &bss_sec_chan);
372 if (pri_chan == bss_pri_chan &&
373 sec_chan == bss_sec_chan) {
5eb4e3d0
JM
374 match = 1;
375 break;
376 }
377 }
378 if (!match) {
ad1e68e6
JM
379 for (i = 0; i < scan_res->num; i++) {
380 struct wpa_scan_res *bss = scan_res->res[i];
c6313c75
FF
381 ieee80211n_get_pri_sec_chan(bss, &bss_pri_chan,
382 &bss_sec_chan);
ad1e68e6
JM
383 if (pri_chan == bss_sec_chan &&
384 sec_chan == bss_pri_chan) {
5eb4e3d0
JM
385 wpa_printf(MSG_INFO, "Switch own primary and "
386 "secondary channel due to BSS "
387 "overlap with " MACSTR,
ad1e68e6 388 MAC2STR(bss->bssid));
5eb4e3d0
JM
389 ieee80211n_switch_pri_sec(iface);
390 break;
391 }
392 }
393 }
394
395 return 1;
396}
397
398
ad1e68e6
JM
399static int ieee80211n_check_40mhz_2g4(struct hostapd_iface *iface,
400 struct wpa_scan_results *scan_res)
5eb4e3d0
JM
401{
402 int pri_freq, sec_freq;
403 int affected_start, affected_end;
ad1e68e6 404 size_t i;
5eb4e3d0
JM
405
406 pri_freq = hostapd_hw_get_freq(iface->bss[0], iface->conf->channel);
407 if (iface->conf->secondary_channel > 0)
408 sec_freq = pri_freq + 20;
409 else
410 sec_freq = pri_freq - 20;
411 affected_start = (pri_freq + sec_freq) / 2 - 25;
412 affected_end = (pri_freq + sec_freq) / 2 + 25;
413 wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
414 affected_start, affected_end);
ad1e68e6
JM
415 for (i = 0; i < scan_res->num; i++) {
416 struct wpa_scan_res *bss = scan_res->res[i];
417 int pri = bss->freq;
5eb4e3d0 418 int sec = pri;
ad1e68e6
JM
419 int sec_chan, pri_chan;
420
421 ieee80211n_get_pri_sec_chan(bss, &pri_chan, &sec_chan);
422
423 if (sec_chan) {
424 if (sec_chan < pri_chan)
5eb4e3d0
JM
425 sec = pri - 20;
426 else
427 sec = pri + 20;
428 }
429
430 if ((pri < affected_start || pri > affected_end) &&
431 (sec < affected_start || sec > affected_end))
432 continue; /* not within affected channel range */
433
ad1e68e6
JM
434 wpa_printf(MSG_DEBUG, "Neighboring BSS: " MACSTR
435 " freq=%d pri=%d sec=%d",
436 MAC2STR(bss->bssid), bss->freq, pri_chan, sec_chan);
437
438 if (sec_chan) {
5eb4e3d0
JM
439 if (pri_freq != pri || sec_freq != sec) {
440 wpa_printf(MSG_DEBUG, "40 MHz pri/sec "
441 "mismatch with BSS " MACSTR
442 " <%d,%d> (chan=%d%c) vs. <%d,%d>",
ad1e68e6
JM
443 MAC2STR(bss->bssid),
444 pri, sec, pri_chan,
5eb4e3d0
JM
445 sec > pri ? '+' : '-',
446 pri_freq, sec_freq);
447 return 0;
448 }
449 }
450
451 /* TODO: 40 MHz intolerant */
452 }
453
454 return 1;
455}
456
457
ad1e68e6 458static void ieee80211n_check_scan(struct hostapd_iface *iface)
5eb4e3d0 459{
ad1e68e6 460 struct wpa_scan_results *scan_res;
5eb4e3d0 461 int oper40;
7615078c 462 int res;
5eb4e3d0 463
5eb4e3d0 464 /* Check list of neighboring BSSes (from scan) to see whether 40 MHz is
7fa56233 465 * allowed per IEEE Std 802.11-2012, 10.15.3.2 */
5eb4e3d0 466
ad1e68e6
JM
467 iface->scan_cb = NULL;
468
469 scan_res = hostapd_driver_get_scan_results(iface->bss[0]);
470 if (scan_res == NULL) {
471 hostapd_setup_interface_complete(iface, 1);
472 return;
473 }
474
5eb4e3d0 475 if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A)
ad1e68e6 476 oper40 = ieee80211n_check_40mhz_5g(iface, scan_res);
5eb4e3d0 477 else
ad1e68e6
JM
478 oper40 = ieee80211n_check_40mhz_2g4(iface, scan_res);
479 wpa_scan_results_free(scan_res);
5eb4e3d0
JM
480
481 if (!oper40) {
482 wpa_printf(MSG_INFO, "20/40 MHz operation not permitted on "
483 "channel pri=%d sec=%d based on overlapping BSSes",
484 iface->conf->channel,
485 iface->conf->channel +
486 iface->conf->secondary_channel * 4);
487 iface->conf->secondary_channel = 0;
5eb4e3d0 488 }
ad1e68e6 489
7615078c
JM
490 res = ieee80211n_allowed_ht40_channel_pair(iface);
491 hostapd_setup_interface_complete(iface, !res);
ad1e68e6
JM
492}
493
494
7fa56233
JM
495static void ieee80211n_scan_channels_2g4(struct hostapd_iface *iface,
496 struct wpa_driver_scan_params *params)
497{
498 /* Scan only the affected frequency range */
499 int pri_freq, sec_freq;
500 int affected_start, affected_end;
501 int i, pos;
502 struct hostapd_hw_modes *mode;
503
504 if (iface->current_mode == NULL)
505 return;
506
507 pri_freq = hostapd_hw_get_freq(iface->bss[0], iface->conf->channel);
508 if (iface->conf->secondary_channel > 0)
509 sec_freq = pri_freq + 20;
510 else
511 sec_freq = pri_freq - 20;
512 affected_start = (pri_freq + sec_freq) / 2 - 25;
513 affected_end = (pri_freq + sec_freq) / 2 + 25;
514 wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
515 affected_start, affected_end);
516
517 mode = iface->current_mode;
f9884c09 518 params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
7fa56233
JM
519 if (params->freqs == NULL)
520 return;
521 pos = 0;
522
523 for (i = 0; i < mode->num_channels; i++) {
524 struct hostapd_channel_data *chan = &mode->channels[i];
525 if (chan->flag & HOSTAPD_CHAN_DISABLED)
526 continue;
527 if (chan->freq < affected_start ||
528 chan->freq > affected_end)
529 continue;
530 params->freqs[pos++] = chan->freq;
531 }
532}
533
534
ad1e68e6
JM
535static int ieee80211n_check_40mhz(struct hostapd_iface *iface)
536{
537 struct wpa_driver_scan_params params;
538
539 if (!iface->conf->secondary_channel)
540 return 0; /* HT40 not used */
541
e1c5faf0 542 hostapd_set_state(iface, HAPD_IFACE_HT_SCAN);
ad1e68e6
JM
543 wpa_printf(MSG_DEBUG, "Scan for neighboring BSSes prior to enabling "
544 "40 MHz channel");
545 os_memset(&params, 0, sizeof(params));
7fa56233
JM
546 if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
547 ieee80211n_scan_channels_2g4(iface, &params);
ad1e68e6
JM
548 if (hostapd_driver_scan(iface->bss[0], &params) < 0) {
549 wpa_printf(MSG_ERROR, "Failed to request a scan of "
550 "neighboring BSSes");
7fa56233 551 os_free(params.freqs);
ad1e68e6
JM
552 return -1;
553 }
7fa56233 554 os_free(params.freqs);
ad1e68e6
JM
555
556 iface->scan_cb = ieee80211n_check_scan;
557 return 1;
5eb4e3d0
JM
558}
559
560
30fd5f14
JM
561static int ieee80211n_supported_ht_capab(struct hostapd_iface *iface)
562{
563 u16 hw = iface->current_mode->ht_capab;
564 u16 conf = iface->conf->ht_capab;
565
30fd5f14
JM
566 if ((conf & HT_CAP_INFO_LDPC_CODING_CAP) &&
567 !(hw & HT_CAP_INFO_LDPC_CODING_CAP)) {
568 wpa_printf(MSG_ERROR, "Driver does not support configured "
569 "HT capability [LDPC]");
570 return 0;
571 }
572
573 if ((conf & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) &&
574 !(hw & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
575 wpa_printf(MSG_ERROR, "Driver does not support configured "
576 "HT capability [HT40*]");
577 return 0;
578 }
579
580 if ((conf & HT_CAP_INFO_SMPS_MASK) != (hw & HT_CAP_INFO_SMPS_MASK) &&
581 (conf & HT_CAP_INFO_SMPS_MASK) != HT_CAP_INFO_SMPS_DISABLED) {
582 wpa_printf(MSG_ERROR, "Driver does not support configured "
583 "HT capability [SMPS-*]");
584 return 0;
585 }
586
587 if ((conf & HT_CAP_INFO_GREEN_FIELD) &&
588 !(hw & HT_CAP_INFO_GREEN_FIELD)) {
589 wpa_printf(MSG_ERROR, "Driver does not support configured "
590 "HT capability [GF]");
591 return 0;
592 }
593
594 if ((conf & HT_CAP_INFO_SHORT_GI20MHZ) &&
595 !(hw & HT_CAP_INFO_SHORT_GI20MHZ)) {
596 wpa_printf(MSG_ERROR, "Driver does not support configured "
597 "HT capability [SHORT-GI-20]");
598 return 0;
599 }
600
601 if ((conf & HT_CAP_INFO_SHORT_GI40MHZ) &&
602 !(hw & HT_CAP_INFO_SHORT_GI40MHZ)) {
603 wpa_printf(MSG_ERROR, "Driver does not support configured "
604 "HT capability [SHORT-GI-40]");
605 return 0;
606 }
607
608 if ((conf & HT_CAP_INFO_TX_STBC) && !(hw & HT_CAP_INFO_TX_STBC)) {
609 wpa_printf(MSG_ERROR, "Driver does not support configured "
610 "HT capability [TX-STBC]");
611 return 0;
612 }
613
614 if ((conf & HT_CAP_INFO_RX_STBC_MASK) >
615 (hw & HT_CAP_INFO_RX_STBC_MASK)) {
616 wpa_printf(MSG_ERROR, "Driver does not support configured "
617 "HT capability [RX-STBC*]");
618 return 0;
619 }
620
621 if ((conf & HT_CAP_INFO_DELAYED_BA) &&
622 !(hw & HT_CAP_INFO_DELAYED_BA)) {
623 wpa_printf(MSG_ERROR, "Driver does not support configured "
624 "HT capability [DELAYED-BA]");
625 return 0;
626 }
627
628 if ((conf & HT_CAP_INFO_MAX_AMSDU_SIZE) &&
629 !(hw & HT_CAP_INFO_MAX_AMSDU_SIZE)) {
630 wpa_printf(MSG_ERROR, "Driver does not support configured "
631 "HT capability [MAX-AMSDU-7935]");
632 return 0;
633 }
634
635 if ((conf & HT_CAP_INFO_DSSS_CCK40MHZ) &&
636 !(hw & HT_CAP_INFO_DSSS_CCK40MHZ)) {
637 wpa_printf(MSG_ERROR, "Driver does not support configured "
638 "HT capability [DSSS_CCK-40]");
639 return 0;
640 }
641
642 if ((conf & HT_CAP_INFO_PSMP_SUPP) && !(hw & HT_CAP_INFO_PSMP_SUPP)) {
643 wpa_printf(MSG_ERROR, "Driver does not support configured "
644 "HT capability [PSMP]");
645 return 0;
646 }
647
648 if ((conf & HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT) &&
649 !(hw & HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT)) {
650 wpa_printf(MSG_ERROR, "Driver does not support configured "
651 "HT capability [LSIG-TXOP-PROT]");
652 return 0;
653 }
654
655 return 1;
656}
ad1e68e6 657
c781eb84
EP
658
659#ifdef CONFIG_IEEE80211AC
660
661static int ieee80211ac_cap_check(u32 hw, u32 conf, u32 cap, const char *name)
662{
663 u32 req_cap = conf & cap;
664
665 /*
666 * Make sure we support all requested capabilities.
667 * NOTE: We assume that 'cap' represents a capability mask,
668 * not a discrete value.
669 */
670 if ((hw & req_cap) != req_cap) {
671 wpa_printf(MSG_ERROR, "Driver does not support configured VHT capability [%s]",
672 name);
673 return 0;
674 }
675 return 1;
676}
677
678
679static int ieee80211ac_cap_check_max(u32 hw, u32 conf, u32 cap,
680 const char *name)
681{
682 u32 hw_max = hw & cap;
683 u32 conf_val = conf & cap;
684
685 if (conf_val > hw_max) {
686 int offset = find_first_bit(cap);
687 wpa_printf(MSG_ERROR, "Configured VHT capability [%s] exceeds max value supported by the driver (%d > %d)",
688 name, conf_val >> offset, hw_max >> offset);
689 return 0;
690 }
691 return 1;
692}
693
694
695static int ieee80211ac_supported_vht_capab(struct hostapd_iface *iface)
696{
697 u32 hw = iface->current_mode->vht_capab;
698 u32 conf = iface->conf->vht_capab;
699
700 wpa_printf(MSG_DEBUG, "hw vht capab: 0x%x, conf vht capab: 0x%x",
701 hw, conf);
702
703#define VHT_CAP_CHECK(cap) \
704 do { \
705 if (!ieee80211ac_cap_check(hw, conf, cap, #cap)) \
706 return 0; \
707 } while (0)
708
709#define VHT_CAP_CHECK_MAX(cap) \
710 do { \
711 if (!ieee80211ac_cap_check_max(hw, conf, cap, #cap)) \
712 return 0; \
713 } while (0)
714
715 VHT_CAP_CHECK_MAX(VHT_CAP_MAX_MPDU_LENGTH_MASK);
716 VHT_CAP_CHECK(VHT_CAP_SUPP_CHAN_WIDTH_160MHZ);
717 VHT_CAP_CHECK(VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ);
718 VHT_CAP_CHECK(VHT_CAP_RXLDPC);
719 VHT_CAP_CHECK(VHT_CAP_SHORT_GI_80);
720 VHT_CAP_CHECK(VHT_CAP_SHORT_GI_160);
721 VHT_CAP_CHECK(VHT_CAP_TXSTBC);
722 VHT_CAP_CHECK_MAX(VHT_CAP_RXSTBC_MASK);
723 VHT_CAP_CHECK(VHT_CAP_SU_BEAMFORMER_CAPABLE);
724 VHT_CAP_CHECK(VHT_CAP_SU_BEAMFORMEE_CAPABLE);
725 VHT_CAP_CHECK_MAX(VHT_CAP_BEAMFORMEE_STS_MAX);
726 VHT_CAP_CHECK_MAX(VHT_CAP_SOUNDING_DIMENSION_MAX);
727 VHT_CAP_CHECK(VHT_CAP_MU_BEAMFORMER_CAPABLE);
728 VHT_CAP_CHECK(VHT_CAP_MU_BEAMFORMEE_CAPABLE);
729 VHT_CAP_CHECK(VHT_CAP_VHT_TXOP_PS);
730 VHT_CAP_CHECK(VHT_CAP_HTC_VHT);
731 VHT_CAP_CHECK_MAX(VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT);
732 VHT_CAP_CHECK(VHT_CAP_VHT_LINK_ADAPTATION_VHT_UNSOL_MFB);
733 VHT_CAP_CHECK(VHT_CAP_VHT_LINK_ADAPTATION_VHT_MRQ_MFB);
734 VHT_CAP_CHECK(VHT_CAP_RX_ANTENNA_PATTERN);
735 VHT_CAP_CHECK(VHT_CAP_TX_ANTENNA_PATTERN);
736
737#undef VHT_CAP_CHECK
738#undef VHT_CAP_CHECK_MAX
739
740 return 1;
741}
742#endif /* CONFIG_IEEE80211AC */
743
ad1e68e6
JM
744#endif /* CONFIG_IEEE80211N */
745
746
747int hostapd_check_ht_capab(struct hostapd_iface *iface)
748{
749#ifdef CONFIG_IEEE80211N
750 int ret;
ec2b8909
SM
751 if (!iface->conf->ieee80211n)
752 return 0;
1b4d3793
NS
753 if (!ieee80211n_supported_ht_capab(iface))
754 return -1;
c781eb84
EP
755#ifdef CONFIG_IEEE80211AC
756 if (!ieee80211ac_supported_vht_capab(iface))
757 return -1;
758#endif /* CONFIG_IEEE80211AC */
ad1e68e6
JM
759 ret = ieee80211n_check_40mhz(iface);
760 if (ret)
761 return ret;
762 if (!ieee80211n_allowed_ht40_channel_pair(iface))
763 return -1;
3e0cb2c5
JM
764#endif /* CONFIG_IEEE80211N */
765
ad1e68e6
JM
766 return 0;
767}
768
3e0cb2c5 769
245e026e
MK
770static int hostapd_is_usable_chan(struct hostapd_iface *iface,
771 int channel, int primary)
772{
773 int i;
774 struct hostapd_channel_data *chan;
775
776 for (i = 0; i < iface->current_mode->num_channels; i++) {
777 chan = &iface->current_mode->channels[i];
778 if (chan->chan != channel)
779 continue;
780
781 if (!(chan->flag & HOSTAPD_CHAN_DISABLED))
782 return 1;
783
784 wpa_printf(MSG_DEBUG,
785 "%schannel [%i] (%i) is disabled for use in AP mode, flags: 0x%x%s%s%s",
786 primary ? "" : "Configured HT40 secondary ",
787 i, chan->chan, chan->flag,
788 chan->flag & HOSTAPD_CHAN_NO_IBSS ? " NO-IBSS" : "",
789 chan->flag & HOSTAPD_CHAN_PASSIVE_SCAN ?
790 " PASSIVE-SCAN" : "",
791 chan->flag & HOSTAPD_CHAN_RADAR ? " RADAR" : "");
792 }
793
794 return 0;
795}
796
797
798static int hostapd_is_usable_chans(struct hostapd_iface *iface)
799{
800 if (!hostapd_is_usable_chan(iface, iface->conf->channel, 1))
801 return 0;
802
803 if (!iface->conf->secondary_channel)
804 return 1;
805
806 return hostapd_is_usable_chan(iface, iface->conf->channel +
807 iface->conf->secondary_channel * 4, 0);
808}
809
810
811static enum hostapd_chan_status
812hostapd_check_chans(struct hostapd_iface *iface)
813{
814 if (iface->conf->channel) {
815 if (hostapd_is_usable_chans(iface))
816 return HOSTAPD_CHAN_VALID;
817 else
818 return HOSTAPD_CHAN_INVALID;
819 }
820
821 /*
50f4f2a0
MK
822 * The user set channel=0 or channel=acs_survey
823 * which is used to trigger ACS.
245e026e 824 */
50f4f2a0
MK
825
826 switch (acs_init(iface)) {
827 case HOSTAPD_CHAN_ACS:
828 return HOSTAPD_CHAN_ACS;
829 case HOSTAPD_CHAN_VALID:
830 case HOSTAPD_CHAN_INVALID:
831 default:
832 return HOSTAPD_CHAN_INVALID;
833 }
245e026e
MK
834}
835
836
837static void hostapd_notify_bad_chans(struct hostapd_iface *iface)
838{
839 hostapd_logger(iface->bss[0], NULL,
840 HOSTAPD_MODULE_IEEE80211,
841 HOSTAPD_LEVEL_WARNING,
842 "Configured channel (%d) not found from the "
843 "channel list of current mode (%d) %s",
844 iface->conf->channel,
845 iface->current_mode->mode,
846 hostapd_hw_mode_txt(iface->current_mode->mode));
847 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
848 HOSTAPD_LEVEL_WARNING,
849 "Hardware does not support configured channel");
850}
851
852
3645fd5a 853int hostapd_acs_completed(struct hostapd_iface *iface, int err)
50f4f2a0 854{
0e1d0b37 855 int ret = -1;
50f4f2a0 856
3645fd5a
HS
857 if (err)
858 goto out;
859
50f4f2a0
MK
860 switch (hostapd_check_chans(iface)) {
861 case HOSTAPD_CHAN_VALID:
ae134e1d
JM
862 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO,
863 ACS_EVENT_COMPLETED "freq=%d channel=%d",
864 hostapd_hw_get_freq(iface->bss[0],
865 iface->conf->channel),
866 iface->conf->channel);
50f4f2a0
MK
867 break;
868 case HOSTAPD_CHAN_ACS:
869 wpa_printf(MSG_ERROR, "ACS error - reported complete, but no result available");
ae134e1d 870 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, ACS_EVENT_FAILED);
50f4f2a0 871 hostapd_notify_bad_chans(iface);
0e1d0b37 872 goto out;
50f4f2a0
MK
873 case HOSTAPD_CHAN_INVALID:
874 default:
875 wpa_printf(MSG_ERROR, "ACS picked unusable channels");
ae134e1d 876 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, ACS_EVENT_FAILED);
50f4f2a0 877 hostapd_notify_bad_chans(iface);
0e1d0b37 878 goto out;
50f4f2a0
MK
879 }
880
881 ret = hostapd_check_ht_capab(iface);
882 if (ret < 0)
0e1d0b37 883 goto out;
50f4f2a0
MK
884 if (ret == 1) {
885 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback");
886 return 0;
887 }
888
0e1d0b37
HS
889 ret = 0;
890out:
891 return hostapd_setup_interface_complete(iface, ret);
50f4f2a0
MK
892}
893
894
6fc6879b 895/**
ddaa83eb 896 * hostapd_select_hw_mode - Select the hardware mode
6fc6879b 897 * @iface: Pointer to interface data.
60eda5e4 898 * Returns: 0 on success, < 0 on failure
6fc6879b 899 *
ddaa83eb
JM
900 * Sets up the hardware mode, channel, rates, and passive scanning
901 * based on the configuration.
6fc6879b 902 */
ddaa83eb 903int hostapd_select_hw_mode(struct hostapd_iface *iface)
6fc6879b 904{
245e026e 905 int i;
6fc6879b
JM
906
907 if (iface->num_hw_features < 1)
908 return -1;
909
910 iface->current_mode = NULL;
911 for (i = 0; i < iface->num_hw_features; i++) {
912 struct hostapd_hw_modes *mode = &iface->hw_features[i];
6caf9ca6 913 if (mode->mode == iface->conf->hw_mode) {
6fc6879b
JM
914 iface->current_mode = mode;
915 break;
916 }
917 }
918
919 if (iface->current_mode == NULL) {
920 wpa_printf(MSG_ERROR, "Hardware does not support configured "
921 "mode");
922 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
923 HOSTAPD_LEVEL_WARNING,
924 "Hardware does not support configured mode "
93ac2404
JM
925 "(%d) (hw_mode in hostapd.conf)",
926 (int) iface->conf->hw_mode);
60eda5e4 927 return -2;
6fc6879b
JM
928 }
929
245e026e
MK
930 switch (hostapd_check_chans(iface)) {
931 case HOSTAPD_CHAN_VALID:
932 return 0;
50f4f2a0
MK
933 case HOSTAPD_CHAN_ACS: /* ACS will run and later complete */
934 return 1;
245e026e
MK
935 case HOSTAPD_CHAN_INVALID:
936 default:
937 hostapd_notify_bad_chans(iface);
60eda5e4 938 return -3;
24c9fceb 939 }
6fc6879b 940
61693eaa 941 return 0;
6fc6879b
JM
942}
943
944
945const char * hostapd_hw_mode_txt(int mode)
946{
947 switch (mode) {
948 case HOSTAPD_MODE_IEEE80211A:
949 return "IEEE 802.11a";
950 case HOSTAPD_MODE_IEEE80211B:
951 return "IEEE 802.11b";
952 case HOSTAPD_MODE_IEEE80211G:
953 return "IEEE 802.11g";
7829894c
VK
954 case HOSTAPD_MODE_IEEE80211AD:
955 return "IEEE 802.11ad";
6fc6879b
JM
956 default:
957 return "UNKNOWN";
958 }
959}
960
961
962int hostapd_hw_get_freq(struct hostapd_data *hapd, int chan)
963{
964 int i;
965
966 if (!hapd->iface->current_mode)
967 return 0;
968
969 for (i = 0; i < hapd->iface->current_mode->num_channels; i++) {
970 struct hostapd_channel_data *ch =
971 &hapd->iface->current_mode->channels[i];
972 if (ch->chan == chan)
973 return ch->freq;
974 }
975
976 return 0;
977}
978
979
980int hostapd_hw_get_channel(struct hostapd_data *hapd, int freq)
981{
982 int i;
983
984 if (!hapd->iface->current_mode)
985 return 0;
986
987 for (i = 0; i < hapd->iface->current_mode->num_channels; i++) {
988 struct hostapd_channel_data *ch =
989 &hapd->iface->current_mode->channels[i];
990 if (ch->freq == freq)
991 return ch->chan;
992 }
993
994 return 0;
995}