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