Improved the error message for passive scan not being available
[wpasupplicant] / hostapd / hw_features.c
1 /*
2  * hostapd / Hardware feature query and different modes
3  * Copyright 2002-2003, Instant802 Networks, Inc.
4  * Copyright 2005-2006, Devicescape Software, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * Alternatively, this software may be distributed under the terms of BSD
11  * license.
12  *
13  * See README and COPYING for more details.
14  */
15
16 #include "includes.h"
17
18 #include "hostapd.h"
19 #include "hw_features.h"
20 #include "driver.h"
21 #include "config.h"
22 #include "eloop.h"
23
24
25 void hostapd_free_hw_features(struct hostapd_hw_modes *hw_features,
26                               size_t num_hw_features)
27 {
28         size_t i;
29
30         if (hw_features == NULL)
31                 return;
32
33         for (i = 0; i < num_hw_features; i++) {
34                 os_free(hw_features[i].channels);
35                 os_free(hw_features[i].rates);
36         }
37
38         os_free(hw_features);
39 }
40
41
42 int hostapd_get_hw_features(struct hostapd_iface *iface)
43 {
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;
48
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;
58         }
59
60         iface->hw_flags = flags;
61
62         hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
63         iface->hw_features = modes;
64         iface->num_hw_features = num_modes;
65
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++) {
71                         /*
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.
77                          */
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",
88                                    feature->mode,
89                                    feature->channels[j].chan,
90                                    feature->channels[j].freq);
91                 }
92         }
93
94         return ret;
95 }
96
97
98 static int hostapd_prepare_rates(struct hostapd_data *hapd,
99                                  struct hostapd_hw_modes *mode)
100 {
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 };
105         int *basic_rates;
106
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;
112                 break;
113         case HOSTAPD_MODE_IEEE80211B:
114                 basic_rates = basic_rates_b;
115                 break;
116         case HOSTAPD_MODE_IEEE80211G:
117                 basic_rates = basic_rates_g;
118                 break;
119         default:
120                 return -1;
121         }
122
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 "
126                            "module");
127         }
128
129         os_free(hapd->iface->current_rates);
130         hapd->iface->num_rates = 0;
131
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 "
136                            "table.");
137                 return -1;
138         }
139
140         for (i = 0; i < mode->num_rates; i++) {
141                 struct hostapd_rate_data *rate;
142
143                 if (hapd->iconf->supported_rates &&
144                     !hostapd_rate_found(hapd->iconf->supported_rates,
145                                         mode->rates[i].rate))
146                         continue;
147
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;
153                         num_basic_rates++;
154                 } else
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++;
159         }
160
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);
165                 return -1;
166         }
167
168         return 0;
169 }
170
171
172 static void select_hw_mode_start(void *eloop_data, void *user_ctx);
173 static void select_hw_mode2_handler(void *eloop_data, void *user_ctx);
174
175 /**
176  * select_hw_mode_finalize - Finish select HW mode & call the callback
177  * @iface: Pointer to interface data.
178  * @status: Status of the select HW mode (0 on success; -1 on failure).
179  * Returns: 0 on success; -1 on failure (e.g., was not in progress).
180  */
181 static int select_hw_mode_finalize(struct hostapd_iface *iface, int status)
182 {
183         hostapd_iface_cb cb;
184
185         if (!iface->hw_mode_sel_cb)
186                 return -1;
187
188         eloop_cancel_timeout(select_hw_mode_start, iface, NULL);
189         eloop_cancel_timeout(select_hw_mode2_handler, iface, NULL);
190
191         cb = iface->hw_mode_sel_cb;
192
193         iface->hw_mode_sel_cb = NULL;
194
195         cb(iface, status);
196
197         return 0;
198 }
199
200
201 /**
202  * select_hw_mode2 - Select the hardware mode (part 2)
203  * @iface: Pointer to interface data.
204  * @status: Status of auto chanel selection.
205  *
206  * Setup the rates and passive scanning based on the configuration.
207  */
208 static void select_hw_mode2(struct hostapd_iface *iface, int status)
209 {
210         int ret = status;
211         if (ret)
212                 goto fail;
213
214         if (iface->current_mode == NULL) {
215                 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
216                                HOSTAPD_LEVEL_WARNING,
217                                "Hardware does not support configured channel");
218                 ret = -1;
219                 goto fail;
220         }
221
222         if (hostapd_prepare_rates(iface->bss[0], iface->current_mode)) {
223                 wpa_printf(MSG_ERROR, "Failed to prepare rates table.");
224                 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
225                                            HOSTAPD_LEVEL_WARNING,
226                                            "Failed to prepare rates table.");
227                 ret = -1;
228                 goto fail;
229         }
230
231         ret = hostapd_passive_scan(iface->bss[0], 0,
232                                    iface->conf->passive_scan_mode,
233                                    iface->conf->passive_scan_interval,
234                                    iface->conf->passive_scan_listen,
235                                    NULL, NULL);
236         if (ret) {
237                 if (ret == -1) {
238                         wpa_printf(MSG_DEBUG, "Passive scanning not "
239                                    "supported");
240                 } else {
241                         wpa_printf(MSG_ERROR, "Could not set passive "
242                                    "scanning: %s", strerror(ret));
243                 }
244                 ret = 0;
245         }
246
247 fail:
248         select_hw_mode_finalize(iface, ret);
249 }
250
251
252 /**
253  * select_hw_mode2_handler - Calls select_hw_mode2 when auto chan isn't used
254  * @eloop_data: Stores the struct hostapd_iface * for the interface.
255  * @user_ctx: Unused.
256  */
257 static void select_hw_mode2_handler(void *eloop_data, void *user_ctx)
258 {
259         struct hostapd_iface *iface = eloop_data;
260
261         select_hw_mode2(iface, 0);
262 }
263
264
265 /**
266  * select_hw_mode1 - Select the hardware mode (part 1)
267  * @iface: Pointer to interface data.
268  * Returns: 0 on success; -1 on failure.
269  *
270  * Setup the hardware mode and channel based on the configuration.
271  * Schedules select_hw_mode2() to be called immediately or after automatic
272  * channel selection takes place.
273  */
274 static int select_hw_mode1(struct hostapd_iface *iface)
275 {
276         int i, j, ok;
277
278         if (iface->num_hw_features < 1)
279                 return -1;
280
281         iface->current_mode = NULL;
282         for (i = 0; i < iface->num_hw_features; i++) {
283                 struct hostapd_hw_modes *mode = &iface->hw_features[i];
284                 if (mode->mode == (int) iface->conf->hw_mode) {
285                         iface->current_mode = mode;
286                         break;
287                 }
288         }
289
290         if (iface->current_mode == NULL) {
291                 wpa_printf(MSG_ERROR, "Hardware does not support configured "
292                            "mode");
293                 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
294                                HOSTAPD_LEVEL_WARNING,
295                                "Hardware does not support configured mode "
296                                "(%d)", (int) iface->conf->hw_mode);
297                 return -1;
298         }
299
300         ok = 0;
301         for (j = 0; j < iface->current_mode->num_channels; j++) {
302                 struct hostapd_channel_data *chan =
303                         &iface->current_mode->channels[j];
304                 if (!(chan->flag & HOSTAPD_CHAN_DISABLED) &&
305                     (chan->chan == iface->conf->channel)) {
306                         ok = 1;
307                         break;
308                 }
309         }
310         if (ok == 0 && iface->conf->channel != 0) {
311                 hostapd_logger(iface->bss[0], NULL,
312                                HOSTAPD_MODULE_IEEE80211,
313                                HOSTAPD_LEVEL_WARNING,
314                                "Configured channel (%d) not found from the "
315                                "channel list of current mode (%d) %s",
316                                iface->conf->channel,
317                                iface->current_mode->mode,
318                                hostapd_hw_mode_txt(iface->current_mode->mode));
319                 iface->current_mode = NULL;
320         }
321
322         /*
323          * Calls select_hw_mode2() via a handler, so that the function is
324          * always executed from eloop.
325          */
326         eloop_register_timeout(0, 0, select_hw_mode2_handler, iface, NULL);
327         return 0;
328 }
329
330
331 /**
332  * select_hw_mode_start - Handler to start select HW mode
333  * @eloop_data: Stores the struct hostapd_iface * for the interface.
334  * @user_ctx: Unused.
335  *
336  * An eloop handler is used so that all errors can be processed by the
337  * callback without introducing stack recursion.
338  */
339 static void select_hw_mode_start(void *eloop_data, void *user_ctx)
340 {
341         struct hostapd_iface *iface = (struct hostapd_iface *)eloop_data;
342
343         int ret;
344
345         ret = select_hw_mode1(iface);
346         if (ret)
347                 select_hw_mode_finalize(iface, ret);
348 }
349
350
351 /**
352  * hostapd_select_hw_mode_start - Start selection of the hardware mode
353  * @iface: Pointer to interface data.
354  * @cb: The function to callback when done.
355  * Returns:  0 if it starts successfully; cb will be called when done.
356  *          -1 on failure; cb will not be called.
357  *
358  * Sets up the hardware mode, channel, rates, and passive scanning
359  * based on the configuration.
360  */
361 int hostapd_select_hw_mode_start(struct hostapd_iface *iface,
362                                  hostapd_iface_cb cb)
363 {
364         if (iface->hw_mode_sel_cb) {
365                 wpa_printf(MSG_DEBUG,
366                            "%s: Hardware mode select already in progress.",
367                            iface->bss[0]->conf->iface);
368                 return -1;
369         }
370
371         iface->hw_mode_sel_cb = cb;
372
373         eloop_register_timeout(0, 0, select_hw_mode_start, iface, NULL);
374
375         return 0;
376 }
377
378
379 /**
380  * hostapd_auto_chan_select_stop - Stops automatic channel selection
381  * @iface: Pointer to interface data.
382  * Returns:  0 if successfully stopped;
383  *          -1 on failure (i.e., was not in progress)
384  */
385 int hostapd_select_hw_mode_stop(struct hostapd_iface *iface)
386 {
387         return select_hw_mode_finalize(iface, -1);
388 }
389
390
391 const char * hostapd_hw_mode_txt(int mode)
392 {
393         switch (mode) {
394         case HOSTAPD_MODE_IEEE80211A:
395                 return "IEEE 802.11a";
396         case HOSTAPD_MODE_IEEE80211B:
397                 return "IEEE 802.11b";
398         case HOSTAPD_MODE_IEEE80211G:
399                 return "IEEE 802.11g";
400         default:
401                 return "UNKNOWN";
402         }
403 }
404
405
406 int hostapd_hw_get_freq(struct hostapd_data *hapd, int chan)
407 {
408         int i;
409
410         if (!hapd->iface->current_mode)
411                 return 0;
412
413         for (i = 0; i < hapd->iface->current_mode->num_channels; i++) {
414                 struct hostapd_channel_data *ch =
415                         &hapd->iface->current_mode->channels[i];
416                 if (ch->chan == chan)
417                         return ch->freq;
418         }
419
420         return 0;
421 }
422
423
424 int hostapd_hw_get_channel(struct hostapd_data *hapd, int freq)
425 {
426         int i;
427
428         if (!hapd->iface->current_mode)
429                 return 0;
430
431         for (i = 0; i < hapd->iface->current_mode->num_channels; i++) {
432                 struct hostapd_channel_data *ch =
433                         &hapd->iface->current_mode->channels[i];
434                 if (ch->freq == freq)
435                         return ch->chan;
436         }
437
438         return 0;
439 }