WPS: Use WEP key index 1..4 instead of 0..3 when configuring AP
[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
238
239 static void ieee80211n_switch_pri_sec(struct hostapd_iface *iface)
240 {
241         if (iface->conf->secondary_channel > 0) {
242                 iface->conf->channel += 4;
243                 iface->conf->secondary_channel = -1;
244         } else {
245                 iface->conf->channel -= 4;
246                 iface->conf->secondary_channel = 1;
247         }
248 }
249
250
251 static int ieee80211n_check_40mhz_5g(struct hostapd_iface *iface)
252 {
253         int pri_chan, sec_chan, pri_freq, sec_freq, pri_bss, sec_bss;
254         const struct hostapd_neighbor_bss *n;
255         size_t i, num;
256         int match;
257
258         pri_chan = iface->conf->channel;
259         sec_chan = iface->conf->secondary_channel * 4;
260         pri_freq = hostapd_hw_get_freq(iface->bss[0], pri_chan);
261         if (iface->conf->secondary_channel > 0)
262                 sec_freq = pri_freq + 20;
263         else
264                 sec_freq = pri_freq - 20;
265
266         n = hostapd_driver_get_neighbor_bss(iface->bss[0], &num);
267
268         /*
269          * Switch PRI/SEC channels if Beacons were detected on selected SEC
270          * channel, but not on selected PRI channel.
271          */
272         pri_bss = sec_bss = 0;
273         for (i = 0; n && i < num; i++) {
274                 if (n[i].freq == pri_freq)
275                         pri_bss++;
276                 else if (n[i].freq == sec_freq)
277                         sec_bss++;
278         }
279         if (sec_bss && !pri_bss) {
280                 wpa_printf(MSG_INFO, "Switch own primary and secondary "
281                            "channel to get secondary channel with no Beacons "
282                            "from other BSSes");
283                 ieee80211n_switch_pri_sec(iface);
284         }
285
286         /*
287          * Match PRI/SEC channel with any existing HT40 BSS on the same
288          * channels that we are about to use (if already mixed order in
289          * existing BSSes, use own preference).
290          */
291         match = 0;
292         for (i = 0; n && i < num; i++) {
293                 if (pri_chan == n[i].pri_chan &&
294                     sec_chan == n[i].sec_chan) {
295                         match = 1;
296                         break;
297                 }
298         }
299         if (!match) {
300                 for (i = 0; n && i < num; i++) {
301                         if (pri_chan == n[i].sec_chan &&
302                             sec_chan == n[i].pri_chan) {
303                                 wpa_printf(MSG_INFO, "Switch own primary and "
304                                            "secondary channel due to BSS "
305                                            "overlap with " MACSTR,
306                                            MAC2STR(n[i].bssid));
307                                 ieee80211n_switch_pri_sec(iface);
308                                 break;
309                         }
310                 }
311         }
312
313         return 1;
314 }
315
316
317 static int ieee80211n_check_40mhz_2g4(struct hostapd_iface *iface)
318 {
319         int pri_freq, sec_freq;
320         int affected_start, affected_end;
321         const struct hostapd_neighbor_bss *n;
322         size_t i, num;
323
324         pri_freq = hostapd_hw_get_freq(iface->bss[0], iface->conf->channel);
325         if (iface->conf->secondary_channel > 0)
326                 sec_freq = pri_freq + 20;
327         else
328                 sec_freq = pri_freq - 20;
329         affected_start = (pri_freq + sec_freq) / 2 - 25;
330         affected_end = (pri_freq + sec_freq) / 2 + 25;
331         wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
332                    affected_start, affected_end);
333         n = hostapd_driver_get_neighbor_bss(iface->bss[0], &num);
334         for (i = 0; n && i < num; i++) {
335                 int pri = n[i].freq;
336                 int sec = pri;
337                 if (n[i].sec_chan) {
338                         if (n[i].sec_chan < n[i].pri_chan)
339                                 sec = pri - 20;
340                         else
341                                 sec = pri + 20;
342                 }
343
344                 if ((pri < affected_start || pri > affected_end) &&
345                     (sec < affected_start || sec > affected_end))
346                         continue; /* not within affected channel range */
347
348                 if (n[i].sec_chan) {
349                         if (pri_freq != pri || sec_freq != sec) {
350                                 wpa_printf(MSG_DEBUG, "40 MHz pri/sec "
351                                            "mismatch with BSS " MACSTR
352                                            " <%d,%d> (chan=%d%c) vs. <%d,%d>",
353                                            MAC2STR(n[i].bssid),
354                                            pri, sec, n[i].pri_chan,
355                                            sec > pri ? '+' : '-',
356                                            pri_freq, sec_freq);
357                                 return 0;
358                         }
359                 }
360
361                 /* TODO: 40 MHz intolerant */
362         }
363
364         return 1;
365 }
366
367
368 static void ieee80211n_check_40mhz(struct hostapd_iface *iface)
369 {
370         int oper40;
371
372         if (!iface->conf->secondary_channel)
373                 return; /* HT40 not used */
374
375         /* Check list of neighboring BSSes (from scan) to see whether 40 MHz is
376          * allowed per IEEE 802.11n/D7.0, 11.14.3.2 */
377
378         if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A)
379                 oper40 = ieee80211n_check_40mhz_5g(iface);
380         else
381                 oper40 = ieee80211n_check_40mhz_2g4(iface);
382
383         if (!oper40) {
384                 wpa_printf(MSG_INFO, "20/40 MHz operation not permitted on "
385                            "channel pri=%d sec=%d based on overlapping BSSes",
386                            iface->conf->channel,
387                            iface->conf->channel +
388                            iface->conf->secondary_channel * 4);
389                 iface->conf->secondary_channel = 0;
390                 iface->conf->ht_capab &= ~HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
391         }
392 }
393
394
395 static int ieee80211n_supported_ht_capab(struct hostapd_iface *iface)
396 {
397         u16 hw = iface->current_mode->ht_capab;
398         u16 conf = iface->conf->ht_capab;
399
400         if (!iface->conf->ieee80211n)
401                 return 1;
402
403         if ((conf & HT_CAP_INFO_LDPC_CODING_CAP) &&
404             !(hw & HT_CAP_INFO_LDPC_CODING_CAP)) {
405                 wpa_printf(MSG_ERROR, "Driver does not support configured "
406                            "HT capability [LDPC]");
407                 return 0;
408         }
409
410         if ((conf & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) &&
411             !(hw & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
412                 wpa_printf(MSG_ERROR, "Driver does not support configured "
413                            "HT capability [HT40*]");
414                 return 0;
415         }
416
417         if ((conf & HT_CAP_INFO_SMPS_MASK) != (hw & HT_CAP_INFO_SMPS_MASK) &&
418             (conf & HT_CAP_INFO_SMPS_MASK) != HT_CAP_INFO_SMPS_DISABLED) {
419                 wpa_printf(MSG_ERROR, "Driver does not support configured "
420                            "HT capability [SMPS-*]");
421                 return 0;
422         }
423
424         if ((conf & HT_CAP_INFO_GREEN_FIELD) &&
425             !(hw & HT_CAP_INFO_GREEN_FIELD)) {
426                 wpa_printf(MSG_ERROR, "Driver does not support configured "
427                            "HT capability [GF]");
428                 return 0;
429         }
430
431         if ((conf & HT_CAP_INFO_SHORT_GI20MHZ) &&
432             !(hw & HT_CAP_INFO_SHORT_GI20MHZ)) {
433                 wpa_printf(MSG_ERROR, "Driver does not support configured "
434                            "HT capability [SHORT-GI-20]");
435                 return 0;
436         }
437
438         if ((conf & HT_CAP_INFO_SHORT_GI40MHZ) &&
439             !(hw & HT_CAP_INFO_SHORT_GI40MHZ)) {
440                 wpa_printf(MSG_ERROR, "Driver does not support configured "
441                            "HT capability [SHORT-GI-40]");
442                 return 0;
443         }
444
445         if ((conf & HT_CAP_INFO_TX_STBC) && !(hw & HT_CAP_INFO_TX_STBC)) {
446                 wpa_printf(MSG_ERROR, "Driver does not support configured "
447                            "HT capability [TX-STBC]");
448                 return 0;
449         }
450
451         if ((conf & HT_CAP_INFO_RX_STBC_MASK) >
452             (hw & HT_CAP_INFO_RX_STBC_MASK)) {
453                 wpa_printf(MSG_ERROR, "Driver does not support configured "
454                            "HT capability [RX-STBC*]");
455                 return 0;
456         }
457
458         if ((conf & HT_CAP_INFO_DELAYED_BA) &&
459             !(hw & HT_CAP_INFO_DELAYED_BA)) {
460                 wpa_printf(MSG_ERROR, "Driver does not support configured "
461                            "HT capability [DELAYED-BA]");
462                 return 0;
463         }
464
465         if ((conf & HT_CAP_INFO_MAX_AMSDU_SIZE) &&
466             !(hw & HT_CAP_INFO_MAX_AMSDU_SIZE)) {
467                 wpa_printf(MSG_ERROR, "Driver does not support configured "
468                            "HT capability [MAX-AMSDU-7935]");
469                 return 0;
470         }
471
472         if ((conf & HT_CAP_INFO_DSSS_CCK40MHZ) &&
473             !(hw & HT_CAP_INFO_DSSS_CCK40MHZ)) {
474                 wpa_printf(MSG_ERROR, "Driver does not support configured "
475                            "HT capability [DSSS_CCK-40]");
476                 return 0;
477         }
478
479         if ((conf & HT_CAP_INFO_PSMP_SUPP) && !(hw & HT_CAP_INFO_PSMP_SUPP)) {
480                 wpa_printf(MSG_ERROR, "Driver does not support configured "
481                            "HT capability [PSMP]");
482                 return 0;
483         }
484
485         if ((conf & HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT) &&
486             !(hw & HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT)) {
487                 wpa_printf(MSG_ERROR, "Driver does not support configured "
488                            "HT capability [LSIG-TXOP-PROT]");
489                 return 0;
490         }
491
492         return 1;
493 }
494 #endif /* CONFIG_IEEE80211N */
495
496
497 /**
498  * hostapd_select_hw_mode - Select the hardware mode
499  * @iface: Pointer to interface data.
500  * Returns: 0 on success, -1 on failure
501  *
502  * Sets up the hardware mode, channel, rates, and passive scanning
503  * based on the configuration.
504  */
505 int hostapd_select_hw_mode(struct hostapd_iface *iface)
506 {
507         int i, j, ok, ret;
508
509         if (iface->num_hw_features < 1)
510                 return -1;
511
512         iface->current_mode = NULL;
513         for (i = 0; i < iface->num_hw_features; i++) {
514                 struct hostapd_hw_modes *mode = &iface->hw_features[i];
515                 if (mode->mode == (int) iface->conf->hw_mode) {
516                         iface->current_mode = mode;
517                         break;
518                 }
519         }
520
521         if (iface->current_mode == NULL) {
522                 wpa_printf(MSG_ERROR, "Hardware does not support configured "
523                            "mode");
524                 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
525                                HOSTAPD_LEVEL_WARNING,
526                                "Hardware does not support configured mode "
527                                "(%d)", (int) iface->conf->hw_mode);
528                 return -1;
529         }
530
531         ok = 0;
532         for (j = 0; j < iface->current_mode->num_channels; j++) {
533                 struct hostapd_channel_data *chan =
534                         &iface->current_mode->channels[j];
535                 if (!(chan->flag & HOSTAPD_CHAN_DISABLED) &&
536                     (chan->chan == iface->conf->channel)) {
537                         ok = 1;
538                         break;
539                 }
540         }
541         if (ok == 0 && iface->conf->channel != 0) {
542                 hostapd_logger(iface->bss[0], NULL,
543                                HOSTAPD_MODULE_IEEE80211,
544                                HOSTAPD_LEVEL_WARNING,
545                                "Configured channel (%d) not found from the "
546                                "channel list of current mode (%d) %s",
547                                iface->conf->channel,
548                                iface->current_mode->mode,
549                                hostapd_hw_mode_txt(iface->current_mode->mode));
550                 iface->current_mode = NULL;
551         }
552
553         if (iface->current_mode == NULL) {
554                 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
555                                HOSTAPD_LEVEL_WARNING,
556                                "Hardware does not support configured channel");
557                 return -1;
558         }
559
560 #ifdef CONFIG_IEEE80211N
561         ieee80211n_check_40mhz(iface);
562         if (!ieee80211n_allowed_ht40_channel_pair(iface))
563                 return -1;
564         if (!ieee80211n_supported_ht_capab(iface))
565                 return -1;
566 #endif /* CONFIG_IEEE80211N */
567
568         if (hostapd_prepare_rates(iface->bss[0], iface->current_mode)) {
569                 wpa_printf(MSG_ERROR, "Failed to prepare rates table.");
570                 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
571                                            HOSTAPD_LEVEL_WARNING,
572                                            "Failed to prepare rates table.");
573                 return -1;
574         }
575
576         ret = hostapd_passive_scan(iface->bss[0], 0,
577                                    iface->conf->passive_scan_mode,
578                                    iface->conf->passive_scan_interval,
579                                    iface->conf->passive_scan_listen,
580                                    NULL, NULL);
581         if (ret) {
582                 if (ret == -1) {
583                         wpa_printf(MSG_DEBUG, "Passive scanning not "
584                                    "supported");
585                 } else {
586                         wpa_printf(MSG_ERROR, "Could not set passive "
587                                    "scanning: %s", strerror(ret));
588                 }
589                 ret = 0;
590         }
591
592         return ret;
593 }
594
595
596 const char * hostapd_hw_mode_txt(int mode)
597 {
598         switch (mode) {
599         case HOSTAPD_MODE_IEEE80211A:
600                 return "IEEE 802.11a";
601         case HOSTAPD_MODE_IEEE80211B:
602                 return "IEEE 802.11b";
603         case HOSTAPD_MODE_IEEE80211G:
604                 return "IEEE 802.11g";
605         default:
606                 return "UNKNOWN";
607         }
608 }
609
610
611 int hostapd_hw_get_freq(struct hostapd_data *hapd, int chan)
612 {
613         int i;
614
615         if (!hapd->iface->current_mode)
616                 return 0;
617
618         for (i = 0; i < hapd->iface->current_mode->num_channels; i++) {
619                 struct hostapd_channel_data *ch =
620                         &hapd->iface->current_mode->channels[i];
621                 if (ch->chan == chan)
622                         return ch->freq;
623         }
624
625         return 0;
626 }
627
628
629 int hostapd_hw_get_channel(struct hostapd_data *hapd, int freq)
630 {
631         int i;
632
633         if (!hapd->iface->current_mode)
634                 return 0;
635
636         for (i = 0; i < hapd->iface->current_mode->num_channels; i++) {
637                 struct hostapd_channel_data *ch =
638                         &hapd->iface->current_mode->channels[i];
639                 if (ch->freq == freq)
640                         return ch->chan;
641         }
642
643         return 0;
644 }