hostapd: Update vendor branch to 0.6.10
[dragonfly.git] / contrib / hostapd / hostapd / hw_features.c
blob1d6299e625ed20a13924b632a76995d036b61a3c
1 /*
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
12 * license.
14 * See README and COPYING for more details.
17 #include "includes.h"
19 #include "hostapd.h"
20 #include "hw_features.h"
21 #include "driver.h"
22 #include "config.h"
25 void hostapd_free_hw_features(struct hostapd_hw_modes *hw_features,
26 size_t num_hw_features)
28 size_t i;
30 if (hw_features == NULL)
31 return;
33 for (i = 0; i < num_hw_features; i++) {
34 os_free(hw_features[i].channels);
35 os_free(hw_features[i].rates);
38 os_free(hw_features);
42 int hostapd_get_hw_features(struct hostapd_iface *iface)
44 struct hostapd_data *hapd = iface->bss[0];
45 int ret = 0, i, j;
46 u16 num_modes, flags;
47 struct hostapd_hw_modes *modes;
49 if (hostapd_drv_none(hapd))
50 return -1;
51 modes = hostapd_get_hw_feature_data(hapd, &num_modes, &flags);
52 if (modes == NULL) {
53 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
54 HOSTAPD_LEVEL_DEBUG,
55 "Fetching hardware channel/rate support not "
56 "supported.");
57 return -1;
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
69 * domain */
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
76 * supported.
78 if (feature->channels[j].flag &
79 (HOSTAPD_CHAN_NO_IBSS |
80 HOSTAPD_CHAN_PASSIVE_SCAN |
81 HOSTAPD_CHAN_RADAR))
82 feature->channels[j].flag |=
83 HOSTAPD_CHAN_DISABLED;
84 if (feature->channels[j].flag & HOSTAPD_CHAN_DISABLED)
85 continue;
86 wpa_printf(MSG_MSGDUMP, "Allowed channel: mode=%d "
87 "chan=%d freq=%d MHz max_tx_power=%d dBm",
88 feature->mode,
89 feature->channels[j].chan,
90 feature->channels[j].freq,
91 feature->channels[j].max_tx_power);
95 return ret;
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 };
106 int *basic_rates;
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;
113 break;
114 case HOSTAPD_MODE_IEEE80211B:
115 basic_rates = basic_rates_b;
116 break;
117 case HOSTAPD_MODE_IEEE80211G:
118 basic_rates = basic_rates_g;
119 break;
120 default:
121 return -1;
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 "
127 "module");
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 "
137 "table.");
138 return -1;
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))
147 continue;
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;
154 num_basic_rates++;
155 } else
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);
166 return -1;
169 return 0;
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,
178 184, 192 };
179 size_t k;
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
190 * channel */
191 ok = 0;
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) {
197 ok = 1;
198 break;
201 if (!ok) {
202 wpa_printf(MSG_ERROR, "HT40 secondary channel %d not allowed",
203 sec_chan);
204 return 0;
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)
214 return 1;
216 if (iface->conf->secondary_channel > 0)
217 first = iface->conf->channel;
218 else
219 first = sec_chan;
221 ok = 0;
222 for (k = 0; k < sizeof(allowed) / sizeof(allowed[0]); k++) {
223 if (first == allowed[k]) {
224 ok = 1;
225 break;
228 if (!ok) {
229 wpa_printf(MSG_ERROR, "HT40 channel pair (%d, %d) not allowed",
230 iface->conf->channel,
231 iface->conf->secondary_channel);
232 return 0;
235 return 1;
239 static int ieee80211n_supported_ht_capab(struct hostapd_iface *iface)
241 u16 hw = iface->current_mode->ht_capab;
242 u16 conf = iface->conf->ht_capab;
244 if (!iface->conf->ieee80211n)
245 return 1;
247 if ((conf & HT_CAP_INFO_LDPC_CODING_CAP) &&
248 !(hw & HT_CAP_INFO_LDPC_CODING_CAP)) {
249 wpa_printf(MSG_ERROR, "Driver does not support configured "
250 "HT capability [LDPC]");
251 return 0;
254 if ((conf & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) &&
255 !(hw & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
256 wpa_printf(MSG_ERROR, "Driver does not support configured "
257 "HT capability [HT40*]");
258 return 0;
261 if ((conf & HT_CAP_INFO_SMPS_MASK) != (hw & HT_CAP_INFO_SMPS_MASK) &&
262 (conf & HT_CAP_INFO_SMPS_MASK) != HT_CAP_INFO_SMPS_DISABLED) {
263 wpa_printf(MSG_ERROR, "Driver does not support configured "
264 "HT capability [SMPS-*]");
265 return 0;
268 if ((conf & HT_CAP_INFO_GREEN_FIELD) &&
269 !(hw & HT_CAP_INFO_GREEN_FIELD)) {
270 wpa_printf(MSG_ERROR, "Driver does not support configured "
271 "HT capability [GF]");
272 return 0;
275 if ((conf & HT_CAP_INFO_SHORT_GI20MHZ) &&
276 !(hw & HT_CAP_INFO_SHORT_GI20MHZ)) {
277 wpa_printf(MSG_ERROR, "Driver does not support configured "
278 "HT capability [SHORT-GI-20]");
279 return 0;
282 if ((conf & HT_CAP_INFO_SHORT_GI40MHZ) &&
283 !(hw & HT_CAP_INFO_SHORT_GI40MHZ)) {
284 wpa_printf(MSG_ERROR, "Driver does not support configured "
285 "HT capability [SHORT-GI-40]");
286 return 0;
289 if ((conf & HT_CAP_INFO_TX_STBC) && !(hw & HT_CAP_INFO_TX_STBC)) {
290 wpa_printf(MSG_ERROR, "Driver does not support configured "
291 "HT capability [TX-STBC]");
292 return 0;
295 if ((conf & HT_CAP_INFO_RX_STBC_MASK) >
296 (hw & HT_CAP_INFO_RX_STBC_MASK)) {
297 wpa_printf(MSG_ERROR, "Driver does not support configured "
298 "HT capability [RX-STBC*]");
299 return 0;
302 if ((conf & HT_CAP_INFO_DELAYED_BA) &&
303 !(hw & HT_CAP_INFO_DELAYED_BA)) {
304 wpa_printf(MSG_ERROR, "Driver does not support configured "
305 "HT capability [DELAYED-BA]");
306 return 0;
309 if ((conf & HT_CAP_INFO_MAX_AMSDU_SIZE) &&
310 !(hw & HT_CAP_INFO_MAX_AMSDU_SIZE)) {
311 wpa_printf(MSG_ERROR, "Driver does not support configured "
312 "HT capability [MAX-AMSDU-7935]");
313 return 0;
316 if ((conf & HT_CAP_INFO_DSSS_CCK40MHZ) &&
317 !(hw & HT_CAP_INFO_DSSS_CCK40MHZ)) {
318 wpa_printf(MSG_ERROR, "Driver does not support configured "
319 "HT capability [DSSS_CCK-40]");
320 return 0;
323 if ((conf & HT_CAP_INFO_PSMP_SUPP) && !(hw & HT_CAP_INFO_PSMP_SUPP)) {
324 wpa_printf(MSG_ERROR, "Driver does not support configured "
325 "HT capability [PSMP]");
326 return 0;
329 if ((conf & HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT) &&
330 !(hw & HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT)) {
331 wpa_printf(MSG_ERROR, "Driver does not support configured "
332 "HT capability [LSIG-TXOP-PROT]");
333 return 0;
336 return 1;
338 #endif /* CONFIG_IEEE80211N */
342 * hostapd_select_hw_mode - Select the hardware mode
343 * @iface: Pointer to interface data.
344 * Returns: 0 on success, -1 on failure
346 * Sets up the hardware mode, channel, rates, and passive scanning
347 * based on the configuration.
349 int hostapd_select_hw_mode(struct hostapd_iface *iface)
351 int i, j, ok, ret;
353 if (iface->num_hw_features < 1)
354 return -1;
356 iface->current_mode = NULL;
357 for (i = 0; i < iface->num_hw_features; i++) {
358 struct hostapd_hw_modes *mode = &iface->hw_features[i];
359 if (mode->mode == (int) iface->conf->hw_mode) {
360 iface->current_mode = mode;
361 break;
365 if (iface->current_mode == NULL) {
366 wpa_printf(MSG_ERROR, "Hardware does not support configured "
367 "mode");
368 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
369 HOSTAPD_LEVEL_WARNING,
370 "Hardware does not support configured mode "
371 "(%d)", (int) iface->conf->hw_mode);
372 return -1;
375 ok = 0;
376 for (j = 0; j < iface->current_mode->num_channels; j++) {
377 struct hostapd_channel_data *chan =
378 &iface->current_mode->channels[j];
379 if (!(chan->flag & HOSTAPD_CHAN_DISABLED) &&
380 (chan->chan == iface->conf->channel)) {
381 ok = 1;
382 break;
385 if (iface->conf->channel == 0) {
386 /* TODO: could request a scan of neighboring BSSes and select
387 * the channel automatically */
388 wpa_printf(MSG_ERROR, "Channel not configured "
389 "(hw_mode/channel in hostapd.conf)");
390 return -1;
392 if (ok == 0 && iface->conf->channel != 0) {
393 hostapd_logger(iface->bss[0], NULL,
394 HOSTAPD_MODULE_IEEE80211,
395 HOSTAPD_LEVEL_WARNING,
396 "Configured channel (%d) not found from the "
397 "channel list of current mode (%d) %s",
398 iface->conf->channel,
399 iface->current_mode->mode,
400 hostapd_hw_mode_txt(iface->current_mode->mode));
401 iface->current_mode = NULL;
404 if (iface->current_mode == NULL) {
405 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
406 HOSTAPD_LEVEL_WARNING,
407 "Hardware does not support configured channel");
408 return -1;
411 #ifdef CONFIG_IEEE80211N
412 if (!ieee80211n_allowed_ht40_channel_pair(iface))
413 return -1;
414 if (!ieee80211n_supported_ht_capab(iface))
415 return -1;
416 #endif /* CONFIG_IEEE80211N */
418 if (hostapd_prepare_rates(iface->bss[0], iface->current_mode)) {
419 wpa_printf(MSG_ERROR, "Failed to prepare rates table.");
420 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
421 HOSTAPD_LEVEL_WARNING,
422 "Failed to prepare rates table.");
423 return -1;
426 ret = hostapd_passive_scan(iface->bss[0], 0,
427 iface->conf->passive_scan_mode,
428 iface->conf->passive_scan_interval,
429 iface->conf->passive_scan_listen,
430 NULL, NULL);
431 if (ret) {
432 if (ret == -1) {
433 wpa_printf(MSG_DEBUG, "Passive scanning not "
434 "supported");
435 } else {
436 wpa_printf(MSG_ERROR, "Could not set passive "
437 "scanning: %s", strerror(ret));
439 ret = 0;
442 return ret;
446 const char * hostapd_hw_mode_txt(int mode)
448 switch (mode) {
449 case HOSTAPD_MODE_IEEE80211A:
450 return "IEEE 802.11a";
451 case HOSTAPD_MODE_IEEE80211B:
452 return "IEEE 802.11b";
453 case HOSTAPD_MODE_IEEE80211G:
454 return "IEEE 802.11g";
455 default:
456 return "UNKNOWN";
461 int hostapd_hw_get_freq(struct hostapd_data *hapd, int chan)
463 int i;
465 if (!hapd->iface->current_mode)
466 return 0;
468 for (i = 0; i < hapd->iface->current_mode->num_channels; i++) {
469 struct hostapd_channel_data *ch =
470 &hapd->iface->current_mode->channels[i];
471 if (ch->chan == chan)
472 return ch->freq;
475 return 0;
479 int hostapd_hw_get_channel(struct hostapd_data *hapd, int freq)
481 int i;
483 if (!hapd->iface->current_mode)
484 return 0;
486 for (i = 0; i < hapd->iface->current_mode->num_channels; i++) {
487 struct hostapd_channel_data *ch =
488 &hapd->iface->current_mode->channels[i];
489 if (ch->freq == freq)
490 return ch->chan;
493 return 0;