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",
89 feature->channels[j].chan,
90 feature->channels[j].freq);
98 static int hostapd_prepare_rates(struct hostapd_data *hapd,
99 struct hostapd_hw_modes *mode)
101 int i, num_basic_rates = 0;
102 int basic_rates_a[] = { 60, 120, 240, -1 };
103 int basic_rates_b[] = { 10, 20, -1 };
104 int basic_rates_g[] = { 10, 20, 55, 110, -1 };
107 if (hapd->iconf->basic_rates)
108 basic_rates = hapd->iconf->basic_rates;
109 else switch (mode->mode) {
110 case HOSTAPD_MODE_IEEE80211A:
111 basic_rates = basic_rates_a;
113 case HOSTAPD_MODE_IEEE80211B:
114 basic_rates = basic_rates_b;
116 case HOSTAPD_MODE_IEEE80211G:
117 basic_rates = basic_rates_g;
123 if (hostapd_set_rate_sets(hapd, hapd->iconf->supported_rates,
124 basic_rates, mode->mode)) {
125 wpa_printf(MSG_ERROR, "Failed to update rate sets in kernel "
129 os_free(hapd->iface->current_rates);
130 hapd->iface->num_rates = 0;
132 hapd->iface->current_rates =
133 os_malloc(mode->num_rates * sizeof(struct hostapd_rate_data));
134 if (!hapd->iface->current_rates) {
135 wpa_printf(MSG_ERROR, "Failed to allocate memory for rate "
140 for (i = 0; i < mode->num_rates; i++) {
141 struct hostapd_rate_data *rate;
143 if (hapd->iconf->supported_rates &&
144 !hostapd_rate_found(hapd->iconf->supported_rates,
145 mode->rates[i].rate))
148 rate = &hapd->iface->current_rates[hapd->iface->num_rates];
149 os_memcpy(rate, &mode->rates[i],
150 sizeof(struct hostapd_rate_data));
151 if (hostapd_rate_found(basic_rates, rate->rate)) {
152 rate->flags |= HOSTAPD_RATE_BASIC;
155 rate->flags &= ~HOSTAPD_RATE_BASIC;
156 wpa_printf(MSG_DEBUG, "RATE[%d] rate=%d flags=0x%x",
157 hapd->iface->num_rates, rate->rate, rate->flags);
158 hapd->iface->num_rates++;
161 if (hapd->iface->num_rates == 0 || num_basic_rates == 0) {
162 wpa_printf(MSG_ERROR, "No rates remaining in supported/basic "
163 "rate sets (%d,%d).",
164 hapd->iface->num_rates, num_basic_rates);
173 * hostapd_select_hw_mode - Select the hardware mode
174 * @iface: Pointer to interface data.
175 * Returns: 0 on success, -1 on failure
177 * Sets up the hardware mode, channel, rates, and passive scanning
178 * based on the configuration.
180 int hostapd_select_hw_mode(struct hostapd_iface *iface)
184 if (iface->num_hw_features < 1)
187 iface->current_mode = NULL;
188 for (i = 0; i < iface->num_hw_features; i++) {
189 struct hostapd_hw_modes *mode = &iface->hw_features[i];
190 if (mode->mode == (int) iface->conf->hw_mode) {
191 iface->current_mode = mode;
196 if (iface->current_mode == NULL) {
197 wpa_printf(MSG_ERROR, "Hardware does not support configured "
199 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
200 HOSTAPD_LEVEL_WARNING,
201 "Hardware does not support configured mode "
202 "(%d)", (int) iface->conf->hw_mode);
207 for (j = 0; j < iface->current_mode->num_channels; j++) {
208 struct hostapd_channel_data *chan =
209 &iface->current_mode->channels[j];
210 if (!(chan->flag & HOSTAPD_CHAN_DISABLED) &&
211 (chan->chan == iface->conf->channel)) {
216 if (ok == 0 && iface->conf->channel != 0) {
217 hostapd_logger(iface->bss[0], NULL,
218 HOSTAPD_MODULE_IEEE80211,
219 HOSTAPD_LEVEL_WARNING,
220 "Configured channel (%d) not found from the "
221 "channel list of current mode (%d) %s",
222 iface->conf->channel,
223 iface->current_mode->mode,
224 hostapd_hw_mode_txt(iface->current_mode->mode));
225 iface->current_mode = NULL;
228 if (iface->current_mode == NULL) {
229 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
230 HOSTAPD_LEVEL_WARNING,
231 "Hardware does not support configured channel");
235 if (hostapd_prepare_rates(iface->bss[0], iface->current_mode)) {
236 wpa_printf(MSG_ERROR, "Failed to prepare rates table.");
237 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
238 HOSTAPD_LEVEL_WARNING,
239 "Failed to prepare rates table.");
243 ret = hostapd_passive_scan(iface->bss[0], 0,
244 iface->conf->passive_scan_mode,
245 iface->conf->passive_scan_interval,
246 iface->conf->passive_scan_listen,
250 wpa_printf(MSG_DEBUG, "Passive scanning not "
253 wpa_printf(MSG_ERROR, "Could not set passive "
254 "scanning: %s", strerror(ret));
263 const char * hostapd_hw_mode_txt(int mode)
266 case HOSTAPD_MODE_IEEE80211A:
267 return "IEEE 802.11a";
268 case HOSTAPD_MODE_IEEE80211B:
269 return "IEEE 802.11b";
270 case HOSTAPD_MODE_IEEE80211G:
271 return "IEEE 802.11g";
278 int hostapd_hw_get_freq(struct hostapd_data *hapd, int chan)
282 if (!hapd->iface->current_mode)
285 for (i = 0; i < hapd->iface->current_mode->num_channels; i++) {
286 struct hostapd_channel_data *ch =
287 &hapd->iface->current_mode->channels[i];
288 if (ch->chan == chan)
296 int hostapd_hw_get_channel(struct hostapd_data *hapd, int freq)
300 if (!hapd->iface->current_mode)
303 for (i = 0; i < hapd->iface->current_mode->num_channels; i++) {
304 struct hostapd_channel_data *ch =
305 &hapd->iface->current_mode->channels[i];
306 if (ch->freq == freq)