Clean up HT40 scan and share nl80211 scanning code
[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  * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
6  *
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.
10  *
11  * Alternatively, this software may be distributed under the terms of BSD
12  * license.
13  *
14  * See README and COPYING for more details.
15  */
16
17 #include "includes.h"
18
19 #include "hostapd.h"
20 #include "ieee802_11_defs.h"
21 #include "ieee802_11_common.h"
22 #include "eloop.h"
23 #include "hw_features.h"
24 #include "driver_i.h"
25 #include "config.h"
26
27
28 void hostapd_free_hw_features(struct hostapd_hw_modes *hw_features,
29                               size_t num_hw_features)
30 {
31         size_t i;
32
33         if (hw_features == NULL)
34                 return;
35
36         for (i = 0; i < num_hw_features; i++) {
37                 os_free(hw_features[i].channels);
38                 os_free(hw_features[i].rates);
39         }
40
41         os_free(hw_features);
42 }
43
44
45 int hostapd_get_hw_features(struct hostapd_iface *iface)
46 {
47         struct hostapd_data *hapd = iface->bss[0];
48         int ret = 0, i, j;
49         u16 num_modes, flags;
50         struct hostapd_hw_modes *modes;
51
52         if (hostapd_drv_none(hapd))
53                 return -1;
54         modes = hostapd_get_hw_feature_data(hapd, &num_modes, &flags);
55         if (modes == NULL) {
56                 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
57                                HOSTAPD_LEVEL_DEBUG,
58                                "Fetching hardware channel/rate support not "
59                                "supported.");
60                 return -1;
61         }
62
63         iface->hw_flags = flags;
64
65         hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
66         iface->hw_features = modes;
67         iface->num_hw_features = num_modes;
68
69         for (i = 0; i < num_modes; i++) {
70                 struct hostapd_hw_modes *feature = &modes[i];
71                 /* set flag for channels we can use in current regulatory
72                  * domain */
73                 for (j = 0; j < feature->num_channels; j++) {
74                         /*
75                          * Disable all channels that are marked not to allow
76                          * IBSS operation or active scanning. In addition,
77                          * disable all channels that require radar detection,
78                          * since that (in addition to full DFS) is not yet
79                          * supported.
80                          */
81                         if (feature->channels[j].flag &
82                             (HOSTAPD_CHAN_NO_IBSS |
83                              HOSTAPD_CHAN_PASSIVE_SCAN |
84                              HOSTAPD_CHAN_RADAR))
85                                 feature->channels[j].flag |=
86                                         HOSTAPD_CHAN_DISABLED;
87                         if (feature->channels[j].flag & HOSTAPD_CHAN_DISABLED)
88                                 continue;
89                         wpa_printf(MSG_MSGDUMP, "Allowed channel: mode=%d "
90                                    "chan=%d freq=%d MHz max_tx_power=%d dBm",
91                                    feature->mode,
92                                    feature->channels[j].chan,
93                                    feature->channels[j].freq,
94                                    feature->channels[j].max_tx_power);
95                 }
96         }
97
98         return ret;
99 }
100
101
102 static int hostapd_prepare_rates(struct hostapd_data *hapd,
103                                  struct hostapd_hw_modes *mode)
104 {
105         int i, num_basic_rates = 0;
106         int basic_rates_a[] = { 60, 120, 240, -1 };
107         int basic_rates_b[] = { 10, 20, -1 };
108         int basic_rates_g[] = { 10, 20, 55, 110, -1 };
109         int *basic_rates;
110
111         if (hapd->iconf->basic_rates)
112                 basic_rates = hapd->iconf->basic_rates;
113         else switch (mode->mode) {
114         case HOSTAPD_MODE_IEEE80211A:
115                 basic_rates = basic_rates_a;
116                 break;
117         case HOSTAPD_MODE_IEEE80211B:
118                 basic_rates = basic_rates_b;
119                 break;
120         case HOSTAPD_MODE_IEEE80211G:
121                 basic_rates = basic_rates_g;
122                 break;
123         default:
124                 return -1;
125         }
126
127         if (hostapd_set_rate_sets(hapd, hapd->iconf->supported_rates,
128                                   basic_rates, mode->mode)) {
129                 wpa_printf(MSG_ERROR, "Failed to update rate sets in kernel "
130                            "module");
131         }
132
133         os_free(hapd->iface->current_rates);
134         hapd->iface->num_rates = 0;
135
136         hapd->iface->current_rates =
137                 os_malloc(mode->num_rates * sizeof(struct hostapd_rate_data));
138         if (!hapd->iface->current_rates) {
139                 wpa_printf(MSG_ERROR, "Failed to allocate memory for rate "
140                            "table.");
141                 return -1;
142         }
143
144         for (i = 0; i < mode->num_rates; i++) {
145                 struct hostapd_rate_data *rate;
146
147                 if (hapd->iconf->supported_rates &&
148                     !hostapd_rate_found(hapd->iconf->supported_rates,
149                                         mode->rates[i].rate))
150                         continue;
151
152                 rate = &hapd->iface->current_rates[hapd->iface->num_rates];
153                 os_memcpy(rate, &mode->rates[i],
154                           sizeof(struct hostapd_rate_data));
155                 if (hostapd_rate_found(basic_rates, rate->rate)) {
156                         rate->flags |= HOSTAPD_RATE_BASIC;
157                         num_basic_rates++;
158                 } else
159                         rate->flags &= ~HOSTAPD_RATE_BASIC;
160                 wpa_printf(MSG_DEBUG, "RATE[%d] rate=%d flags=0x%x",
161                            hapd->iface->num_rates, rate->rate, rate->flags);
162                 hapd->iface->num_rates++;
163         }
164
165         if (hapd->iface->num_rates == 0 || num_basic_rates == 0) {
166                 wpa_printf(MSG_ERROR, "No rates remaining in supported/basic "
167                            "rate sets (%d,%d).",
168                            hapd->iface->num_rates, num_basic_rates);
169                 return -1;
170         }
171
172         return 0;
173 }
174
175
176 #ifdef CONFIG_IEEE80211N
177 static int ieee80211n_allowed_ht40_channel_pair(struct hostapd_iface *iface)
178 {
179         int sec_chan, ok, j, first;
180         int allowed[] = { 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157,
181                           184, 192 };
182         size_t k;
183
184         if (!iface->conf->secondary_channel)
185                 return 1; /* HT40 not used */
186
187         sec_chan = iface->conf->channel + iface->conf->secondary_channel * 4;
188         wpa_printf(MSG_DEBUG, "HT40: control channel: %d  "
189                    "secondary channel: %d",
190                    iface->conf->channel, sec_chan);
191
192         /* Verify that HT40 secondary channel is an allowed 20 MHz
193          * channel */
194         ok = 0;
195         for (j = 0; j < iface->current_mode->num_channels; j++) {
196                 struct hostapd_channel_data *chan =
197                         &iface->current_mode->channels[j];
198                 if (!(chan->flag & HOSTAPD_CHAN_DISABLED) &&
199                     chan->chan == sec_chan) {
200                         ok = 1;
201                         break;
202                 }
203         }
204         if (!ok) {
205                 wpa_printf(MSG_ERROR, "HT40 secondary channel %d not allowed",
206                            sec_chan);
207                 return 0;
208         }
209
210         /*
211          * Verify that HT40 primary,secondary channel pair is allowed per
212          * IEEE 802.11n Annex J. This is only needed for 5 GHz band since
213          * 2.4 GHz rules allow all cases where the secondary channel fits into
214          * the list of allowed channels (already checked above).
215          */
216         if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A)
217                 return 1;
218
219         if (iface->conf->secondary_channel > 0)
220                 first = iface->conf->channel;
221         else
222                 first = sec_chan;
223
224         ok = 0;
225         for (k = 0; k < sizeof(allowed) / sizeof(allowed[0]); k++) {
226                 if (first == allowed[k]) {
227                         ok = 1;
228                         break;
229                 }
230         }
231         if (!ok) {
232                 wpa_printf(MSG_ERROR, "HT40 channel pair (%d, %d) not allowed",
233                            iface->conf->channel,
234                            iface->conf->secondary_channel);
235                 return 0;
236         }
237
238         return 1;
239 }
240
241
242 static void ieee80211n_switch_pri_sec(struct hostapd_iface *iface)
243 {
244         if (iface->conf->secondary_channel > 0) {
245                 iface->conf->channel += 4;
246                 iface->conf->secondary_channel = -1;
247         } else {
248                 iface->conf->channel -= 4;
249                 iface->conf->secondary_channel = 1;
250         }
251 }
252
253
254 static void ieee80211n_get_pri_sec_chan(struct wpa_scan_res *bss,
255                                         int *pri_chan, int *sec_chan)
256 {
257         struct ieee80211_ht_operation *oper;
258         struct ieee802_11_elems elems;
259
260         *pri_chan = *sec_chan = 0;
261
262         ieee802_11_parse_elems((u8 *) (bss + 1), bss->ie_len, &elems, 0);
263         if (elems.ht_operation &&
264             elems.ht_operation_len >= sizeof(*oper)) {
265                 oper = (struct ieee80211_ht_operation *) elems.ht_operation;
266                 *pri_chan = oper->control_chan;
267                 if (oper->ht_param & HT_INFO_HT_PARAM_REC_TRANS_CHNL_WIDTH) {
268                         if (oper->ht_param &
269                             HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
270                                 *sec_chan = *pri_chan + 4;
271                         else if (oper->ht_param &
272                                  HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
273                                 *sec_chan = *pri_chan - 4;
274                 }
275         }
276 }
277
278
279 static int ieee80211n_check_40mhz_5g(struct hostapd_iface *iface,
280                                      struct wpa_scan_results *scan_res)
281 {
282         int pri_chan, sec_chan, pri_freq, sec_freq, pri_bss, sec_bss;
283         int bss_pri_chan, bss_sec_chan;
284         size_t i;
285         int match;
286
287         pri_chan = iface->conf->channel;
288         sec_chan = iface->conf->secondary_channel * 4;
289         pri_freq = hostapd_hw_get_freq(iface->bss[0], pri_chan);
290         if (iface->conf->secondary_channel > 0)
291                 sec_freq = pri_freq + 20;
292         else
293                 sec_freq = pri_freq - 20;
294
295         /*
296          * Switch PRI/SEC channels if Beacons were detected on selected SEC
297          * channel, but not on selected PRI channel.
298          */
299         pri_bss = sec_bss = 0;
300         for (i = 0; i < scan_res->num; i++) {
301                 struct wpa_scan_res *bss = scan_res->res[i];
302                 if (bss->freq == pri_freq)
303                         pri_bss++;
304                 else if (bss->freq == sec_freq)
305                         sec_bss++;
306         }
307         if (sec_bss && !pri_bss) {
308                 wpa_printf(MSG_INFO, "Switch own primary and secondary "
309                            "channel to get secondary channel with no Beacons "
310                            "from other BSSes");
311                 ieee80211n_switch_pri_sec(iface);
312         }
313
314         /*
315          * Match PRI/SEC channel with any existing HT40 BSS on the same
316          * channels that we are about to use (if already mixed order in
317          * existing BSSes, use own preference).
318          */
319         match = 0;
320         for (i = 0; i < scan_res->num; i++) {
321                 struct wpa_scan_res *bss = scan_res->res[i];
322                 ieee80211n_get_pri_sec_chan(bss, &bss_pri_chan, &bss_sec_chan);
323                 if (pri_chan == bss_pri_chan &&
324                     sec_chan == bss_sec_chan) {
325                         match = 1;
326                         break;
327                 }
328         }
329         if (!match) {
330                 for (i = 0; i < scan_res->num; i++) {
331                         struct wpa_scan_res *bss = scan_res->res[i];
332                         ieee80211n_get_pri_sec_chan(bss, &pri_chan, &sec_chan);
333                         if (pri_chan == bss_sec_chan &&
334                             sec_chan == bss_pri_chan) {
335                                 wpa_printf(MSG_INFO, "Switch own primary and "
336                                            "secondary channel due to BSS "
337                                            "overlap with " MACSTR,
338                                            MAC2STR(bss->bssid));
339                                 ieee80211n_switch_pri_sec(iface);
340                                 break;
341                         }
342                 }
343         }
344
345         return 1;
346 }
347
348
349 static int ieee80211n_check_40mhz_2g4(struct hostapd_iface *iface,
350                                       struct wpa_scan_results *scan_res)
351 {
352         int pri_freq, sec_freq;
353         int affected_start, affected_end;
354         size_t i;
355
356         pri_freq = hostapd_hw_get_freq(iface->bss[0], iface->conf->channel);
357         if (iface->conf->secondary_channel > 0)
358                 sec_freq = pri_freq + 20;
359         else
360                 sec_freq = pri_freq - 20;
361         affected_start = (pri_freq + sec_freq) / 2 - 25;
362         affected_end = (pri_freq + sec_freq) / 2 + 25;
363         wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
364                    affected_start, affected_end);
365         for (i = 0; i < scan_res->num; i++) {
366                 struct wpa_scan_res *bss = scan_res->res[i];
367                 int pri = bss->freq;
368                 int sec = pri;
369                 int sec_chan, pri_chan;
370
371                 ieee80211n_get_pri_sec_chan(bss, &pri_chan, &sec_chan);
372
373                 if (sec_chan) {
374                         if (sec_chan < pri_chan)
375                                 sec = pri - 20;
376                         else
377                                 sec = pri + 20;
378                 }
379
380                 if ((pri < affected_start || pri > affected_end) &&
381                     (sec < affected_start || sec > affected_end))
382                         continue; /* not within affected channel range */
383
384                 wpa_printf(MSG_DEBUG, "Neighboring BSS: " MACSTR
385                            " freq=%d pri=%d sec=%d",
386                            MAC2STR(bss->bssid), bss->freq, pri_chan, sec_chan);
387
388                 if (sec_chan) {
389                         if (pri_freq != pri || sec_freq != sec) {
390                                 wpa_printf(MSG_DEBUG, "40 MHz pri/sec "
391                                            "mismatch with BSS " MACSTR
392                                            " <%d,%d> (chan=%d%c) vs. <%d,%d>",
393                                            MAC2STR(bss->bssid),
394                                            pri, sec, pri_chan,
395                                            sec > pri ? '+' : '-',
396                                            pri_freq, sec_freq);
397                                 return 0;
398                         }
399                 }
400
401                 /* TODO: 40 MHz intolerant */
402         }
403
404         return 1;
405 }
406
407
408 static void ieee80211n_check_scan(struct hostapd_iface *iface)
409 {
410         struct wpa_scan_results *scan_res;
411         int oper40;
412
413         /* Check list of neighboring BSSes (from scan) to see whether 40 MHz is
414          * allowed per IEEE 802.11n/D7.0, 11.14.3.2 */
415
416         iface->scan_cb = NULL;
417
418         scan_res = hostapd_driver_get_scan_results(iface->bss[0]);
419         if (scan_res == NULL) {
420                 hostapd_setup_interface_complete(iface, 1);
421                 return;
422         }
423
424         if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A)
425                 oper40 = ieee80211n_check_40mhz_5g(iface, scan_res);
426         else
427                 oper40 = ieee80211n_check_40mhz_2g4(iface, scan_res);
428         wpa_scan_results_free(scan_res);
429
430         if (!oper40) {
431                 wpa_printf(MSG_INFO, "20/40 MHz operation not permitted on "
432                            "channel pri=%d sec=%d based on overlapping BSSes",
433                            iface->conf->channel,
434                            iface->conf->channel +
435                            iface->conf->secondary_channel * 4);
436                 iface->conf->secondary_channel = 0;
437                 iface->conf->ht_capab &= ~HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
438         }
439
440         hostapd_setup_interface_complete(iface, 0);
441 }
442
443
444 static int ieee80211n_check_40mhz(struct hostapd_iface *iface)
445 {
446         struct wpa_driver_scan_params params;
447
448         if (!iface->conf->secondary_channel)
449                 return 0; /* HT40 not used */
450
451         wpa_printf(MSG_DEBUG, "Scan for neighboring BSSes prior to enabling "
452                    "40 MHz channel");
453         os_memset(&params, 0, sizeof(params));
454         /* TODO: scan only the needed frequency */
455         if (hostapd_driver_scan(iface->bss[0], &params) < 0) {
456                 wpa_printf(MSG_ERROR, "Failed to request a scan of "
457                            "neighboring BSSes");
458                 return -1;
459         }
460
461         iface->scan_cb = ieee80211n_check_scan;
462         return 1;
463 }
464
465
466 static int ieee80211n_supported_ht_capab(struct hostapd_iface *iface)
467 {
468         u16 hw = iface->current_mode->ht_capab;
469         u16 conf = iface->conf->ht_capab;
470
471         if (!iface->conf->ieee80211n)
472                 return 1;
473
474         if ((conf & HT_CAP_INFO_LDPC_CODING_CAP) &&
475             !(hw & HT_CAP_INFO_LDPC_CODING_CAP)) {
476                 wpa_printf(MSG_ERROR, "Driver does not support configured "
477                            "HT capability [LDPC]");
478                 return 0;
479         }
480
481         if ((conf & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) &&
482             !(hw & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
483                 wpa_printf(MSG_ERROR, "Driver does not support configured "
484                            "HT capability [HT40*]");
485                 return 0;
486         }
487
488         if ((conf & HT_CAP_INFO_SMPS_MASK) != (hw & HT_CAP_INFO_SMPS_MASK) &&
489             (conf & HT_CAP_INFO_SMPS_MASK) != HT_CAP_INFO_SMPS_DISABLED) {
490                 wpa_printf(MSG_ERROR, "Driver does not support configured "
491                            "HT capability [SMPS-*]");
492                 return 0;
493         }
494
495         if ((conf & HT_CAP_INFO_GREEN_FIELD) &&
496             !(hw & HT_CAP_INFO_GREEN_FIELD)) {
497                 wpa_printf(MSG_ERROR, "Driver does not support configured "
498                            "HT capability [GF]");
499                 return 0;
500         }
501
502         if ((conf & HT_CAP_INFO_SHORT_GI20MHZ) &&
503             !(hw & HT_CAP_INFO_SHORT_GI20MHZ)) {
504                 wpa_printf(MSG_ERROR, "Driver does not support configured "
505                            "HT capability [SHORT-GI-20]");
506                 return 0;
507         }
508
509         if ((conf & HT_CAP_INFO_SHORT_GI40MHZ) &&
510             !(hw & HT_CAP_INFO_SHORT_GI40MHZ)) {
511                 wpa_printf(MSG_ERROR, "Driver does not support configured "
512                            "HT capability [SHORT-GI-40]");
513                 return 0;
514         }
515
516         if ((conf & HT_CAP_INFO_TX_STBC) && !(hw & HT_CAP_INFO_TX_STBC)) {
517                 wpa_printf(MSG_ERROR, "Driver does not support configured "
518                            "HT capability [TX-STBC]");
519                 return 0;
520         }
521
522         if ((conf & HT_CAP_INFO_RX_STBC_MASK) >
523             (hw & HT_CAP_INFO_RX_STBC_MASK)) {
524                 wpa_printf(MSG_ERROR, "Driver does not support configured "
525                            "HT capability [RX-STBC*]");
526                 return 0;
527         }
528
529         if ((conf & HT_CAP_INFO_DELAYED_BA) &&
530             !(hw & HT_CAP_INFO_DELAYED_BA)) {
531                 wpa_printf(MSG_ERROR, "Driver does not support configured "
532                            "HT capability [DELAYED-BA]");
533                 return 0;
534         }
535
536         if ((conf & HT_CAP_INFO_MAX_AMSDU_SIZE) &&
537             !(hw & HT_CAP_INFO_MAX_AMSDU_SIZE)) {
538                 wpa_printf(MSG_ERROR, "Driver does not support configured "
539                            "HT capability [MAX-AMSDU-7935]");
540                 return 0;
541         }
542
543         if ((conf & HT_CAP_INFO_DSSS_CCK40MHZ) &&
544             !(hw & HT_CAP_INFO_DSSS_CCK40MHZ)) {
545                 wpa_printf(MSG_ERROR, "Driver does not support configured "
546                            "HT capability [DSSS_CCK-40]");
547                 return 0;
548         }
549
550         if ((conf & HT_CAP_INFO_PSMP_SUPP) && !(hw & HT_CAP_INFO_PSMP_SUPP)) {
551                 wpa_printf(MSG_ERROR, "Driver does not support configured "
552                            "HT capability [PSMP]");
553                 return 0;
554         }
555
556         if ((conf & HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT) &&
557             !(hw & HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT)) {
558                 wpa_printf(MSG_ERROR, "Driver does not support configured "
559                            "HT capability [LSIG-TXOP-PROT]");
560                 return 0;
561         }
562
563         return 1;
564 }
565
566 #endif /* CONFIG_IEEE80211N */
567
568
569 int hostapd_check_ht_capab(struct hostapd_iface *iface)
570 {
571 #ifdef CONFIG_IEEE80211N
572         int ret;
573         ret = ieee80211n_check_40mhz(iface);
574         if (ret)
575                 return ret;
576         if (!ieee80211n_allowed_ht40_channel_pair(iface))
577                 return -1;
578         if (!ieee80211n_supported_ht_capab(iface))
579                 return -1;
580 #endif /* CONFIG_IEEE80211N */
581
582         return 0;
583 }
584
585
586 /**
587  * hostapd_select_hw_mode - Select the hardware mode
588  * @iface: Pointer to interface data.
589  * Returns: 0 on success, -1 on failure
590  *
591  * Sets up the hardware mode, channel, rates, and passive scanning
592  * based on the configuration.
593  */
594 int hostapd_select_hw_mode(struct hostapd_iface *iface)
595 {
596         int i, j, ok, ret;
597
598         if (iface->num_hw_features < 1)
599                 return -1;
600
601         iface->current_mode = NULL;
602         for (i = 0; i < iface->num_hw_features; i++) {
603                 struct hostapd_hw_modes *mode = &iface->hw_features[i];
604                 if (mode->mode == iface->conf->hw_mode) {
605                         iface->current_mode = mode;
606                         break;
607                 }
608         }
609
610         if (iface->current_mode == NULL) {
611                 wpa_printf(MSG_ERROR, "Hardware does not support configured "
612                            "mode");
613                 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
614                                HOSTAPD_LEVEL_WARNING,
615                                "Hardware does not support configured mode "
616                                "(%d)", (int) iface->conf->hw_mode);
617                 return -1;
618         }
619
620         ok = 0;
621         for (j = 0; j < iface->current_mode->num_channels; j++) {
622                 struct hostapd_channel_data *chan =
623                         &iface->current_mode->channels[j];
624                 if (!(chan->flag & HOSTAPD_CHAN_DISABLED) &&
625                     (chan->chan == iface->conf->channel)) {
626                         ok = 1;
627                         break;
628                 }
629         }
630         if (ok == 0 && iface->conf->channel != 0) {
631                 hostapd_logger(iface->bss[0], NULL,
632                                HOSTAPD_MODULE_IEEE80211,
633                                HOSTAPD_LEVEL_WARNING,
634                                "Configured channel (%d) not found from the "
635                                "channel list of current mode (%d) %s",
636                                iface->conf->channel,
637                                iface->current_mode->mode,
638                                hostapd_hw_mode_txt(iface->current_mode->mode));
639                 iface->current_mode = NULL;
640         }
641
642         if (iface->current_mode == NULL) {
643                 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
644                                HOSTAPD_LEVEL_WARNING,
645                                "Hardware does not support configured channel");
646                 return -1;
647         }
648
649         if (hostapd_prepare_rates(iface->bss[0], iface->current_mode)) {
650                 wpa_printf(MSG_ERROR, "Failed to prepare rates table.");
651                 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
652                                            HOSTAPD_LEVEL_WARNING,
653                                            "Failed to prepare rates table.");
654                 return -1;
655         }
656
657         ret = hostapd_passive_scan(iface->bss[0], 0,
658                                    iface->conf->passive_scan_mode,
659                                    iface->conf->passive_scan_interval,
660                                    iface->conf->passive_scan_listen,
661                                    NULL, NULL);
662         if (ret) {
663                 if (ret == -1) {
664                         wpa_printf(MSG_DEBUG, "Passive scanning not "
665                                    "supported");
666                 } else {
667                         wpa_printf(MSG_ERROR, "Could not set passive "
668                                    "scanning: %s", strerror(ret));
669                 }
670                 ret = 0;
671         }
672
673         return ret;
674 }
675
676
677 const char * hostapd_hw_mode_txt(int mode)
678 {
679         switch (mode) {
680         case HOSTAPD_MODE_IEEE80211A:
681                 return "IEEE 802.11a";
682         case HOSTAPD_MODE_IEEE80211B:
683                 return "IEEE 802.11b";
684         case HOSTAPD_MODE_IEEE80211G:
685                 return "IEEE 802.11g";
686         default:
687                 return "UNKNOWN";
688         }
689 }
690
691
692 int hostapd_hw_get_freq(struct hostapd_data *hapd, int chan)
693 {
694         int i;
695
696         if (!hapd->iface->current_mode)
697                 return 0;
698
699         for (i = 0; i < hapd->iface->current_mode->num_channels; i++) {
700                 struct hostapd_channel_data *ch =
701                         &hapd->iface->current_mode->channels[i];
702                 if (ch->chan == chan)
703                         return ch->freq;
704         }
705
706         return 0;
707 }
708
709
710 int hostapd_hw_get_channel(struct hostapd_data *hapd, int freq)
711 {
712         int i;
713
714         if (!hapd->iface->current_mode)
715                 return 0;
716
717         for (i = 0; i < hapd->iface->current_mode->num_channels; i++) {
718                 struct hostapd_channel_data *ch =
719                         &hapd->iface->current_mode->channels[i];
720                 if (ch->freq == freq)
721                         return ch->chan;
722         }
723
724         return 0;
725 }