Merge branch 'vendor/LIBEDIT'
[dragonfly.git] / contrib / hostapd / 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 "hw_features.h"
21 #include "driver.h"
22 #include "config.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 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);
92                 }
93         }
94
95         return ret;
96 }
97
98
99 static int hostapd_prepare_rates(struct hostapd_data *hapd,
100                                  struct hostapd_hw_modes *mode)
101 {
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;
107
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;
122         }
123
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");
128         }
129
130         os_free(hapd->iface->current_rates);
131         hapd->iface->num_rates = 0;
132
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;
139         }
140
141         for (i = 0; i < mode->num_rates; i++) {
142                 struct hostapd_rate_data *rate;
143
144                 if (hapd->iconf->supported_rates &&
145                     !hostapd_rate_found(hapd->iconf->supported_rates,
146                                         mode->rates[i].rate))
147                         continue;
148
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++;
160         }
161
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;
167         }
168
169         return 0;
170 }
171
172
173 #ifdef CONFIG_IEEE80211N
174 static int ieee80211n_allowed_ht40_channel_pair(struct hostapd_iface *iface)
175 {
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;
180
181         if (!iface->conf->secondary_channel)
182                 return 1; /* HT40 not used */
183
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);
188
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;
199                 }
200         }
201         if (!ok) {
202                 wpa_printf(MSG_ERROR, "HT40 secondary channel %d not allowed",
203                            sec_chan);
204                 return 0;
205         }
206
207         /*
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).
212          */
213         if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A)
214                 return 1;
215
216         if (iface->conf->secondary_channel > 0)
217                 first = iface->conf->channel;
218         else
219                 first = sec_chan;
220
221         ok = 0;
222         for (k = 0; k < sizeof(allowed) / sizeof(allowed[0]); k++) {
223                 if (first == allowed[k]) {
224                         ok = 1;
225                         break;
226                 }
227         }
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;
233         }
234
235         return 1;
236 }
237
238
239 static int ieee80211n_supported_ht_capab(struct hostapd_iface *iface)
240 {
241         u16 hw = iface->current_mode->ht_capab;
242         u16 conf = iface->conf->ht_capab;
243
244         if (!iface->conf->ieee80211n)
245                 return 1;
246
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;
252         }
253
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;
259         }
260
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;
266         }
267
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;
273         }
274
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;
280         }
281
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;
287         }
288
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;
293         }
294
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;
300         }
301
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;
307         }
308
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;
314         }
315
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;
321         }
322
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;
327         }
328
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;
334         }
335
336         return 1;
337 }
338 #endif /* CONFIG_IEEE80211N */
339
340
341 /**
342  * hostapd_select_hw_mode - Select the hardware mode
343  * @iface: Pointer to interface data.
344  * Returns: 0 on success, -1 on failure
345  *
346  * Sets up the hardware mode, channel, rates, and passive scanning
347  * based on the configuration.
348  */
349 int hostapd_select_hw_mode(struct hostapd_iface *iface)
350 {
351         int i, j, ok, ret;
352
353         if (iface->num_hw_features < 1)
354                 return -1;
355
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;
362                 }
363         }
364
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;
373         }
374
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;
383                 }
384         }
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;
391         }
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;
402         }
403
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;
409         }
410
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 */
417
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;
424         }
425
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));
438                 }
439                 ret = 0;
440         }
441
442         return ret;
443 }
444
445
446 const char * hostapd_hw_mode_txt(int mode)
447 {
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";
457         }
458 }
459
460
461 int hostapd_hw_get_freq(struct hostapd_data *hapd, int chan)
462 {
463         int i;
464
465         if (!hapd->iface->current_mode)
466                 return 0;
467
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;
473         }
474
475         return 0;
476 }
477
478
479 int hostapd_hw_get_channel(struct hostapd_data *hapd, int freq)
480 {
481         int i;
482
483         if (!hapd->iface->current_mode)
484                 return 0;
485
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;
491         }
492
493         return 0;
494 }