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 1393793999 Lines: 296 521 56.8 %
Date: 2014-03-02 Functions: 22 27 81.5 %
Branches: 157 359 43.7 %

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

Generated by: LCOV version 1.9