]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/ap/hw_features.c
Rename some src/ap files to avoid duplicate file names
[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.
1057d78e 5 * Copyright (c) 2008-2009, 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"
6226e38d
JM
23#include "hostapd.h"
24#include "ap_config.h"
bfddd95c 25#include "driver_i.h"
6226e38d 26#include "hw_features.h"
6fc6879b
JM
27
28
29void hostapd_free_hw_features(struct hostapd_hw_modes *hw_features,
30 size_t num_hw_features)
31{
32 size_t i;
33
34 if (hw_features == NULL)
35 return;
36
37 for (i = 0; i < num_hw_features; i++) {
38 os_free(hw_features[i].channels);
39 os_free(hw_features[i].rates);
40 }
41
42 os_free(hw_features);
43}
44
45
46int hostapd_get_hw_features(struct hostapd_iface *iface)
47{
48 struct hostapd_data *hapd = iface->bss[0];
49 int ret = 0, i, j;
50 u16 num_modes, flags;
51 struct hostapd_hw_modes *modes;
52
85141289
JM
53 if (hostapd_drv_none(hapd))
54 return -1;
6fc6879b
JM
55 modes = hostapd_get_hw_feature_data(hapd, &num_modes, &flags);
56 if (modes == NULL) {
57 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
58 HOSTAPD_LEVEL_DEBUG,
59 "Fetching hardware channel/rate support not "
60 "supported.");
61 return -1;
62 }
63
64 iface->hw_flags = flags;
65
66 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
67 iface->hw_features = modes;
68 iface->num_hw_features = num_modes;
69
70 for (i = 0; i < num_modes; i++) {
71 struct hostapd_hw_modes *feature = &modes[i];
72 /* set flag for channels we can use in current regulatory
73 * domain */
74 for (j = 0; j < feature->num_channels; j++) {
10b83bd7
JM
75 /*
76 * Disable all channels that are marked not to allow
77 * IBSS operation or active scanning. In addition,
78 * disable all channels that require radar detection,
79 * since that (in addition to full DFS) is not yet
80 * supported.
81 */
82 if (feature->channels[j].flag &
83 (HOSTAPD_CHAN_NO_IBSS |
84 HOSTAPD_CHAN_PASSIVE_SCAN |
85 HOSTAPD_CHAN_RADAR))
6fc6879b 86 feature->channels[j].flag |=
10b83bd7
JM
87 HOSTAPD_CHAN_DISABLED;
88 if (feature->channels[j].flag & HOSTAPD_CHAN_DISABLED)
89 continue;
90 wpa_printf(MSG_MSGDUMP, "Allowed channel: mode=%d "
bf01d8bc 91 "chan=%d freq=%d MHz max_tx_power=%d dBm",
10b83bd7
JM
92 feature->mode,
93 feature->channels[j].chan,
bf01d8bc
JM
94 feature->channels[j].freq,
95 feature->channels[j].max_tx_power);
6fc6879b
JM
96 }
97 }
98
99 return ret;
100}
101
102
103static int hostapd_prepare_rates(struct hostapd_data *hapd,
104 struct hostapd_hw_modes *mode)
105{
106 int i, num_basic_rates = 0;
107 int basic_rates_a[] = { 60, 120, 240, -1 };
108 int basic_rates_b[] = { 10, 20, -1 };
109 int basic_rates_g[] = { 10, 20, 55, 110, -1 };
110 int *basic_rates;
111
112 if (hapd->iconf->basic_rates)
113 basic_rates = hapd->iconf->basic_rates;
114 else switch (mode->mode) {
115 case HOSTAPD_MODE_IEEE80211A:
116 basic_rates = basic_rates_a;
117 break;
118 case HOSTAPD_MODE_IEEE80211B:
119 basic_rates = basic_rates_b;
120 break;
121 case HOSTAPD_MODE_IEEE80211G:
122 basic_rates = basic_rates_g;
123 break;
124 default:
125 return -1;
126 }
127
128 if (hostapd_set_rate_sets(hapd, hapd->iconf->supported_rates,
129 basic_rates, mode->mode)) {
130 wpa_printf(MSG_ERROR, "Failed to update rate sets in kernel "
131 "module");
132 }
133
134 os_free(hapd->iface->current_rates);
135 hapd->iface->num_rates = 0;
136
137 hapd->iface->current_rates =
fb7842aa 138 os_zalloc(mode->num_rates * sizeof(struct hostapd_rate_data));
6fc6879b
JM
139 if (!hapd->iface->current_rates) {
140 wpa_printf(MSG_ERROR, "Failed to allocate memory for rate "
141 "table.");
142 return -1;
143 }
144
145 for (i = 0; i < mode->num_rates; i++) {
146 struct hostapd_rate_data *rate;
147
148 if (hapd->iconf->supported_rates &&
149 !hostapd_rate_found(hapd->iconf->supported_rates,
fb7842aa 150 mode->rates[i]))
6fc6879b
JM
151 continue;
152
153 rate = &hapd->iface->current_rates[hapd->iface->num_rates];
fb7842aa 154 rate->rate = mode->rates[i];
6fc6879b
JM
155 if (hostapd_rate_found(basic_rates, rate->rate)) {
156 rate->flags |= HOSTAPD_RATE_BASIC;
157 num_basic_rates++;
fb7842aa 158 }
6fc6879b
JM
159 wpa_printf(MSG_DEBUG, "RATE[%d] rate=%d flags=0x%x",
160 hapd->iface->num_rates, rate->rate, rate->flags);
161 hapd->iface->num_rates++;
162 }
163
164 if (hapd->iface->num_rates == 0 || num_basic_rates == 0) {
165 wpa_printf(MSG_ERROR, "No rates remaining in supported/basic "
166 "rate sets (%d,%d).",
167 hapd->iface->num_rates, num_basic_rates);
168 return -1;
169 }
170
171 return 0;
172}
173
174
3e0cb2c5
JM
175#ifdef CONFIG_IEEE80211N
176static int ieee80211n_allowed_ht40_channel_pair(struct hostapd_iface *iface)
177{
178 int sec_chan, ok, j, first;
179 int allowed[] = { 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157,
180 184, 192 };
181 size_t k;
182
183 if (!iface->conf->secondary_channel)
184 return 1; /* HT40 not used */
185
186 sec_chan = iface->conf->channel + iface->conf->secondary_channel * 4;
187 wpa_printf(MSG_DEBUG, "HT40: control channel: %d "
188 "secondary channel: %d",
189 iface->conf->channel, sec_chan);
190
191 /* Verify that HT40 secondary channel is an allowed 20 MHz
192 * channel */
193 ok = 0;
194 for (j = 0; j < iface->current_mode->num_channels; j++) {
195 struct hostapd_channel_data *chan =
196 &iface->current_mode->channels[j];
197 if (!(chan->flag & HOSTAPD_CHAN_DISABLED) &&
198 chan->chan == sec_chan) {
199 ok = 1;
200 break;
201 }
202 }
203 if (!ok) {
204 wpa_printf(MSG_ERROR, "HT40 secondary channel %d not allowed",
205 sec_chan);
206 return 0;
207 }
208
209 /*
210 * Verify that HT40 primary,secondary channel pair is allowed per
211 * IEEE 802.11n Annex J. This is only needed for 5 GHz band since
212 * 2.4 GHz rules allow all cases where the secondary channel fits into
213 * the list of allowed channels (already checked above).
214 */
215 if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A)
216 return 1;
217
218 if (iface->conf->secondary_channel > 0)
219 first = iface->conf->channel;
220 else
221 first = sec_chan;
222
223 ok = 0;
224 for (k = 0; k < sizeof(allowed) / sizeof(allowed[0]); k++) {
225 if (first == allowed[k]) {
226 ok = 1;
227 break;
228 }
229 }
230 if (!ok) {
231 wpa_printf(MSG_ERROR, "HT40 channel pair (%d, %d) not allowed",
232 iface->conf->channel,
233 iface->conf->secondary_channel);
234 return 0;
235 }
236
237 return 1;
238}
30fd5f14
JM
239
240
5eb4e3d0
JM
241static void ieee80211n_switch_pri_sec(struct hostapd_iface *iface)
242{
243 if (iface->conf->secondary_channel > 0) {
244 iface->conf->channel += 4;
245 iface->conf->secondary_channel = -1;
246 } else {
247 iface->conf->channel -= 4;
248 iface->conf->secondary_channel = 1;
249 }
250}
251
252
ad1e68e6
JM
253static void ieee80211n_get_pri_sec_chan(struct wpa_scan_res *bss,
254 int *pri_chan, int *sec_chan)
255{
256 struct ieee80211_ht_operation *oper;
257 struct ieee802_11_elems elems;
258
259 *pri_chan = *sec_chan = 0;
260
261 ieee802_11_parse_elems((u8 *) (bss + 1), bss->ie_len, &elems, 0);
262 if (elems.ht_operation &&
263 elems.ht_operation_len >= sizeof(*oper)) {
264 oper = (struct ieee80211_ht_operation *) elems.ht_operation;
265 *pri_chan = oper->control_chan;
266 if (oper->ht_param & HT_INFO_HT_PARAM_REC_TRANS_CHNL_WIDTH) {
267 if (oper->ht_param &
268 HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
269 *sec_chan = *pri_chan + 4;
270 else if (oper->ht_param &
271 HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
272 *sec_chan = *pri_chan - 4;
273 }
274 }
275}
276
277
278static int ieee80211n_check_40mhz_5g(struct hostapd_iface *iface,
279 struct wpa_scan_results *scan_res)
5eb4e3d0
JM
280{
281 int pri_chan, sec_chan, pri_freq, sec_freq, pri_bss, sec_bss;
ad1e68e6
JM
282 int bss_pri_chan, bss_sec_chan;
283 size_t i;
5eb4e3d0
JM
284 int match;
285
286 pri_chan = iface->conf->channel;
287 sec_chan = iface->conf->secondary_channel * 4;
288 pri_freq = hostapd_hw_get_freq(iface->bss[0], pri_chan);
289 if (iface->conf->secondary_channel > 0)
290 sec_freq = pri_freq + 20;
291 else
292 sec_freq = pri_freq - 20;
293
5eb4e3d0
JM
294 /*
295 * Switch PRI/SEC channels if Beacons were detected on selected SEC
296 * channel, but not on selected PRI channel.
297 */
298 pri_bss = sec_bss = 0;
ad1e68e6
JM
299 for (i = 0; i < scan_res->num; i++) {
300 struct wpa_scan_res *bss = scan_res->res[i];
301 if (bss->freq == pri_freq)
5eb4e3d0 302 pri_bss++;
ad1e68e6 303 else if (bss->freq == sec_freq)
5eb4e3d0
JM
304 sec_bss++;
305 }
306 if (sec_bss && !pri_bss) {
307 wpa_printf(MSG_INFO, "Switch own primary and secondary "
308 "channel to get secondary channel with no Beacons "
309 "from other BSSes");
310 ieee80211n_switch_pri_sec(iface);
311 }
312
313 /*
314 * Match PRI/SEC channel with any existing HT40 BSS on the same
315 * channels that we are about to use (if already mixed order in
316 * existing BSSes, use own preference).
317 */
318 match = 0;
ad1e68e6
JM
319 for (i = 0; i < scan_res->num; i++) {
320 struct wpa_scan_res *bss = scan_res->res[i];
321 ieee80211n_get_pri_sec_chan(bss, &bss_pri_chan, &bss_sec_chan);
322 if (pri_chan == bss_pri_chan &&
323 sec_chan == bss_sec_chan) {
5eb4e3d0
JM
324 match = 1;
325 break;
326 }
327 }
328 if (!match) {
ad1e68e6
JM
329 for (i = 0; i < scan_res->num; i++) {
330 struct wpa_scan_res *bss = scan_res->res[i];
c6313c75
FF
331 ieee80211n_get_pri_sec_chan(bss, &bss_pri_chan,
332 &bss_sec_chan);
ad1e68e6
JM
333 if (pri_chan == bss_sec_chan &&
334 sec_chan == bss_pri_chan) {
5eb4e3d0
JM
335 wpa_printf(MSG_INFO, "Switch own primary and "
336 "secondary channel due to BSS "
337 "overlap with " MACSTR,
ad1e68e6 338 MAC2STR(bss->bssid));
5eb4e3d0
JM
339 ieee80211n_switch_pri_sec(iface);
340 break;
341 }
342 }
343 }
344
345 return 1;
346}
347
348
ad1e68e6
JM
349static int ieee80211n_check_40mhz_2g4(struct hostapd_iface *iface,
350 struct wpa_scan_results *scan_res)
5eb4e3d0
JM
351{
352 int pri_freq, sec_freq;
353 int affected_start, affected_end;
ad1e68e6 354 size_t i;
5eb4e3d0
JM
355
356 pri_freq = hostapd_hw_get_freq(iface->bss[0], iface->conf->channel);
357 if (iface->conf->secondary_channel > 0)
358 sec_freq = pri_freq + 20;
359 else
360 sec_freq = pri_freq - 20;
361 affected_start = (pri_freq + sec_freq) / 2 - 25;
362 affected_end = (pri_freq + sec_freq) / 2 + 25;
363 wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
364 affected_start, affected_end);
ad1e68e6
JM
365 for (i = 0; i < scan_res->num; i++) {
366 struct wpa_scan_res *bss = scan_res->res[i];
367 int pri = bss->freq;
5eb4e3d0 368 int sec = pri;
ad1e68e6
JM
369 int sec_chan, pri_chan;
370
371 ieee80211n_get_pri_sec_chan(bss, &pri_chan, &sec_chan);
372
373 if (sec_chan) {
374 if (sec_chan < pri_chan)
5eb4e3d0
JM
375 sec = pri - 20;
376 else
377 sec = pri + 20;
378 }
379
380 if ((pri < affected_start || pri > affected_end) &&
381 (sec < affected_start || sec > affected_end))
382 continue; /* not within affected channel range */
383
ad1e68e6
JM
384 wpa_printf(MSG_DEBUG, "Neighboring BSS: " MACSTR
385 " freq=%d pri=%d sec=%d",
386 MAC2STR(bss->bssid), bss->freq, pri_chan, sec_chan);
387
388 if (sec_chan) {
5eb4e3d0
JM
389 if (pri_freq != pri || sec_freq != sec) {
390 wpa_printf(MSG_DEBUG, "40 MHz pri/sec "
391 "mismatch with BSS " MACSTR
392 " <%d,%d> (chan=%d%c) vs. <%d,%d>",
ad1e68e6
JM
393 MAC2STR(bss->bssid),
394 pri, sec, pri_chan,
5eb4e3d0
JM
395 sec > pri ? '+' : '-',
396 pri_freq, sec_freq);
397 return 0;
398 }
399 }
400
401 /* TODO: 40 MHz intolerant */
402 }
403
404 return 1;
405}
406
407
ad1e68e6 408static void ieee80211n_check_scan(struct hostapd_iface *iface)
5eb4e3d0 409{
ad1e68e6 410 struct wpa_scan_results *scan_res;
5eb4e3d0
JM
411 int oper40;
412
5eb4e3d0
JM
413 /* Check list of neighboring BSSes (from scan) to see whether 40 MHz is
414 * allowed per IEEE 802.11n/D7.0, 11.14.3.2 */
415
ad1e68e6
JM
416 iface->scan_cb = NULL;
417
418 scan_res = hostapd_driver_get_scan_results(iface->bss[0]);
419 if (scan_res == NULL) {
420 hostapd_setup_interface_complete(iface, 1);
421 return;
422 }
423
5eb4e3d0 424 if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A)
ad1e68e6 425 oper40 = ieee80211n_check_40mhz_5g(iface, scan_res);
5eb4e3d0 426 else
ad1e68e6
JM
427 oper40 = ieee80211n_check_40mhz_2g4(iface, scan_res);
428 wpa_scan_results_free(scan_res);
5eb4e3d0
JM
429
430 if (!oper40) {
431 wpa_printf(MSG_INFO, "20/40 MHz operation not permitted on "
432 "channel pri=%d sec=%d based on overlapping BSSes",
433 iface->conf->channel,
434 iface->conf->channel +
435 iface->conf->secondary_channel * 4);
436 iface->conf->secondary_channel = 0;
437 iface->conf->ht_capab &= ~HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
438 }
ad1e68e6
JM
439
440 hostapd_setup_interface_complete(iface, 0);
441}
442
443
444static int ieee80211n_check_40mhz(struct hostapd_iface *iface)
445{
446 struct wpa_driver_scan_params params;
447
448 if (!iface->conf->secondary_channel)
449 return 0; /* HT40 not used */
450
451 wpa_printf(MSG_DEBUG, "Scan for neighboring BSSes prior to enabling "
452 "40 MHz channel");
453 os_memset(&params, 0, sizeof(params));
454 /* TODO: scan only the needed frequency */
455 if (hostapd_driver_scan(iface->bss[0], &params) < 0) {
456 wpa_printf(MSG_ERROR, "Failed to request a scan of "
457 "neighboring BSSes");
458 return -1;
459 }
460
461 iface->scan_cb = ieee80211n_check_scan;
462 return 1;
5eb4e3d0
JM
463}
464
465
30fd5f14
JM
466static int ieee80211n_supported_ht_capab(struct hostapd_iface *iface)
467{
468 u16 hw = iface->current_mode->ht_capab;
469 u16 conf = iface->conf->ht_capab;
470
471 if (!iface->conf->ieee80211n)
472 return 1;
473
474 if ((conf & HT_CAP_INFO_LDPC_CODING_CAP) &&
475 !(hw & HT_CAP_INFO_LDPC_CODING_CAP)) {
476 wpa_printf(MSG_ERROR, "Driver does not support configured "
477 "HT capability [LDPC]");
478 return 0;
479 }
480
481 if ((conf & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) &&
482 !(hw & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
483 wpa_printf(MSG_ERROR, "Driver does not support configured "
484 "HT capability [HT40*]");
485 return 0;
486 }
487
488 if ((conf & HT_CAP_INFO_SMPS_MASK) != (hw & HT_CAP_INFO_SMPS_MASK) &&
489 (conf & HT_CAP_INFO_SMPS_MASK) != HT_CAP_INFO_SMPS_DISABLED) {
490 wpa_printf(MSG_ERROR, "Driver does not support configured "
491 "HT capability [SMPS-*]");
492 return 0;
493 }
494
495 if ((conf & HT_CAP_INFO_GREEN_FIELD) &&
496 !(hw & HT_CAP_INFO_GREEN_FIELD)) {
497 wpa_printf(MSG_ERROR, "Driver does not support configured "
498 "HT capability [GF]");
499 return 0;
500 }
501
502 if ((conf & HT_CAP_INFO_SHORT_GI20MHZ) &&
503 !(hw & HT_CAP_INFO_SHORT_GI20MHZ)) {
504 wpa_printf(MSG_ERROR, "Driver does not support configured "
505 "HT capability [SHORT-GI-20]");
506 return 0;
507 }
508
509 if ((conf & HT_CAP_INFO_SHORT_GI40MHZ) &&
510 !(hw & HT_CAP_INFO_SHORT_GI40MHZ)) {
511 wpa_printf(MSG_ERROR, "Driver does not support configured "
512 "HT capability [SHORT-GI-40]");
513 return 0;
514 }
515
516 if ((conf & HT_CAP_INFO_TX_STBC) && !(hw & HT_CAP_INFO_TX_STBC)) {
517 wpa_printf(MSG_ERROR, "Driver does not support configured "
518 "HT capability [TX-STBC]");
519 return 0;
520 }
521
522 if ((conf & HT_CAP_INFO_RX_STBC_MASK) >
523 (hw & HT_CAP_INFO_RX_STBC_MASK)) {
524 wpa_printf(MSG_ERROR, "Driver does not support configured "
525 "HT capability [RX-STBC*]");
526 return 0;
527 }
528
529 if ((conf & HT_CAP_INFO_DELAYED_BA) &&
530 !(hw & HT_CAP_INFO_DELAYED_BA)) {
531 wpa_printf(MSG_ERROR, "Driver does not support configured "
532 "HT capability [DELAYED-BA]");
533 return 0;
534 }
535
536 if ((conf & HT_CAP_INFO_MAX_AMSDU_SIZE) &&
537 !(hw & HT_CAP_INFO_MAX_AMSDU_SIZE)) {
538 wpa_printf(MSG_ERROR, "Driver does not support configured "
539 "HT capability [MAX-AMSDU-7935]");
540 return 0;
541 }
542
543 if ((conf & HT_CAP_INFO_DSSS_CCK40MHZ) &&
544 !(hw & HT_CAP_INFO_DSSS_CCK40MHZ)) {
545 wpa_printf(MSG_ERROR, "Driver does not support configured "
546 "HT capability [DSSS_CCK-40]");
547 return 0;
548 }
549
550 if ((conf & HT_CAP_INFO_PSMP_SUPP) && !(hw & HT_CAP_INFO_PSMP_SUPP)) {
551 wpa_printf(MSG_ERROR, "Driver does not support configured "
552 "HT capability [PSMP]");
553 return 0;
554 }
555
556 if ((conf & HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT) &&
557 !(hw & HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT)) {
558 wpa_printf(MSG_ERROR, "Driver does not support configured "
559 "HT capability [LSIG-TXOP-PROT]");
560 return 0;
561 }
562
563 return 1;
564}
ad1e68e6
JM
565
566#endif /* CONFIG_IEEE80211N */
567
568
569int hostapd_check_ht_capab(struct hostapd_iface *iface)
570{
571#ifdef CONFIG_IEEE80211N
572 int ret;
573 ret = ieee80211n_check_40mhz(iface);
574 if (ret)
575 return ret;
576 if (!ieee80211n_allowed_ht40_channel_pair(iface))
577 return -1;
578 if (!ieee80211n_supported_ht_capab(iface))
579 return -1;
3e0cb2c5
JM
580#endif /* CONFIG_IEEE80211N */
581
ad1e68e6
JM
582 return 0;
583}
584
3e0cb2c5 585
6fc6879b 586/**
ddaa83eb 587 * hostapd_select_hw_mode - Select the hardware mode
6fc6879b 588 * @iface: Pointer to interface data.
ddaa83eb 589 * Returns: 0 on success, -1 on failure
6fc6879b 590 *
ddaa83eb
JM
591 * Sets up the hardware mode, channel, rates, and passive scanning
592 * based on the configuration.
6fc6879b 593 */
ddaa83eb 594int hostapd_select_hw_mode(struct hostapd_iface *iface)
6fc6879b 595{
61693eaa 596 int i, j, ok;
6fc6879b
JM
597
598 if (iface->num_hw_features < 1)
599 return -1;
600
601 iface->current_mode = NULL;
602 for (i = 0; i < iface->num_hw_features; i++) {
603 struct hostapd_hw_modes *mode = &iface->hw_features[i];
6caf9ca6 604 if (mode->mode == iface->conf->hw_mode) {
6fc6879b
JM
605 iface->current_mode = mode;
606 break;
607 }
608 }
609
610 if (iface->current_mode == NULL) {
611 wpa_printf(MSG_ERROR, "Hardware does not support configured "
612 "mode");
613 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
614 HOSTAPD_LEVEL_WARNING,
615 "Hardware does not support configured mode "
616 "(%d)", (int) iface->conf->hw_mode);
617 return -1;
618 }
619
620 ok = 0;
621 for (j = 0; j < iface->current_mode->num_channels; j++) {
622 struct hostapd_channel_data *chan =
623 &iface->current_mode->channels[j];
10b83bd7 624 if (!(chan->flag & HOSTAPD_CHAN_DISABLED) &&
6fc6879b
JM
625 (chan->chan == iface->conf->channel)) {
626 ok = 1;
627 break;
628 }
629 }
24c9fceb
JM
630 if (iface->conf->channel == 0) {
631 /* TODO: could request a scan of neighboring BSSes and select
632 * the channel automatically */
633 wpa_printf(MSG_ERROR, "Channel not configured "
634 "(hw_mode/channel in hostapd.conf)");
635 return -1;
636 }
6fc6879b
JM
637 if (ok == 0 && iface->conf->channel != 0) {
638 hostapd_logger(iface->bss[0], NULL,
639 HOSTAPD_MODULE_IEEE80211,
640 HOSTAPD_LEVEL_WARNING,
641 "Configured channel (%d) not found from the "
642 "channel list of current mode (%d) %s",
643 iface->conf->channel,
644 iface->current_mode->mode,
645 hostapd_hw_mode_txt(iface->current_mode->mode));
646 iface->current_mode = NULL;
647 }
648
ddaa83eb
JM
649 if (iface->current_mode == NULL) {
650 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
651 HOSTAPD_LEVEL_WARNING,
652 "Hardware does not support configured channel");
6fc6879b
JM
653 return -1;
654 }
655
ddaa83eb
JM
656 if (hostapd_prepare_rates(iface->bss[0], iface->current_mode)) {
657 wpa_printf(MSG_ERROR, "Failed to prepare rates table.");
658 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
659 HOSTAPD_LEVEL_WARNING,
660 "Failed to prepare rates table.");
661 return -1;
662 }
6fc6879b 663
61693eaa 664 return 0;
6fc6879b
JM
665}
666
667
668const char * hostapd_hw_mode_txt(int mode)
669{
670 switch (mode) {
671 case HOSTAPD_MODE_IEEE80211A:
672 return "IEEE 802.11a";
673 case HOSTAPD_MODE_IEEE80211B:
674 return "IEEE 802.11b";
675 case HOSTAPD_MODE_IEEE80211G:
676 return "IEEE 802.11g";
677 default:
678 return "UNKNOWN";
679 }
680}
681
682
683int hostapd_hw_get_freq(struct hostapd_data *hapd, int chan)
684{
685 int i;
686
687 if (!hapd->iface->current_mode)
688 return 0;
689
690 for (i = 0; i < hapd->iface->current_mode->num_channels; i++) {
691 struct hostapd_channel_data *ch =
692 &hapd->iface->current_mode->channels[i];
693 if (ch->chan == chan)
694 return ch->freq;
695 }
696
697 return 0;
698}
699
700
701int hostapd_hw_get_channel(struct hostapd_data *hapd, int freq)
702{
703 int i;
704
705 if (!hapd->iface->current_mode)
706 return 0;
707
708 for (i = 0; i < hapd->iface->current_mode->num_channels; i++) {
709 struct hostapd_channel_data *ch =
710 &hapd->iface->current_mode->channels[i];
711 if (ch->freq == freq)
712 return ch->chan;
713 }
714
715 return 0;
716}