2 * hostapd / Hardware feature query and different modes
3 * Copyright 2002-2003, Instant802 Networks, Inc.
4 * Copyright 2005-2006, Devicescape Software, Inc.
5 * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
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.
11 * Alternatively, this software may be distributed under the terms of BSD
14 * See README and COPYING for more details.
20 #include "hw_features.h"
25 void hostapd_free_hw_features(struct hostapd_hw_modes *hw_features,
26 size_t num_hw_features)
30 if (hw_features == NULL)
33 for (i = 0; i < num_hw_features; i++) {
34 os_free(hw_features[i].channels);
35 os_free(hw_features[i].rates);
42 int hostapd_get_hw_features(struct hostapd_iface *iface)
44 struct hostapd_data *hapd = iface->bss[0];
47 struct hostapd_hw_modes *modes;
49 if (hostapd_drv_none(hapd))
51 modes = hostapd_get_hw_feature_data(hapd, &num_modes, &flags);
53 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
55 "Fetching hardware channel/rate support not "
60 iface->hw_flags = flags;
62 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
63 iface->hw_features = modes;
64 iface->num_hw_features = num_modes;
66 for (i = 0; i < num_modes; i++) {
67 struct hostapd_hw_modes *feature = &modes[i];
68 /* set flag for channels we can use in current regulatory
70 for (j = 0; j < feature->num_channels; j++) {
72 * Disable all channels that are marked not to allow
73 * IBSS operation or active scanning. In addition,
74 * disable all channels that require radar detection,
75 * since that (in addition to full DFS) is not yet
78 if (feature->channels[j].flag &
79 (HOSTAPD_CHAN_NO_IBSS |
80 HOSTAPD_CHAN_PASSIVE_SCAN |
82 feature->channels[j].flag |=
83 HOSTAPD_CHAN_DISABLED;
84 if (feature->channels[j].flag & HOSTAPD_CHAN_DISABLED)
86 wpa_printf(MSG_MSGDUMP, "Allowed channel: mode=%d "
87 "chan=%d freq=%d MHz max_tx_power=%d dBm",
89 feature->channels[j].chan,
90 feature->channels[j].freq,
91 feature->channels[j].max_tx_power);
99 static int hostapd_prepare_rates(struct hostapd_data *hapd,
100 struct hostapd_hw_modes *mode)
102 int i, num_basic_rates = 0;
103 int basic_rates_a[] = { 60, 120, 240, -1 };
104 int basic_rates_b[] = { 10, 20, -1 };
105 int basic_rates_g[] = { 10, 20, 55, 110, -1 };
108 if (hapd->iconf->basic_rates)
109 basic_rates = hapd->iconf->basic_rates;
110 else switch (mode->mode) {
111 case HOSTAPD_MODE_IEEE80211A:
112 basic_rates = basic_rates_a;
114 case HOSTAPD_MODE_IEEE80211B:
115 basic_rates = basic_rates_b;
117 case HOSTAPD_MODE_IEEE80211G:
118 basic_rates = basic_rates_g;
124 if (hostapd_set_rate_sets(hapd, hapd->iconf->supported_rates,
125 basic_rates, mode->mode)) {
126 wpa_printf(MSG_ERROR, "Failed to update rate sets in kernel "
130 os_free(hapd->iface->current_rates);
131 hapd->iface->num_rates = 0;
133 hapd->iface->current_rates =
134 os_malloc(mode->num_rates * sizeof(struct hostapd_rate_data));
135 if (!hapd->iface->current_rates) {
136 wpa_printf(MSG_ERROR, "Failed to allocate memory for rate "
141 for (i = 0; i < mode->num_rates; i++) {
142 struct hostapd_rate_data *rate;
144 if (hapd->iconf->supported_rates &&
145 !hostapd_rate_found(hapd->iconf->supported_rates,
146 mode->rates[i].rate))
149 rate = &hapd->iface->current_rates[hapd->iface->num_rates];
150 os_memcpy(rate, &mode->rates[i],
151 sizeof(struct hostapd_rate_data));
152 if (hostapd_rate_found(basic_rates, rate->rate)) {
153 rate->flags |= HOSTAPD_RATE_BASIC;
156 rate->flags &= ~HOSTAPD_RATE_BASIC;
157 wpa_printf(MSG_DEBUG, "RATE[%d] rate=%d flags=0x%x",
158 hapd->iface->num_rates, rate->rate, rate->flags);
159 hapd->iface->num_rates++;
162 if (hapd->iface->num_rates == 0 || num_basic_rates == 0) {
163 wpa_printf(MSG_ERROR, "No rates remaining in supported/basic "
164 "rate sets (%d,%d).",
165 hapd->iface->num_rates, num_basic_rates);
173 #ifdef CONFIG_IEEE80211N
174 static int ieee80211n_allowed_ht40_channel_pair(struct hostapd_iface *iface)
176 int sec_chan, ok, j, first;
177 int allowed[] = { 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157,
181 if (!iface->conf->secondary_channel)
182 return 1; /* HT40 not used */
184 sec_chan = iface->conf->channel + iface->conf->secondary_channel * 4;
185 wpa_printf(MSG_DEBUG, "HT40: control channel: %d "
186 "secondary channel: %d",
187 iface->conf->channel, sec_chan);
189 /* Verify that HT40 secondary channel is an allowed 20 MHz
192 for (j = 0; j < iface->current_mode->num_channels; j++) {
193 struct hostapd_channel_data *chan =
194 &iface->current_mode->channels[j];
195 if (!(chan->flag & HOSTAPD_CHAN_DISABLED) &&
196 chan->chan == sec_chan) {
202 wpa_printf(MSG_ERROR, "HT40 secondary channel %d not allowed",
208 * Verify that HT40 primary,secondary channel pair is allowed per
209 * IEEE 802.11n Annex J. This is only needed for 5 GHz band since
210 * 2.4 GHz rules allow all cases where the secondary channel fits into
211 * the list of allowed channels (already checked above).
213 if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A)
216 if (iface->conf->secondary_channel > 0)
217 first = iface->conf->channel;
222 for (k = 0; k < sizeof(allowed) / sizeof(allowed[0]); k++) {
223 if (first == allowed[k]) {
229 wpa_printf(MSG_ERROR, "HT40 channel pair (%d, %d) not allowed",
230 iface->conf->channel,
231 iface->conf->secondary_channel);
239 static void ieee80211n_switch_pri_sec(struct hostapd_iface *iface)
241 if (iface->conf->secondary_channel > 0) {
242 iface->conf->channel += 4;
243 iface->conf->secondary_channel = -1;
245 iface->conf->channel -= 4;
246 iface->conf->secondary_channel = 1;
251 static int ieee80211n_check_40mhz_5g(struct hostapd_iface *iface)
253 int pri_chan, sec_chan, pri_freq, sec_freq, pri_bss, sec_bss;
254 const struct hostapd_neighbor_bss *n;
258 pri_chan = iface->conf->channel;
259 sec_chan = iface->conf->secondary_channel * 4;
260 pri_freq = hostapd_hw_get_freq(iface->bss[0], pri_chan);
261 if (iface->conf->secondary_channel > 0)
262 sec_freq = pri_freq + 20;
264 sec_freq = pri_freq - 20;
266 n = hostapd_driver_get_neighbor_bss(iface->bss[0], &num);
269 * Switch PRI/SEC channels if Beacons were detected on selected SEC
270 * channel, but not on selected PRI channel.
272 pri_bss = sec_bss = 0;
273 for (i = 0; n && i < num; i++) {
274 if (n[i].freq == pri_freq)
276 else if (n[i].freq == sec_freq)
279 if (sec_bss && !pri_bss) {
280 wpa_printf(MSG_INFO, "Switch own primary and secondary "
281 "channel to get secondary channel with no Beacons "
283 ieee80211n_switch_pri_sec(iface);
287 * Match PRI/SEC channel with any existing HT40 BSS on the same
288 * channels that we are about to use (if already mixed order in
289 * existing BSSes, use own preference).
292 for (i = 0; n && i < num; i++) {
293 if (pri_chan == n[i].pri_chan &&
294 sec_chan == n[i].sec_chan) {
300 for (i = 0; n && i < num; i++) {
301 if (pri_chan == n[i].sec_chan &&
302 sec_chan == n[i].pri_chan) {
303 wpa_printf(MSG_INFO, "Switch own primary and "
304 "secondary channel due to BSS "
305 "overlap with " MACSTR,
306 MAC2STR(n[i].bssid));
307 ieee80211n_switch_pri_sec(iface);
317 static int ieee80211n_check_40mhz_2g4(struct hostapd_iface *iface)
319 int pri_freq, sec_freq;
320 int affected_start, affected_end;
321 const struct hostapd_neighbor_bss *n;
324 pri_freq = hostapd_hw_get_freq(iface->bss[0], iface->conf->channel);
325 if (iface->conf->secondary_channel > 0)
326 sec_freq = pri_freq + 20;
328 sec_freq = pri_freq - 20;
329 affected_start = (pri_freq + sec_freq) / 2 - 25;
330 affected_end = (pri_freq + sec_freq) / 2 + 25;
331 wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
332 affected_start, affected_end);
333 n = hostapd_driver_get_neighbor_bss(iface->bss[0], &num);
334 for (i = 0; n && i < num; i++) {
338 if (n[i].sec_chan < n[i].pri_chan)
344 if ((pri < affected_start || pri > affected_end) &&
345 (sec < affected_start || sec > affected_end))
346 continue; /* not within affected channel range */
349 if (pri_freq != pri || sec_freq != sec) {
350 wpa_printf(MSG_DEBUG, "40 MHz pri/sec "
351 "mismatch with BSS " MACSTR
352 " <%d,%d> (chan=%d%c) vs. <%d,%d>",
354 pri, sec, n[i].pri_chan,
355 sec > pri ? '+' : '-',
361 /* TODO: 40 MHz intolerant */
368 static void ieee80211n_check_40mhz(struct hostapd_iface *iface)
372 if (!iface->conf->secondary_channel)
373 return; /* HT40 not used */
375 /* Check list of neighboring BSSes (from scan) to see whether 40 MHz is
376 * allowed per IEEE 802.11n/D7.0, 11.14.3.2 */
378 if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A)
379 oper40 = ieee80211n_check_40mhz_5g(iface);
381 oper40 = ieee80211n_check_40mhz_2g4(iface);
384 wpa_printf(MSG_INFO, "20/40 MHz operation not permitted on "
385 "channel pri=%d sec=%d based on overlapping BSSes",
386 iface->conf->channel,
387 iface->conf->channel +
388 iface->conf->secondary_channel * 4);
389 iface->conf->secondary_channel = 0;
390 iface->conf->ht_capab &= ~HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
395 static int ieee80211n_supported_ht_capab(struct hostapd_iface *iface)
397 u16 hw = iface->current_mode->ht_capab;
398 u16 conf = iface->conf->ht_capab;
400 if (!iface->conf->ieee80211n)
403 if ((conf & HT_CAP_INFO_LDPC_CODING_CAP) &&
404 !(hw & HT_CAP_INFO_LDPC_CODING_CAP)) {
405 wpa_printf(MSG_ERROR, "Driver does not support configured "
406 "HT capability [LDPC]");
410 if ((conf & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) &&
411 !(hw & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
412 wpa_printf(MSG_ERROR, "Driver does not support configured "
413 "HT capability [HT40*]");
417 if ((conf & HT_CAP_INFO_SMPS_MASK) != (hw & HT_CAP_INFO_SMPS_MASK) &&
418 (conf & HT_CAP_INFO_SMPS_MASK) != HT_CAP_INFO_SMPS_DISABLED) {
419 wpa_printf(MSG_ERROR, "Driver does not support configured "
420 "HT capability [SMPS-*]");
424 if ((conf & HT_CAP_INFO_GREEN_FIELD) &&
425 !(hw & HT_CAP_INFO_GREEN_FIELD)) {
426 wpa_printf(MSG_ERROR, "Driver does not support configured "
427 "HT capability [GF]");
431 if ((conf & HT_CAP_INFO_SHORT_GI20MHZ) &&
432 !(hw & HT_CAP_INFO_SHORT_GI20MHZ)) {
433 wpa_printf(MSG_ERROR, "Driver does not support configured "
434 "HT capability [SHORT-GI-20]");
438 if ((conf & HT_CAP_INFO_SHORT_GI40MHZ) &&
439 !(hw & HT_CAP_INFO_SHORT_GI40MHZ)) {
440 wpa_printf(MSG_ERROR, "Driver does not support configured "
441 "HT capability [SHORT-GI-40]");
445 if ((conf & HT_CAP_INFO_TX_STBC) && !(hw & HT_CAP_INFO_TX_STBC)) {
446 wpa_printf(MSG_ERROR, "Driver does not support configured "
447 "HT capability [TX-STBC]");
451 if ((conf & HT_CAP_INFO_RX_STBC_MASK) >
452 (hw & HT_CAP_INFO_RX_STBC_MASK)) {
453 wpa_printf(MSG_ERROR, "Driver does not support configured "
454 "HT capability [RX-STBC*]");
458 if ((conf & HT_CAP_INFO_DELAYED_BA) &&
459 !(hw & HT_CAP_INFO_DELAYED_BA)) {
460 wpa_printf(MSG_ERROR, "Driver does not support configured "
461 "HT capability [DELAYED-BA]");
465 if ((conf & HT_CAP_INFO_MAX_AMSDU_SIZE) &&
466 !(hw & HT_CAP_INFO_MAX_AMSDU_SIZE)) {
467 wpa_printf(MSG_ERROR, "Driver does not support configured "
468 "HT capability [MAX-AMSDU-7935]");
472 if ((conf & HT_CAP_INFO_DSSS_CCK40MHZ) &&
473 !(hw & HT_CAP_INFO_DSSS_CCK40MHZ)) {
474 wpa_printf(MSG_ERROR, "Driver does not support configured "
475 "HT capability [DSSS_CCK-40]");
479 if ((conf & HT_CAP_INFO_PSMP_SUPP) && !(hw & HT_CAP_INFO_PSMP_SUPP)) {
480 wpa_printf(MSG_ERROR, "Driver does not support configured "
481 "HT capability [PSMP]");
485 if ((conf & HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT) &&
486 !(hw & HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT)) {
487 wpa_printf(MSG_ERROR, "Driver does not support configured "
488 "HT capability [LSIG-TXOP-PROT]");
494 #endif /* CONFIG_IEEE80211N */
498 * hostapd_select_hw_mode - Select the hardware mode
499 * @iface: Pointer to interface data.
500 * Returns: 0 on success, -1 on failure
502 * Sets up the hardware mode, channel, rates, and passive scanning
503 * based on the configuration.
505 int hostapd_select_hw_mode(struct hostapd_iface *iface)
509 if (iface->num_hw_features < 1)
512 iface->current_mode = NULL;
513 for (i = 0; i < iface->num_hw_features; i++) {
514 struct hostapd_hw_modes *mode = &iface->hw_features[i];
515 if (mode->mode == (int) iface->conf->hw_mode) {
516 iface->current_mode = mode;
521 if (iface->current_mode == NULL) {
522 wpa_printf(MSG_ERROR, "Hardware does not support configured "
524 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
525 HOSTAPD_LEVEL_WARNING,
526 "Hardware does not support configured mode "
527 "(%d)", (int) iface->conf->hw_mode);
532 for (j = 0; j < iface->current_mode->num_channels; j++) {
533 struct hostapd_channel_data *chan =
534 &iface->current_mode->channels[j];
535 if (!(chan->flag & HOSTAPD_CHAN_DISABLED) &&
536 (chan->chan == iface->conf->channel)) {
541 if (ok == 0 && iface->conf->channel != 0) {
542 hostapd_logger(iface->bss[0], NULL,
543 HOSTAPD_MODULE_IEEE80211,
544 HOSTAPD_LEVEL_WARNING,
545 "Configured channel (%d) not found from the "
546 "channel list of current mode (%d) %s",
547 iface->conf->channel,
548 iface->current_mode->mode,
549 hostapd_hw_mode_txt(iface->current_mode->mode));
550 iface->current_mode = NULL;
553 if (iface->current_mode == NULL) {
554 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
555 HOSTAPD_LEVEL_WARNING,
556 "Hardware does not support configured channel");
560 #ifdef CONFIG_IEEE80211N
561 ieee80211n_check_40mhz(iface);
562 if (!ieee80211n_allowed_ht40_channel_pair(iface))
564 if (!ieee80211n_supported_ht_capab(iface))
566 #endif /* CONFIG_IEEE80211N */
568 if (hostapd_prepare_rates(iface->bss[0], iface->current_mode)) {
569 wpa_printf(MSG_ERROR, "Failed to prepare rates table.");
570 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
571 HOSTAPD_LEVEL_WARNING,
572 "Failed to prepare rates table.");
576 ret = hostapd_passive_scan(iface->bss[0], 0,
577 iface->conf->passive_scan_mode,
578 iface->conf->passive_scan_interval,
579 iface->conf->passive_scan_listen,
583 wpa_printf(MSG_DEBUG, "Passive scanning not "
586 wpa_printf(MSG_ERROR, "Could not set passive "
587 "scanning: %s", strerror(ret));
596 const char * hostapd_hw_mode_txt(int mode)
599 case HOSTAPD_MODE_IEEE80211A:
600 return "IEEE 802.11a";
601 case HOSTAPD_MODE_IEEE80211B:
602 return "IEEE 802.11b";
603 case HOSTAPD_MODE_IEEE80211G:
604 return "IEEE 802.11g";
611 int hostapd_hw_get_freq(struct hostapd_data *hapd, int chan)
615 if (!hapd->iface->current_mode)
618 for (i = 0; i < hapd->iface->current_mode->num_channels; i++) {
619 struct hostapd_channel_data *ch =
620 &hapd->iface->current_mode->channels[i];
621 if (ch->chan == chan)
629 int hostapd_hw_get_channel(struct hostapd_data *hapd, int freq)
633 if (!hapd->iface->current_mode)
636 for (i = 0; i < hapd->iface->current_mode->num_channels; i++) {
637 struct hostapd_channel_data *ch =
638 &hapd->iface->current_mode->channels[i];
639 if (ch->freq == freq)