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);
237 #endif /* CONFIG_IEEE80211N */
241 * hostapd_select_hw_mode - Select the hardware mode
242 * @iface: Pointer to interface data.
243 * Returns: 0 on success, -1 on failure
245 * Sets up the hardware mode, channel, rates, and passive scanning
246 * based on the configuration.
248 int hostapd_select_hw_mode(struct hostapd_iface *iface)
252 if (iface->num_hw_features < 1)
255 iface->current_mode = NULL;
256 for (i = 0; i < iface->num_hw_features; i++) {
257 struct hostapd_hw_modes *mode = &iface->hw_features[i];
258 if (mode->mode == (int) iface->conf->hw_mode) {
259 iface->current_mode = mode;
264 if (iface->current_mode == NULL) {
265 wpa_printf(MSG_ERROR, "Hardware does not support configured "
267 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
268 HOSTAPD_LEVEL_WARNING,
269 "Hardware does not support configured mode "
270 "(%d)", (int) iface->conf->hw_mode);
275 for (j = 0; j < iface->current_mode->num_channels; j++) {
276 struct hostapd_channel_data *chan =
277 &iface->current_mode->channels[j];
278 if (!(chan->flag & HOSTAPD_CHAN_DISABLED) &&
279 (chan->chan == iface->conf->channel)) {
284 if (ok == 0 && iface->conf->channel != 0) {
285 hostapd_logger(iface->bss[0], NULL,
286 HOSTAPD_MODULE_IEEE80211,
287 HOSTAPD_LEVEL_WARNING,
288 "Configured channel (%d) not found from the "
289 "channel list of current mode (%d) %s",
290 iface->conf->channel,
291 iface->current_mode->mode,
292 hostapd_hw_mode_txt(iface->current_mode->mode));
293 iface->current_mode = NULL;
296 if (iface->current_mode == NULL) {
297 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
298 HOSTAPD_LEVEL_WARNING,
299 "Hardware does not support configured channel");
303 #ifdef CONFIG_IEEE80211N
304 if (!ieee80211n_allowed_ht40_channel_pair(iface))
306 #endif /* CONFIG_IEEE80211N */
308 if (hostapd_prepare_rates(iface->bss[0], iface->current_mode)) {
309 wpa_printf(MSG_ERROR, "Failed to prepare rates table.");
310 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
311 HOSTAPD_LEVEL_WARNING,
312 "Failed to prepare rates table.");
316 ret = hostapd_passive_scan(iface->bss[0], 0,
317 iface->conf->passive_scan_mode,
318 iface->conf->passive_scan_interval,
319 iface->conf->passive_scan_listen,
323 wpa_printf(MSG_DEBUG, "Passive scanning not "
326 wpa_printf(MSG_ERROR, "Could not set passive "
327 "scanning: %s", strerror(ret));
336 const char * hostapd_hw_mode_txt(int mode)
339 case HOSTAPD_MODE_IEEE80211A:
340 return "IEEE 802.11a";
341 case HOSTAPD_MODE_IEEE80211B:
342 return "IEEE 802.11b";
343 case HOSTAPD_MODE_IEEE80211G:
344 return "IEEE 802.11g";
351 int hostapd_hw_get_freq(struct hostapd_data *hapd, int chan)
355 if (!hapd->iface->current_mode)
358 for (i = 0; i < hapd->iface->current_mode->num_channels; i++) {
359 struct hostapd_channel_data *ch =
360 &hapd->iface->current_mode->channels[i];
361 if (ch->chan == chan)
369 int hostapd_hw_get_channel(struct hostapd_data *hapd, int freq)
373 if (!hapd->iface->current_mode)
376 for (i = 0; i < hapd->iface->current_mode->num_channels; i++) {
377 struct hostapd_channel_data *ch =
378 &hapd->iface->current_mode->channels[i];
379 if (ch->freq == freq)