LCOV - code coverage report
Current view: top level - src/ap - hw_features.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1475438200 Lines: 370 475 77.9 %
Date: 2016-10-02 Functions: 27 28 96.4 %

          Line data    Source code
       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-2012, Jouni Malinen <j@w1.fi>
       6             :  *
       7             :  * This software may be distributed under the terms of the BSD license.
       8             :  * See README for more details.
       9             :  */
      10             : 
      11             : #include "utils/includes.h"
      12             : 
      13             : #include "utils/common.h"
      14             : #include "utils/eloop.h"
      15             : #include "common/ieee802_11_defs.h"
      16             : #include "common/ieee802_11_common.h"
      17             : #include "common/wpa_ctrl.h"
      18             : #include "common/hw_features_common.h"
      19             : #include "hostapd.h"
      20             : #include "ap_config.h"
      21             : #include "ap_drv_ops.h"
      22             : #include "acs.h"
      23             : #include "ieee802_11.h"
      24             : #include "beacon.h"
      25             : #include "hw_features.h"
      26             : 
      27             : 
      28        5526 : void hostapd_free_hw_features(struct hostapd_hw_modes *hw_features,
      29             :                               size_t num_hw_features)
      30             : {
      31             :         size_t i;
      32             : 
      33        5526 :         if (hw_features == NULL)
      34        8434 :                 return;
      35             : 
      36       10469 :         for (i = 0; i < num_hw_features; i++) {
      37        7851 :                 os_free(hw_features[i].channels);
      38        7851 :                 os_free(hw_features[i].rates);
      39             :         }
      40             : 
      41        2618 :         os_free(hw_features);
      42             : }
      43             : 
      44             : 
      45             : #ifndef CONFIG_NO_STDOUT_DEBUG
      46         244 : static char * dfs_info(struct hostapd_channel_data *chan)
      47             : {
      48             :         static char info[256];
      49             :         char *state;
      50             : 
      51         244 :         switch (chan->flag & HOSTAPD_CHAN_DFS_MASK) {
      52             :         case HOSTAPD_CHAN_DFS_UNKNOWN:
      53           0 :                 state = "unknown";
      54           0 :                 break;
      55             :         case HOSTAPD_CHAN_DFS_USABLE:
      56         244 :                 state = "usable";
      57         244 :                 break;
      58             :         case HOSTAPD_CHAN_DFS_UNAVAILABLE:
      59           0 :                 state = "unavailable";
      60           0 :                 break;
      61             :         case HOSTAPD_CHAN_DFS_AVAILABLE:
      62           0 :                 state = "available";
      63           0 :                 break;
      64             :         default:
      65           0 :                 return "";
      66             :         }
      67         244 :         os_snprintf(info, sizeof(info), " (DFS state = %s)", state);
      68         244 :         info[sizeof(info) - 1] = '\0';
      69             : 
      70         244 :         return info;
      71             : }
      72             : #endif /* CONFIG_NO_STDOUT_DEBUG */
      73             : 
      74             : 
      75        2630 : int hostapd_get_hw_features(struct hostapd_iface *iface)
      76             : {
      77        2630 :         struct hostapd_data *hapd = iface->bss[0];
      78             :         int i, j;
      79             :         u16 num_modes, flags;
      80             :         struct hostapd_hw_modes *modes;
      81             : 
      82        2630 :         if (hostapd_drv_none(hapd))
      83          12 :                 return -1;
      84        2618 :         modes = hostapd_get_hw_feature_data(hapd, &num_modes, &flags);
      85        2618 :         if (modes == NULL) {
      86           0 :                 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
      87             :                                HOSTAPD_LEVEL_DEBUG,
      88             :                                "Fetching hardware channel/rate support not "
      89             :                                "supported.");
      90           0 :                 return -1;
      91             :         }
      92             : 
      93        2618 :         iface->hw_flags = flags;
      94             : 
      95        2618 :         hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
      96        2618 :         iface->hw_features = modes;
      97        2618 :         iface->num_hw_features = num_modes;
      98             : 
      99       10469 :         for (i = 0; i < num_modes; i++) {
     100        7851 :                 struct hostapd_hw_modes *feature = &modes[i];
     101        7902 :                 int dfs_enabled = hapd->iconf->ieee80211h &&
     102          51 :                         (iface->drv_flags & WPA_DRIVER_FLAGS_RADAR);
     103             : 
     104             :                 /* set flag for channels we can use in current regulatory
     105             :                  * domain */
     106      143945 :                 for (j = 0; j < feature->num_channels; j++) {
     107      136094 :                         int dfs = 0;
     108             : 
     109             :                         /*
     110             :                          * Disable all channels that are marked not to allow
     111             :                          * to initiate radiation (a.k.a. passive scan and no
     112             :                          * IBSS).
     113             :                          * Use radar channels only if the driver supports DFS.
     114             :                          */
     115      136094 :                         if ((feature->channels[j].flag &
     116       39204 :                              HOSTAPD_CHAN_RADAR) && dfs_enabled) {
     117         244 :                                 dfs = 1;
     118      135850 :                         } else if (((feature->channels[j].flag &
     119       38960 :                                      HOSTAPD_CHAN_RADAR) &&
     120       38960 :                                     !(iface->drv_flags &
     121       96890 :                                       WPA_DRIVER_FLAGS_DFS_OFFLOAD)) ||
     122       96890 :                                    (feature->channels[j].flag &
     123             :                                     HOSTAPD_CHAN_NO_IR)) {
     124       74415 :                                 feature->channels[j].flag |=
     125             :                                         HOSTAPD_CHAN_DISABLED;
     126             :                         }
     127             : 
     128      136094 :                         if (feature->channels[j].flag & HOSTAPD_CHAN_DISABLED)
     129       74482 :                                 continue;
     130             : 
     131      308304 :                         wpa_printf(MSG_MSGDUMP, "Allowed channel: mode=%d "
     132             :                                    "chan=%d freq=%d MHz max_tx_power=%d dBm%s",
     133       61612 :                                    feature->mode,
     134       61612 :                                    feature->channels[j].chan,
     135       61612 :                                    feature->channels[j].freq,
     136       61612 :                                    feature->channels[j].max_tx_power,
     137         244 :                                    dfs ? dfs_info(&feature->channels[j]) : "");
     138             :                 }
     139             :         }
     140             : 
     141        2618 :         return 0;
     142             : }
     143             : 
     144             : 
     145        2604 : int hostapd_prepare_rates(struct hostapd_iface *iface,
     146             :                           struct hostapd_hw_modes *mode)
     147             : {
     148        2604 :         int i, num_basic_rates = 0;
     149        2604 :         int basic_rates_a[] = { 60, 120, 240, -1 };
     150        2604 :         int basic_rates_b[] = { 10, 20, -1 };
     151        2604 :         int basic_rates_g[] = { 10, 20, 55, 110, -1 };
     152             :         int *basic_rates;
     153             : 
     154        2604 :         if (iface->conf->basic_rates)
     155         557 :                 basic_rates = iface->conf->basic_rates;
     156        2047 :         else switch (mode->mode) {
     157             :         case HOSTAPD_MODE_IEEE80211A:
     158         232 :                 basic_rates = basic_rates_a;
     159         232 :                 break;
     160             :         case HOSTAPD_MODE_IEEE80211B:
     161           9 :                 basic_rates = basic_rates_b;
     162           9 :                 break;
     163             :         case HOSTAPD_MODE_IEEE80211G:
     164        1806 :                 basic_rates = basic_rates_g;
     165        1806 :                 break;
     166             :         case HOSTAPD_MODE_IEEE80211AD:
     167           0 :                 return 0; /* No basic rates for 11ad */
     168             :         default:
     169           0 :                 return -1;
     170             :         }
     171             : 
     172        2604 :         i = 0;
     173       15498 :         while (basic_rates[i] >= 0)
     174       10290 :                 i++;
     175        2604 :         if (i)
     176        2604 :                 i++; /* -1 termination */
     177        2604 :         os_free(iface->basic_rates);
     178        2604 :         iface->basic_rates = os_malloc(i * sizeof(int));
     179        2604 :         if (iface->basic_rates)
     180        2603 :                 os_memcpy(iface->basic_rates, basic_rates, i * sizeof(int));
     181             : 
     182        2604 :         os_free(iface->current_rates);
     183        2604 :         iface->num_rates = 0;
     184             : 
     185        2604 :         iface->current_rates =
     186        2604 :                 os_calloc(mode->num_rates, sizeof(struct hostapd_rate_data));
     187        2604 :         if (!iface->current_rates) {
     188           1 :                 wpa_printf(MSG_ERROR, "Failed to allocate memory for rate "
     189             :                            "table.");
     190           1 :                 return -1;
     191             :         }
     192             : 
     193       32839 :         for (i = 0; i < mode->num_rates; i++) {
     194             :                 struct hostapd_rate_data *rate;
     195             : 
     196       34888 :                 if (iface->conf->supported_rates &&
     197        4652 :                     !hostapd_rate_found(iface->conf->supported_rates,
     198        4652 :                                         mode->rates[i]))
     199        1552 :                         continue;
     200             : 
     201       28684 :                 rate = &iface->current_rates[iface->num_rates];
     202       28684 :                 rate->rate = mode->rates[i];
     203       28684 :                 if (hostapd_rate_found(basic_rates, rate->rate)) {
     204       10279 :                         rate->flags |= HOSTAPD_RATE_BASIC;
     205       10279 :                         num_basic_rates++;
     206             :                 }
     207       28684 :                 wpa_printf(MSG_DEBUG, "RATE[%d] rate=%d flags=0x%x",
     208             :                            iface->num_rates, rate->rate, rate->flags);
     209       28684 :                 iface->num_rates++;
     210             :         }
     211             : 
     212        2604 :         if ((iface->num_rates == 0 || num_basic_rates == 0) &&
     213           2 :             (!iface->conf->ieee80211n || !iface->conf->require_ht)) {
     214           0 :                 wpa_printf(MSG_ERROR, "No rates remaining in supported/basic "
     215             :                            "rate sets (%d,%d).",
     216             :                            iface->num_rates, num_basic_rates);
     217           0 :                 return -1;
     218             :         }
     219             : 
     220        2603 :         return 0;
     221             : }
     222             : 
     223             : 
     224             : #ifdef CONFIG_IEEE80211N
     225        2418 : static int ieee80211n_allowed_ht40_channel_pair(struct hostapd_iface *iface)
     226             : {
     227             :         int pri_chan, sec_chan;
     228             : 
     229        2418 :         if (!iface->conf->secondary_channel)
     230        2352 :                 return 1; /* HT40 not used */
     231             : 
     232          66 :         pri_chan = iface->conf->channel;
     233          66 :         sec_chan = pri_chan + iface->conf->secondary_channel * 4;
     234             : 
     235          66 :         return allowed_ht40_channel_pair(iface->current_mode, pri_chan,
     236             :                                          sec_chan);
     237             : }
     238             : 
     239             : 
     240           2 : static void ieee80211n_switch_pri_sec(struct hostapd_iface *iface)
     241             : {
     242           2 :         if (iface->conf->secondary_channel > 0) {
     243           0 :                 iface->conf->channel += 4;
     244           0 :                 iface->conf->secondary_channel = -1;
     245             :         } else {
     246           2 :                 iface->conf->channel -= 4;
     247           2 :                 iface->conf->secondary_channel = 1;
     248             :         }
     249           2 : }
     250             : 
     251             : 
     252          46 : static int ieee80211n_check_40mhz_5g(struct hostapd_iface *iface,
     253             :                                      struct wpa_scan_results *scan_res)
     254             : {
     255             :         int pri_chan, sec_chan;
     256             :         int res;
     257             : 
     258          46 :         pri_chan = iface->conf->channel;
     259          46 :         sec_chan = pri_chan + iface->conf->secondary_channel * 4;
     260             : 
     261          46 :         res = check_40mhz_5g(iface->current_mode, scan_res, pri_chan, sec_chan);
     262             : 
     263          46 :         if (res == 2) {
     264           2 :                 if (iface->conf->no_pri_sec_switch) {
     265           0 :                         wpa_printf(MSG_DEBUG,
     266             :                                    "Cannot switch PRI/SEC channels due to local constraint");
     267             :                 } else {
     268           2 :                         ieee80211n_switch_pri_sec(iface);
     269             :                 }
     270             :         }
     271             : 
     272          46 :         return !!res;
     273             : }
     274             : 
     275             : 
     276          25 : static int ieee80211n_check_40mhz_2g4(struct hostapd_iface *iface,
     277             :                                       struct wpa_scan_results *scan_res)
     278             : {
     279             :         int pri_chan, sec_chan;
     280             : 
     281          25 :         pri_chan = iface->conf->channel;
     282          25 :         sec_chan = pri_chan + iface->conf->secondary_channel * 4;
     283             : 
     284          25 :         return check_40mhz_2g4(iface->current_mode, scan_res, pri_chan,
     285             :                                sec_chan);
     286             : }
     287             : 
     288             : 
     289          71 : static void ieee80211n_check_scan(struct hostapd_iface *iface)
     290             : {
     291             :         struct wpa_scan_results *scan_res;
     292             :         int oper40;
     293             :         int res;
     294             : 
     295             :         /* Check list of neighboring BSSes (from scan) to see whether 40 MHz is
     296             :          * allowed per IEEE Std 802.11-2012, 10.15.3.2 */
     297             : 
     298          71 :         iface->scan_cb = NULL;
     299             : 
     300          71 :         scan_res = hostapd_driver_get_scan_results(iface->bss[0]);
     301          71 :         if (scan_res == NULL) {
     302           0 :                 hostapd_setup_interface_complete(iface, 1);
     303          71 :                 return;
     304             :         }
     305             : 
     306          71 :         if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A)
     307          46 :                 oper40 = ieee80211n_check_40mhz_5g(iface, scan_res);
     308             :         else
     309          25 :                 oper40 = ieee80211n_check_40mhz_2g4(iface, scan_res);
     310          71 :         wpa_scan_results_free(scan_res);
     311             : 
     312          71 :         iface->secondary_ch = iface->conf->secondary_channel;
     313          71 :         if (!oper40) {
     314          18 :                 wpa_printf(MSG_INFO, "20/40 MHz operation not permitted on "
     315             :                            "channel pri=%d sec=%d based on overlapping BSSes",
     316           6 :                            iface->conf->channel,
     317           6 :                            iface->conf->channel +
     318           6 :                            iface->conf->secondary_channel * 4);
     319           6 :                 iface->conf->secondary_channel = 0;
     320           6 :                 if (iface->drv_flags & WPA_DRIVER_FLAGS_HT_2040_COEX) {
     321             :                         /*
     322             :                          * TODO: Could consider scheduling another scan to check
     323             :                          * if channel width can be changed if no coex reports
     324             :                          * are received from associating stations.
     325             :                          */
     326             :                 }
     327             :         }
     328             : 
     329          71 :         res = ieee80211n_allowed_ht40_channel_pair(iface);
     330          71 :         if (!res) {
     331           1 :                 iface->conf->secondary_channel = 0;
     332           1 :                 res = 1;
     333           1 :                 wpa_printf(MSG_INFO, "Fallback to 20 MHz");
     334             :         }
     335             : 
     336          71 :         hostapd_setup_interface_complete(iface, !res);
     337             : }
     338             : 
     339             : 
     340          28 : static void ieee80211n_scan_channels_2g4(struct hostapd_iface *iface,
     341             :                                          struct wpa_driver_scan_params *params)
     342             : {
     343             :         /* Scan only the affected frequency range */
     344             :         int pri_freq, sec_freq;
     345             :         int affected_start, affected_end;
     346             :         int i, pos;
     347             :         struct hostapd_hw_modes *mode;
     348             : 
     349          28 :         if (iface->current_mode == NULL)
     350           0 :                 return;
     351             : 
     352          28 :         pri_freq = hostapd_hw_get_freq(iface->bss[0], iface->conf->channel);
     353          28 :         if (iface->conf->secondary_channel > 0)
     354          14 :                 sec_freq = pri_freq + 20;
     355             :         else
     356          14 :                 sec_freq = pri_freq - 20;
     357             :         /*
     358             :          * Note: Need to find the PRI channel also in cases where the affected
     359             :          * channel is the SEC channel of a 40 MHz BSS, so need to include the
     360             :          * scanning coverage here to be 40 MHz from the center frequency.
     361             :          */
     362          28 :         affected_start = (pri_freq + sec_freq) / 2 - 40;
     363          28 :         affected_end = (pri_freq + sec_freq) / 2 + 40;
     364          28 :         wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
     365             :                    affected_start, affected_end);
     366             : 
     367          28 :         mode = iface->current_mode;
     368          28 :         params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
     369          28 :         if (params->freqs == NULL)
     370           0 :                 return;
     371          28 :         pos = 0;
     372             : 
     373         420 :         for (i = 0; i < mode->num_channels; i++) {
     374         392 :                 struct hostapd_channel_data *chan = &mode->channels[i];
     375         392 :                 if (chan->flag & HOSTAPD_CHAN_DISABLED)
     376          84 :                         continue;
     377         616 :                 if (chan->freq < affected_start ||
     378         308 :                     chan->freq > affected_end)
     379           0 :                         continue;
     380         308 :                 params->freqs[pos++] = chan->freq;
     381             :         }
     382             : }
     383             : 
     384             : 
     385          46 : static void ieee80211n_scan_channels_5g(struct hostapd_iface *iface,
     386             :                                         struct wpa_driver_scan_params *params)
     387             : {
     388             :         /* Scan only the affected frequency range */
     389             :         int pri_freq;
     390             :         int affected_start, affected_end;
     391             :         int i, pos;
     392             :         struct hostapd_hw_modes *mode;
     393             : 
     394          46 :         if (iface->current_mode == NULL)
     395           0 :                 return;
     396             : 
     397          46 :         pri_freq = hostapd_hw_get_freq(iface->bss[0], iface->conf->channel);
     398          46 :         if (iface->conf->secondary_channel > 0) {
     399          36 :                 affected_start = pri_freq - 10;
     400          36 :                 affected_end = pri_freq + 30;
     401             :         } else {
     402          10 :                 affected_start = pri_freq - 30;
     403          10 :                 affected_end = pri_freq + 10;
     404             :         }
     405          46 :         wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
     406             :                    affected_start, affected_end);
     407             : 
     408          46 :         mode = iface->current_mode;
     409          46 :         params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
     410          46 :         if (params->freqs == NULL)
     411           0 :                 return;
     412          46 :         pos = 0;
     413             : 
     414        1150 :         for (i = 0; i < mode->num_channels; i++) {
     415        1104 :                 struct hostapd_channel_data *chan = &mode->channels[i];
     416        1104 :                 if (chan->flag & HOSTAPD_CHAN_DISABLED)
     417         614 :                         continue;
     418         919 :                 if (chan->freq < affected_start ||
     419         429 :                     chan->freq > affected_end)
     420         398 :                         continue;
     421          92 :                 params->freqs[pos++] = chan->freq;
     422             :         }
     423             : }
     424             : 
     425             : 
     426           0 : static void ap_ht40_scan_retry(void *eloop_data, void *user_data)
     427             : {
     428             : #define HT2040_COEX_SCAN_RETRY 15
     429           0 :         struct hostapd_iface *iface = eloop_data;
     430             :         struct wpa_driver_scan_params params;
     431             :         int ret;
     432             : 
     433           0 :         os_memset(&params, 0, sizeof(params));
     434           0 :         if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
     435           0 :                 ieee80211n_scan_channels_2g4(iface, &params);
     436             :         else
     437           0 :                 ieee80211n_scan_channels_5g(iface, &params);
     438             : 
     439           0 :         ret = hostapd_driver_scan(iface->bss[0], &params);
     440           0 :         iface->num_ht40_scan_tries++;
     441           0 :         os_free(params.freqs);
     442             : 
     443           0 :         if (ret == -EBUSY &&
     444           0 :             iface->num_ht40_scan_tries < HT2040_COEX_SCAN_RETRY) {
     445           0 :                 wpa_printf(MSG_ERROR,
     446             :                            "Failed to request a scan of neighboring BSSes ret=%d (%s) - try to scan again (attempt %d)",
     447             :                            ret, strerror(-ret), iface->num_ht40_scan_tries);
     448           0 :                 eloop_register_timeout(1, 0, ap_ht40_scan_retry, iface, NULL);
     449           0 :                 return;
     450             :         }
     451             : 
     452           0 :         if (ret == 0) {
     453           0 :                 iface->scan_cb = ieee80211n_check_scan;
     454           0 :                 return;
     455             :         }
     456             : 
     457           0 :         wpa_printf(MSG_DEBUG,
     458             :                    "Failed to request a scan in device, bringing up in HT20 mode");
     459           0 :         iface->conf->secondary_channel = 0;
     460           0 :         iface->conf->ht_capab &= ~HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
     461           0 :         hostapd_setup_interface_complete(iface, 0);
     462             : }
     463             : 
     464             : 
     465        5590 : void hostapd_stop_setup_timers(struct hostapd_iface *iface)
     466             : {
     467        5590 :         eloop_cancel_timeout(ap_ht40_scan_retry, iface, NULL);
     468        5590 : }
     469             : 
     470             : 
     471        2421 : static int ieee80211n_check_40mhz(struct hostapd_iface *iface)
     472             : {
     473             :         struct wpa_driver_scan_params params;
     474             :         int ret;
     475             : 
     476             :         /* Check that HT40 is used and PRI / SEC switch is allowed */
     477        2421 :         if (!iface->conf->secondary_channel || iface->conf->no_pri_sec_switch)
     478        2347 :                 return 0;
     479             : 
     480          74 :         hostapd_set_state(iface, HAPD_IFACE_HT_SCAN);
     481          74 :         wpa_printf(MSG_DEBUG, "Scan for neighboring BSSes prior to enabling "
     482             :                    "40 MHz channel");
     483          74 :         os_memset(&params, 0, sizeof(params));
     484          74 :         if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
     485          28 :                 ieee80211n_scan_channels_2g4(iface, &params);
     486             :         else
     487          46 :                 ieee80211n_scan_channels_5g(iface, &params);
     488             : 
     489          74 :         ret = hostapd_driver_scan(iface->bss[0], &params);
     490          74 :         os_free(params.freqs);
     491             : 
     492          74 :         if (ret == -EBUSY) {
     493           0 :                 wpa_printf(MSG_ERROR,
     494             :                            "Failed to request a scan of neighboring BSSes ret=%d (%s) - try to scan again",
     495             :                            ret, strerror(-ret));
     496           0 :                 iface->num_ht40_scan_tries = 1;
     497           0 :                 eloop_cancel_timeout(ap_ht40_scan_retry, iface, NULL);
     498           0 :                 eloop_register_timeout(1, 0, ap_ht40_scan_retry, iface, NULL);
     499           0 :                 return 1;
     500             :         }
     501             : 
     502          74 :         if (ret < 0) {
     503           0 :                 wpa_printf(MSG_ERROR,
     504             :                            "Failed to request a scan of neighboring BSSes ret=%d (%s)",
     505             :                            ret, strerror(-ret));
     506           0 :                 return -1;
     507             :         }
     508             : 
     509          74 :         iface->scan_cb = ieee80211n_check_scan;
     510          74 :         return 1;
     511             : }
     512             : 
     513             : 
     514        2423 : static int ieee80211n_supported_ht_capab(struct hostapd_iface *iface)
     515             : {
     516        2423 :         u16 hw = iface->current_mode->ht_capab;
     517        2423 :         u16 conf = iface->conf->ht_capab;
     518             : 
     519        2424 :         if ((conf & HT_CAP_INFO_LDPC_CODING_CAP) &&
     520           1 :             !(hw & HT_CAP_INFO_LDPC_CODING_CAP)) {
     521           1 :                 wpa_printf(MSG_ERROR, "Driver does not support configured "
     522             :                            "HT capability [LDPC]");
     523           1 :                 return 0;
     524             :         }
     525             : 
     526             :         /*
     527             :          * Driver ACS chosen channel may not be HT40 due to internal driver
     528             :          * restrictions.
     529             :          */
     530        2495 :         if (!iface->conf->acs && (conf & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) &&
     531          73 :             !(hw & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
     532           0 :                 wpa_printf(MSG_ERROR, "Driver does not support configured "
     533             :                            "HT capability [HT40*]");
     534           0 :                 return 0;
     535             :         }
     536             : 
     537        2422 :         switch (conf & HT_CAP_INFO_SMPS_MASK) {
     538             :         case HT_CAP_INFO_SMPS_STATIC:
     539           1 :                 if (!(iface->smps_modes & WPA_DRIVER_SMPS_MODE_STATIC)) {
     540           0 :                         wpa_printf(MSG_ERROR,
     541             :                                    "Driver does not support configured HT capability [SMPS-STATIC]");
     542           0 :                         return 0;
     543             :                 }
     544           1 :                 break;
     545             :         case HT_CAP_INFO_SMPS_DYNAMIC:
     546           1 :                 if (!(iface->smps_modes & WPA_DRIVER_SMPS_MODE_DYNAMIC)) {
     547           0 :                         wpa_printf(MSG_ERROR,
     548             :                                    "Driver does not support configured HT capability [SMPS-DYNAMIC]");
     549           0 :                         return 0;
     550             :                 }
     551           1 :                 break;
     552             :         case HT_CAP_INFO_SMPS_DISABLED:
     553             :         default:
     554        2420 :                 break;
     555             :         }
     556             : 
     557        2859 :         if ((conf & HT_CAP_INFO_GREEN_FIELD) &&
     558         437 :             !(hw & HT_CAP_INFO_GREEN_FIELD)) {
     559           0 :                 wpa_printf(MSG_ERROR, "Driver does not support configured "
     560             :                            "HT capability [GF]");
     561           0 :                 return 0;
     562             :         }
     563             : 
     564        2859 :         if ((conf & HT_CAP_INFO_SHORT_GI20MHZ) &&
     565         437 :             !(hw & HT_CAP_INFO_SHORT_GI20MHZ)) {
     566           0 :                 wpa_printf(MSG_ERROR, "Driver does not support configured "
     567             :                            "HT capability [SHORT-GI-20]");
     568           0 :                 return 0;
     569             :         }
     570             : 
     571        2861 :         if ((conf & HT_CAP_INFO_SHORT_GI40MHZ) &&
     572         439 :             !(hw & HT_CAP_INFO_SHORT_GI40MHZ)) {
     573           0 :                 wpa_printf(MSG_ERROR, "Driver does not support configured "
     574             :                            "HT capability [SHORT-GI-40]");
     575           0 :                 return 0;
     576             :         }
     577             : 
     578        2422 :         if ((conf & HT_CAP_INFO_TX_STBC) && !(hw & HT_CAP_INFO_TX_STBC)) {
     579           0 :                 wpa_printf(MSG_ERROR, "Driver does not support configured "
     580             :                            "HT capability [TX-STBC]");
     581           0 :                 return 0;
     582             :         }
     583             : 
     584        4844 :         if ((conf & HT_CAP_INFO_RX_STBC_MASK) >
     585        2422 :             (hw & HT_CAP_INFO_RX_STBC_MASK)) {
     586           0 :                 wpa_printf(MSG_ERROR, "Driver does not support configured "
     587             :                            "HT capability [RX-STBC*]");
     588           0 :                 return 0;
     589             :         }
     590             : 
     591        2422 :         if ((conf & HT_CAP_INFO_DELAYED_BA) &&
     592           0 :             !(hw & HT_CAP_INFO_DELAYED_BA)) {
     593           0 :                 wpa_printf(MSG_ERROR, "Driver does not support configured "
     594             :                            "HT capability [DELAYED-BA]");
     595           0 :                 return 0;
     596             :         }
     597             : 
     598        2422 :         if ((conf & HT_CAP_INFO_MAX_AMSDU_SIZE) &&
     599           0 :             !(hw & HT_CAP_INFO_MAX_AMSDU_SIZE)) {
     600           0 :                 wpa_printf(MSG_ERROR, "Driver does not support configured "
     601             :                            "HT capability [MAX-AMSDU-7935]");
     602           0 :                 return 0;
     603             :         }
     604             : 
     605        2422 :         if ((conf & HT_CAP_INFO_DSSS_CCK40MHZ) &&
     606           0 :             !(hw & HT_CAP_INFO_DSSS_CCK40MHZ)) {
     607           0 :                 wpa_printf(MSG_ERROR, "Driver does not support configured "
     608             :                            "HT capability [DSSS_CCK-40]");
     609           0 :                 return 0;
     610             :         }
     611             : 
     612        2422 :         if ((conf & HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT) &&
     613           0 :             !(hw & HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT)) {
     614           0 :                 wpa_printf(MSG_ERROR, "Driver does not support configured "
     615             :                            "HT capability [LSIG-TXOP-PROT]");
     616           0 :                 return 0;
     617             :         }
     618             : 
     619        2422 :         return 1;
     620             : }
     621             : 
     622             : 
     623             : #ifdef CONFIG_IEEE80211AC
     624             : 
     625       38736 : static int ieee80211ac_cap_check(u32 hw, u32 conf, u32 cap, const char *name)
     626             : {
     627       38736 :         u32 req_cap = conf & cap;
     628             : 
     629             :         /*
     630             :          * Make sure we support all requested capabilities.
     631             :          * NOTE: We assume that 'cap' represents a capability mask,
     632             :          * not a discrete value.
     633             :          */
     634       38736 :         if ((hw & req_cap) != req_cap) {
     635           0 :                 wpa_printf(MSG_ERROR, "Driver does not support configured VHT capability [%s]",
     636             :                            name);
     637           0 :                 return 0;
     638             :         }
     639       38736 :         return 1;
     640             : }
     641             : 
     642             : 
     643       12106 : static int ieee80211ac_cap_check_max(u32 hw, u32 conf, u32 mask,
     644             :                                      unsigned int shift,
     645             :                                      const char *name)
     646             : {
     647       12106 :         u32 hw_max = hw & mask;
     648       12106 :         u32 conf_val = conf & mask;
     649             : 
     650       12106 :         if (conf_val > hw_max) {
     651           1 :                 wpa_printf(MSG_ERROR, "Configured VHT capability [%s] exceeds max value supported by the driver (%d > %d)",
     652             :                            name, conf_val >> shift, hw_max >> shift);
     653           1 :                 return 0;
     654             :         }
     655       12105 :         return 1;
     656             : }
     657             : 
     658             : 
     659        2422 : static int ieee80211ac_supported_vht_capab(struct hostapd_iface *iface)
     660             : {
     661        2422 :         struct hostapd_hw_modes *mode = iface->current_mode;
     662        2422 :         u32 hw = mode->vht_capab;
     663        2422 :         u32 conf = iface->conf->vht_capab;
     664             : 
     665        2422 :         wpa_printf(MSG_DEBUG, "hw vht capab: 0x%x, conf vht capab: 0x%x",
     666             :                    hw, conf);
     667             : 
     668        4610 :         if (mode->mode == HOSTAPD_MODE_IEEE80211G &&
     669        2189 :             iface->conf->bss[0]->vendor_vht &&
     670           2 :             mode->vht_capab == 0 && iface->hw_features) {
     671             :                 int i;
     672             : 
     673           2 :                 for (i = 0; i < iface->num_hw_features; i++) {
     674           2 :                         if (iface->hw_features[i].mode ==
     675             :                             HOSTAPD_MODE_IEEE80211A) {
     676           1 :                                 mode = &iface->hw_features[i];
     677           1 :                                 hw = mode->vht_capab;
     678           1 :                                 wpa_printf(MSG_DEBUG,
     679             :                                            "update hw vht capab based on 5 GHz band: 0x%x",
     680             :                                            hw);
     681           1 :                                 break;
     682             :                         }
     683             :                 }
     684             :         }
     685             : 
     686             : #define VHT_CAP_CHECK(cap) \
     687             :         do { \
     688             :                 if (!ieee80211ac_cap_check(hw, conf, cap, #cap)) \
     689             :                         return 0; \
     690             :         } while (0)
     691             : 
     692             : #define VHT_CAP_CHECK_MAX(cap) \
     693             :         do { \
     694             :                 if (!ieee80211ac_cap_check_max(hw, conf, cap, cap ## _SHIFT, \
     695             :                                                #cap)) \
     696             :                         return 0; \
     697             :         } while (0)
     698             : 
     699        2422 :         VHT_CAP_CHECK_MAX(VHT_CAP_MAX_MPDU_LENGTH_MASK);
     700        2421 :         VHT_CAP_CHECK(VHT_CAP_SUPP_CHAN_WIDTH_160MHZ);
     701        2421 :         VHT_CAP_CHECK(VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ);
     702        2421 :         VHT_CAP_CHECK(VHT_CAP_RXLDPC);
     703        2421 :         VHT_CAP_CHECK(VHT_CAP_SHORT_GI_80);
     704        2421 :         VHT_CAP_CHECK(VHT_CAP_SHORT_GI_160);
     705        2421 :         VHT_CAP_CHECK(VHT_CAP_TXSTBC);
     706        2421 :         VHT_CAP_CHECK_MAX(VHT_CAP_RXSTBC_MASK);
     707        2421 :         VHT_CAP_CHECK(VHT_CAP_SU_BEAMFORMER_CAPABLE);
     708        2421 :         VHT_CAP_CHECK(VHT_CAP_SU_BEAMFORMEE_CAPABLE);
     709        2421 :         VHT_CAP_CHECK_MAX(VHT_CAP_BEAMFORMEE_STS_MAX);
     710        2421 :         VHT_CAP_CHECK_MAX(VHT_CAP_SOUNDING_DIMENSION_MAX);
     711        2421 :         VHT_CAP_CHECK(VHT_CAP_MU_BEAMFORMER_CAPABLE);
     712        2421 :         VHT_CAP_CHECK(VHT_CAP_MU_BEAMFORMEE_CAPABLE);
     713        2421 :         VHT_CAP_CHECK(VHT_CAP_VHT_TXOP_PS);
     714        2421 :         VHT_CAP_CHECK(VHT_CAP_HTC_VHT);
     715        2421 :         VHT_CAP_CHECK_MAX(VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX);
     716        2421 :         VHT_CAP_CHECK(VHT_CAP_VHT_LINK_ADAPTATION_VHT_UNSOL_MFB);
     717        2421 :         VHT_CAP_CHECK(VHT_CAP_VHT_LINK_ADAPTATION_VHT_MRQ_MFB);
     718        2421 :         VHT_CAP_CHECK(VHT_CAP_RX_ANTENNA_PATTERN);
     719        2421 :         VHT_CAP_CHECK(VHT_CAP_TX_ANTENNA_PATTERN);
     720             : 
     721             : #undef VHT_CAP_CHECK
     722             : #undef VHT_CAP_CHECK_MAX
     723             : 
     724        2421 :         return 1;
     725             : }
     726             : #endif /* CONFIG_IEEE80211AC */
     727             : 
     728             : #endif /* CONFIG_IEEE80211N */
     729             : 
     730             : 
     731        2613 : int hostapd_check_ht_capab(struct hostapd_iface *iface)
     732             : {
     733             : #ifdef CONFIG_IEEE80211N
     734             :         int ret;
     735        2613 :         if (!iface->conf->ieee80211n)
     736         190 :                 return 0;
     737             : 
     738        4840 :         if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211B &&
     739        2645 :             iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G &&
     740         228 :             (iface->conf->ht_capab & HT_CAP_INFO_DSSS_CCK40MHZ)) {
     741           0 :                 wpa_printf(MSG_DEBUG,
     742             :                            "Disable HT capability [DSSS_CCK-40] on 5 GHz band");
     743           0 :                 iface->conf->ht_capab &= ~HT_CAP_INFO_DSSS_CCK40MHZ;
     744             :         }
     745             : 
     746        2423 :         if (!ieee80211n_supported_ht_capab(iface))
     747           1 :                 return -1;
     748             : #ifdef CONFIG_IEEE80211AC
     749        2422 :         if (!ieee80211ac_supported_vht_capab(iface))
     750           1 :                 return -1;
     751             : #endif /* CONFIG_IEEE80211AC */
     752        2421 :         ret = ieee80211n_check_40mhz(iface);
     753        2421 :         if (ret)
     754          74 :                 return ret;
     755        2347 :         if (!ieee80211n_allowed_ht40_channel_pair(iface))
     756           0 :                 return -1;
     757             : #endif /* CONFIG_IEEE80211N */
     758             : 
     759        2347 :         return 0;
     760             : }
     761             : 
     762             : 
     763        2700 : static int hostapd_is_usable_chan(struct hostapd_iface *iface,
     764             :                                   int channel, int primary)
     765             : {
     766             :         int i;
     767             :         struct hostapd_channel_data *chan;
     768             : 
     769        2700 :         if (!iface->current_mode)
     770           0 :                 return 0;
     771             : 
     772        5180 :         for (i = 0; i < iface->current_mode->num_channels; i++) {
     773        5177 :                 chan = &iface->current_mode->channels[i];
     774        5177 :                 if (chan->chan != channel)
     775        2479 :                         continue;
     776             : 
     777        2698 :                 if (!(chan->flag & HOSTAPD_CHAN_DISABLED))
     778        2697 :                         return 1;
     779             : 
     780           3 :                 wpa_printf(MSG_DEBUG,
     781             :                            "%schannel [%i] (%i) is disabled for use in AP mode, flags: 0x%x%s%s",
     782             :                            primary ? "" : "Configured HT40 secondary ",
     783           1 :                            i, chan->chan, chan->flag,
     784           1 :                            chan->flag & HOSTAPD_CHAN_NO_IR ? " NO-IR" : "",
     785           1 :                            chan->flag & HOSTAPD_CHAN_RADAR ? " RADAR" : "");
     786             :         }
     787             : 
     788           3 :         return 0;
     789             : }
     790             : 
     791             : 
     792        2616 : static int hostapd_is_usable_chans(struct hostapd_iface *iface)
     793             : {
     794        2616 :         if (!hostapd_is_usable_chan(iface, iface->conf->channel, 1))
     795           2 :                 return 0;
     796             : 
     797        2614 :         if (!iface->conf->secondary_channel)
     798        2530 :                 return 1;
     799             : 
     800         168 :         return hostapd_is_usable_chan(iface, iface->conf->channel +
     801          84 :                                       iface->conf->secondary_channel * 4, 0);
     802             : }
     803             : 
     804             : 
     805             : static enum hostapd_chan_status
     806        2628 : hostapd_check_chans(struct hostapd_iface *iface)
     807             : {
     808        2628 :         if (iface->conf->channel) {
     809        2616 :                 if (hostapd_is_usable_chans(iface))
     810        2613 :                         return HOSTAPD_CHAN_VALID;
     811             :                 else
     812           3 :                         return HOSTAPD_CHAN_INVALID;
     813             :         }
     814             : 
     815             :         /*
     816             :          * The user set channel=0 or channel=acs_survey
     817             :          * which is used to trigger ACS.
     818             :          */
     819             : 
     820          12 :         switch (acs_init(iface)) {
     821             :         case HOSTAPD_CHAN_ACS:
     822          12 :                 return HOSTAPD_CHAN_ACS;
     823             :         case HOSTAPD_CHAN_VALID:
     824             :         case HOSTAPD_CHAN_INVALID:
     825             :         default:
     826           0 :                 return HOSTAPD_CHAN_INVALID;
     827             :         }
     828             : }
     829             : 
     830             : 
     831           3 : static void hostapd_notify_bad_chans(struct hostapd_iface *iface)
     832             : {
     833           3 :         if (!iface->current_mode) {
     834           0 :                 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
     835             :                                HOSTAPD_LEVEL_WARNING,
     836             :                                "Hardware does not support configured mode");
     837           3 :                 return;
     838             :         }
     839           6 :         hostapd_logger(iface->bss[0], NULL,
     840             :                        HOSTAPD_MODULE_IEEE80211,
     841             :                        HOSTAPD_LEVEL_WARNING,
     842             :                        "Configured channel (%d) not found from the "
     843             :                        "channel list of current mode (%d) %s",
     844           3 :                        iface->conf->channel,
     845           3 :                        iface->current_mode->mode,
     846           3 :                        hostapd_hw_mode_txt(iface->current_mode->mode));
     847           3 :         hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
     848             :                        HOSTAPD_LEVEL_WARNING,
     849             :                        "Hardware does not support configured channel");
     850             : }
     851             : 
     852             : 
     853          10 : int hostapd_acs_completed(struct hostapd_iface *iface, int err)
     854             : {
     855          10 :         int ret = -1;
     856             : 
     857          10 :         if (err)
     858           0 :                 goto out;
     859             : 
     860          10 :         switch (hostapd_check_chans(iface)) {
     861             :         case HOSTAPD_CHAN_VALID:
     862          30 :                 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO,
     863             :                         ACS_EVENT_COMPLETED "freq=%d channel=%d",
     864          10 :                         hostapd_hw_get_freq(iface->bss[0],
     865          10 :                                             iface->conf->channel),
     866          10 :                         iface->conf->channel);
     867          10 :                 break;
     868             :         case HOSTAPD_CHAN_ACS:
     869           0 :                 wpa_printf(MSG_ERROR, "ACS error - reported complete, but no result available");
     870           0 :                 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, ACS_EVENT_FAILED);
     871           0 :                 hostapd_notify_bad_chans(iface);
     872           0 :                 goto out;
     873             :         case HOSTAPD_CHAN_INVALID:
     874             :         default:
     875           0 :                 wpa_printf(MSG_ERROR, "ACS picked unusable channels");
     876           0 :                 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, ACS_EVENT_FAILED);
     877           0 :                 hostapd_notify_bad_chans(iface);
     878           0 :                 goto out;
     879             :         }
     880             : 
     881          10 :         ret = hostapd_check_ht_capab(iface);
     882          10 :         if (ret < 0)
     883           0 :                 goto out;
     884          10 :         if (ret == 1) {
     885           3 :                 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback");
     886           3 :                 return 0;
     887             :         }
     888             : 
     889           7 :         ret = 0;
     890             : out:
     891           7 :         return hostapd_setup_interface_complete(iface, ret);
     892             : }
     893             : 
     894             : 
     895             : /**
     896             :  * hostapd_select_hw_mode - Select the hardware mode
     897             :  * @iface: Pointer to interface data.
     898             :  * Returns: 0 on success, < 0 on failure
     899             :  *
     900             :  * Sets up the hardware mode, channel, rates, and passive scanning
     901             :  * based on the configuration.
     902             :  */
     903        2618 : int hostapd_select_hw_mode(struct hostapd_iface *iface)
     904             : {
     905             :         int i;
     906             : 
     907        2618 :         if (iface->num_hw_features < 1)
     908           0 :                 return -1;
     909             : 
     910        2865 :         if ((iface->conf->hw_mode == HOSTAPD_MODE_IEEE80211G ||
     911        2865 :              iface->conf->ieee80211n || iface->conf->ieee80211ac) &&
     912        2608 :             iface->conf->channel == 14) {
     913           2 :                 wpa_printf(MSG_INFO, "Disable OFDM/HT/VHT on channel 14");
     914           2 :                 iface->conf->hw_mode = HOSTAPD_MODE_IEEE80211B;
     915           2 :                 iface->conf->ieee80211n = 0;
     916           2 :                 iface->conf->ieee80211ac = 0;
     917             :         }
     918             : 
     919        2618 :         iface->current_mode = NULL;
     920        2874 :         for (i = 0; i < iface->num_hw_features; i++) {
     921        2874 :                 struct hostapd_hw_modes *mode = &iface->hw_features[i];
     922        2874 :                 if (mode->mode == iface->conf->hw_mode) {
     923        2618 :                         iface->current_mode = mode;
     924        2618 :                         break;
     925             :                 }
     926             :         }
     927             : 
     928        2618 :         if (iface->current_mode == NULL) {
     929           0 :                 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_ACS_OFFLOAD) ||
     930           0 :                     !(iface->drv_flags & WPA_DRIVER_FLAGS_SUPPORT_HW_MODE_ANY))
     931             :                 {
     932           0 :                         wpa_printf(MSG_ERROR,
     933             :                                    "Hardware does not support configured mode");
     934           0 :                         hostapd_logger(iface->bss[0], NULL,
     935             :                                        HOSTAPD_MODULE_IEEE80211,
     936             :                                        HOSTAPD_LEVEL_WARNING,
     937             :                                        "Hardware does not support configured mode (%d) (hw_mode in hostapd.conf)",
     938           0 :                                        (int) iface->conf->hw_mode);
     939           0 :                         return -2;
     940             :                 }
     941             :         }
     942             : 
     943        2618 :         switch (hostapd_check_chans(iface)) {
     944             :         case HOSTAPD_CHAN_VALID:
     945        2603 :                 return 0;
     946             :         case HOSTAPD_CHAN_ACS: /* ACS will run and later complete */
     947          12 :                 return 1;
     948             :         case HOSTAPD_CHAN_INVALID:
     949             :         default:
     950           3 :                 hostapd_notify_bad_chans(iface);
     951           3 :                 return -3;
     952             :         }
     953             : }
     954             : 
     955             : 
     956        2624 : const char * hostapd_hw_mode_txt(int mode)
     957             : {
     958        2624 :         switch (mode) {
     959             :         case HOSTAPD_MODE_IEEE80211A:
     960         250 :                 return "IEEE 802.11a";
     961             :         case HOSTAPD_MODE_IEEE80211B:
     962           9 :                 return "IEEE 802.11b";
     963             :         case HOSTAPD_MODE_IEEE80211G:
     964        2365 :                 return "IEEE 802.11g";
     965             :         case HOSTAPD_MODE_IEEE80211AD:
     966           0 :                 return "IEEE 802.11ad";
     967             :         default:
     968           0 :                 return "UNKNOWN";
     969             :         }
     970             : }
     971             : 
     972             : 
     973        2741 : int hostapd_hw_get_freq(struct hostapd_data *hapd, int chan)
     974             : {
     975        2741 :         return hw_get_freq(hapd->iface->current_mode, chan);
     976             : }
     977             : 
     978             : 
     979          23 : int hostapd_hw_get_channel(struct hostapd_data *hapd, int freq)
     980             : {
     981          23 :         return hw_get_chan(hapd->iface->current_mode, freq);
     982             : }

Generated by: LCOV version 1.10