Split hostapd/driver.h into two files
[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 "hw_features.h"
21 #include "driver_i.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 #endif /* CONFIG_IEEE80211N */
238
239
240 /**
241  * hostapd_select_hw_mode - Select the hardware mode
242  * @iface: Pointer to interface data.
243  * Returns: 0 on success, -1 on failure
244  *
245  * Sets up the hardware mode, channel, rates, and passive scanning
246  * based on the configuration.
247  */
248 int hostapd_select_hw_mode(struct hostapd_iface *iface)
249 {
250         int i, j, ok, ret;
251
252         if (iface->num_hw_features < 1)
253                 return -1;
254
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;
260                         break;
261                 }
262         }
263
264         if (iface->current_mode == NULL) {
265                 wpa_printf(MSG_ERROR, "Hardware does not support configured "
266                            "mode");
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);
271                 return -1;
272         }
273
274         ok = 0;
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)) {
280                         ok = 1;
281                         break;
282                 }
283         }
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;
294         }
295
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");
300                 return -1;
301         }
302
303 #ifdef CONFIG_IEEE80211N
304         if (!ieee80211n_allowed_ht40_channel_pair(iface))
305                 return -1;
306 #endif /* CONFIG_IEEE80211N */
307
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.");
313                 return -1;
314         }
315
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,
320                                    NULL, NULL);
321         if (ret) {
322                 if (ret == -1) {
323                         wpa_printf(MSG_DEBUG, "Passive scanning not "
324                                    "supported");
325                 } else {
326                         wpa_printf(MSG_ERROR, "Could not set passive "
327                                    "scanning: %s", strerror(ret));
328                 }
329                 ret = 0;
330         }
331
332         return ret;
333 }
334
335
336 const char * hostapd_hw_mode_txt(int mode)
337 {
338         switch (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";
345         default:
346                 return "UNKNOWN";
347         }
348 }
349
350
351 int hostapd_hw_get_freq(struct hostapd_data *hapd, int chan)
352 {
353         int i;
354
355         if (!hapd->iface->current_mode)
356                 return 0;
357
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)
362                         return ch->freq;
363         }
364
365         return 0;
366 }
367
368
369 int hostapd_hw_get_channel(struct hostapd_data *hapd, int freq)
370 {
371         int i;
372
373         if (!hapd->iface->current_mode)
374                 return 0;
375
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)
380                         return ch->chan;
381         }
382
383         return 0;
384 }