• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * hostapd / Hardware feature query and different modes
3  * Copyright 2002-2003, Instant802 Networks, Inc.
4  * Copyright 2005-2006, Devicescape Software, Inc.
5  * Copyright (c) 2008-2012, Jouni Malinen <j@w1.fi>
6  *
7  * This software may be distributed under the terms of the BSD license.
8  * See README for more details.
9  */
10 
11 #include "utils/includes.h"
12 
13 #include "utils/common.h"
14 #include "utils/eloop.h"
15 #include "common/ieee802_11_defs.h"
16 #include "common/ieee802_11_common.h"
17 #include "common/wpa_ctrl.h"
18 #include "common/hw_features_common.h"
19 #include "hostapd.h"
20 #include "ap_config.h"
21 #include "ap_drv_ops.h"
22 #include "acs.h"
23 #include "ieee802_11.h"
24 #include "beacon.h"
25 #include "hw_features.h"
26 
27 
hostapd_free_hw_features(struct hostapd_hw_modes * hw_features,size_t num_hw_features)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
dfs_info(struct hostapd_channel_data * chan)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 
hostapd_get_hw_features(struct hostapd_iface * iface)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 	u8 dfs_domain;
82 
83 	if (hostapd_drv_none(hapd))
84 		return -1;
85 	modes = hostapd_get_hw_feature_data(hapd, &num_modes, &flags,
86 					    &dfs_domain);
87 	if (modes == NULL) {
88 		hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
89 			       HOSTAPD_LEVEL_DEBUG,
90 			       "Fetching hardware channel/rate support not "
91 			       "supported.");
92 		return -1;
93 	}
94 
95 	iface->hw_flags = flags;
96 	iface->dfs_domain = dfs_domain;
97 
98 	hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
99 	iface->hw_features = modes;
100 	iface->num_hw_features = num_modes;
101 
102 	for (i = 0; i < num_modes; i++) {
103 		struct hostapd_hw_modes *feature = &modes[i];
104 		int dfs_enabled = hapd->iconf->ieee80211h &&
105 			(iface->drv_flags & WPA_DRIVER_FLAGS_RADAR);
106 
107 		/* set flag for channels we can use in current regulatory
108 		 * domain */
109 		for (j = 0; j < feature->num_channels; j++) {
110 			int dfs = 0;
111 
112 			/*
113 			 * Disable all channels that are marked not to allow
114 			 * to initiate radiation (a.k.a. passive scan and no
115 			 * IBSS).
116 			 * Use radar channels only if the driver supports DFS.
117 			 */
118 			if ((feature->channels[j].flag &
119 			     HOSTAPD_CHAN_RADAR) && dfs_enabled) {
120 				dfs = 1;
121 			} else if (((feature->channels[j].flag &
122 				     HOSTAPD_CHAN_RADAR) &&
123 				    !(iface->drv_flags &
124 				      WPA_DRIVER_FLAGS_DFS_OFFLOAD)) ||
125 				   (feature->channels[j].flag &
126 				    HOSTAPD_CHAN_NO_IR)) {
127 				feature->channels[j].flag |=
128 					HOSTAPD_CHAN_DISABLED;
129 			}
130 
131 			if (feature->channels[j].flag & HOSTAPD_CHAN_DISABLED)
132 				continue;
133 
134 			wpa_printf(MSG_MSGDUMP, "Allowed channel: mode=%d "
135 				   "chan=%d freq=%d MHz max_tx_power=%d dBm%s",
136 				   feature->mode,
137 				   feature->channels[j].chan,
138 				   feature->channels[j].freq,
139 				   feature->channels[j].max_tx_power,
140 				   dfs ? dfs_info(&feature->channels[j]) : "");
141 		}
142 	}
143 
144 	return 0;
145 }
146 
147 
hostapd_prepare_rates(struct hostapd_iface * iface,struct hostapd_hw_modes * mode)148 int hostapd_prepare_rates(struct hostapd_iface *iface,
149 			  struct hostapd_hw_modes *mode)
150 {
151 	int i, num_basic_rates = 0;
152 	int basic_rates_a[] = { 60, 120, 240, -1 };
153 	int basic_rates_b[] = { 10, 20, -1 };
154 	int basic_rates_g[] = { 10, 20, 55, 110, -1 };
155 	int *basic_rates;
156 
157 	if (iface->conf->basic_rates)
158 		basic_rates = iface->conf->basic_rates;
159 	else switch (mode->mode) {
160 	case HOSTAPD_MODE_IEEE80211A:
161 		basic_rates = basic_rates_a;
162 		break;
163 	case HOSTAPD_MODE_IEEE80211B:
164 		basic_rates = basic_rates_b;
165 		break;
166 	case HOSTAPD_MODE_IEEE80211G:
167 		basic_rates = basic_rates_g;
168 		break;
169 	case HOSTAPD_MODE_IEEE80211AD:
170 		return 0; /* No basic rates for 11ad */
171 	default:
172 		return -1;
173 	}
174 
175 	i = 0;
176 	while (basic_rates[i] >= 0)
177 		i++;
178 	if (i)
179 		i++; /* -1 termination */
180 	os_free(iface->basic_rates);
181 	iface->basic_rates = os_malloc(i * sizeof(int));
182 	if (iface->basic_rates)
183 		os_memcpy(iface->basic_rates, basic_rates, i * sizeof(int));
184 
185 	os_free(iface->current_rates);
186 	iface->num_rates = 0;
187 
188 	iface->current_rates =
189 		os_calloc(mode->num_rates, sizeof(struct hostapd_rate_data));
190 	if (!iface->current_rates) {
191 		wpa_printf(MSG_ERROR, "Failed to allocate memory for rate "
192 			   "table.");
193 		return -1;
194 	}
195 
196 	for (i = 0; i < mode->num_rates; i++) {
197 		struct hostapd_rate_data *rate;
198 
199 		if (iface->conf->supported_rates &&
200 		    !hostapd_rate_found(iface->conf->supported_rates,
201 					mode->rates[i]))
202 			continue;
203 
204 		rate = &iface->current_rates[iface->num_rates];
205 		rate->rate = mode->rates[i];
206 		if (hostapd_rate_found(basic_rates, rate->rate)) {
207 			rate->flags |= HOSTAPD_RATE_BASIC;
208 			num_basic_rates++;
209 		}
210 		wpa_printf(MSG_DEBUG, "RATE[%d] rate=%d flags=0x%x",
211 			   iface->num_rates, rate->rate, rate->flags);
212 		iface->num_rates++;
213 	}
214 
215 	if ((iface->num_rates == 0 || num_basic_rates == 0) &&
216 	    (!iface->conf->ieee80211n || !iface->conf->require_ht)) {
217 		wpa_printf(MSG_ERROR, "No rates remaining in supported/basic "
218 			   "rate sets (%d,%d).",
219 			   iface->num_rates, num_basic_rates);
220 		return -1;
221 	}
222 
223 	return 0;
224 }
225 
226 
ieee80211n_allowed_ht40_channel_pair(struct hostapd_iface * iface)227 static int ieee80211n_allowed_ht40_channel_pair(struct hostapd_iface *iface)
228 {
229 	int pri_freq, sec_freq;
230 	struct hostapd_channel_data *p_chan, *s_chan;
231 
232 	pri_freq = iface->freq;
233 	sec_freq = pri_freq + iface->conf->secondary_channel * 20;
234 
235 	if (!iface->current_mode)
236 		return 0;
237 
238 	p_chan = hw_get_channel_freq(iface->current_mode->mode, pri_freq, NULL,
239 				     iface->hw_features,
240 				     iface->num_hw_features);
241 
242 	s_chan = hw_get_channel_freq(iface->current_mode->mode, sec_freq, NULL,
243 				     iface->hw_features,
244 				     iface->num_hw_features);
245 
246 	return allowed_ht40_channel_pair(iface->current_mode->mode,
247 					 p_chan, s_chan);
248 }
249 
250 
ieee80211n_switch_pri_sec(struct hostapd_iface * iface)251 static void ieee80211n_switch_pri_sec(struct hostapd_iface *iface)
252 {
253 	if (iface->conf->secondary_channel > 0) {
254 		iface->conf->channel += 4;
255 		iface->freq += 20;
256 		iface->conf->secondary_channel = -1;
257 	} else {
258 		iface->conf->channel -= 4;
259 		iface->freq -= 20;
260 		iface->conf->secondary_channel = 1;
261 	}
262 }
263 
264 
ieee80211n_check_40mhz_5g(struct hostapd_iface * iface,struct wpa_scan_results * scan_res)265 static int ieee80211n_check_40mhz_5g(struct hostapd_iface *iface,
266 				     struct wpa_scan_results *scan_res)
267 {
268 	unsigned int pri_freq, sec_freq;
269 	int res;
270 	struct hostapd_channel_data *pri_chan, *sec_chan;
271 
272 	pri_freq = iface->freq;
273 	sec_freq = pri_freq + iface->conf->secondary_channel * 20;
274 
275 	if (!iface->current_mode)
276 		return 0;
277 	pri_chan = hw_get_channel_freq(iface->current_mode->mode, pri_freq,
278 				       NULL, iface->hw_features,
279 				       iface->num_hw_features);
280 	sec_chan = hw_get_channel_freq(iface->current_mode->mode, sec_freq,
281 				       NULL, iface->hw_features,
282 				       iface->num_hw_features);
283 
284 	res = check_40mhz_5g(scan_res, pri_chan, sec_chan);
285 
286 	if (res == 2) {
287 		if (iface->conf->no_pri_sec_switch) {
288 			wpa_printf(MSG_DEBUG,
289 				   "Cannot switch PRI/SEC channels due to local constraint");
290 		} else {
291 			ieee80211n_switch_pri_sec(iface);
292 		}
293 	}
294 
295 	return !!res;
296 }
297 
298 
ieee80211n_check_40mhz_2g4(struct hostapd_iface * iface,struct wpa_scan_results * scan_res)299 static int ieee80211n_check_40mhz_2g4(struct hostapd_iface *iface,
300 				      struct wpa_scan_results *scan_res)
301 {
302 	int pri_chan, sec_chan;
303 
304 	pri_chan = iface->conf->channel;
305 	sec_chan = pri_chan + iface->conf->secondary_channel * 4;
306 
307 	return check_40mhz_2g4(iface->current_mode, scan_res, pri_chan,
308 			       sec_chan);
309 }
310 
311 
ieee80211n_check_scan(struct hostapd_iface * iface)312 static void ieee80211n_check_scan(struct hostapd_iface *iface)
313 {
314 	struct wpa_scan_results *scan_res;
315 	int oper40;
316 	int res = 0;
317 
318 	/* Check list of neighboring BSSes (from scan) to see whether 40 MHz is
319 	 * allowed per IEEE Std 802.11-2012, 10.15.3.2 */
320 
321 	iface->scan_cb = NULL;
322 
323 	scan_res = hostapd_driver_get_scan_results(iface->bss[0]);
324 	if (scan_res == NULL) {
325 		hostapd_setup_interface_complete(iface, 1);
326 		return;
327 	}
328 
329 	if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A)
330 		oper40 = ieee80211n_check_40mhz_5g(iface, scan_res);
331 	else
332 		oper40 = ieee80211n_check_40mhz_2g4(iface, scan_res);
333 	wpa_scan_results_free(scan_res);
334 
335 	iface->secondary_ch = iface->conf->secondary_channel;
336 	if (!oper40) {
337 		wpa_printf(MSG_INFO, "20/40 MHz operation not permitted on "
338 			   "channel pri=%d sec=%d based on overlapping BSSes",
339 			   iface->conf->channel,
340 			   iface->conf->channel +
341 			   iface->conf->secondary_channel * 4);
342 		iface->conf->secondary_channel = 0;
343 		if (iface->drv_flags & WPA_DRIVER_FLAGS_HT_2040_COEX) {
344 			/*
345 			 * TODO: Could consider scheduling another scan to check
346 			 * if channel width can be changed if no coex reports
347 			 * are received from associating stations.
348 			 */
349 		}
350 	}
351 
352 #ifdef CONFIG_IEEE80211AX
353 	if (iface->conf->secondary_channel &&
354 	    iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
355 	    iface->conf->ieee80211ax) {
356 		struct he_capabilities *he_cap;
357 
358 		he_cap = &iface->current_mode->he_capab[IEEE80211_MODE_AP];
359 		if (!(he_cap->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
360 		      HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_IN_2G)) {
361 			wpa_printf(MSG_DEBUG,
362 				   "HE: 40 MHz channel width is not supported in 2.4 GHz; clear secondary channel configuration");
363 			iface->conf->secondary_channel = 0;
364 		}
365 	}
366 #endif /* CONFIG_IEEE80211AX */
367 
368 	if (iface->conf->secondary_channel)
369 		res = ieee80211n_allowed_ht40_channel_pair(iface);
370 	if (!res) {
371 		iface->conf->secondary_channel = 0;
372 		hostapd_set_oper_centr_freq_seg0_idx(iface->conf, 0);
373 		hostapd_set_oper_centr_freq_seg1_idx(iface->conf, 0);
374 		hostapd_set_oper_chwidth(iface->conf, CONF_OPER_CHWIDTH_USE_HT);
375 		res = 1;
376 		wpa_printf(MSG_INFO, "Fallback to 20 MHz");
377 	}
378 
379 	hostapd_setup_interface_complete(iface, !res);
380 }
381 
382 
ieee80211n_scan_channels_2g4(struct hostapd_iface * iface,struct wpa_driver_scan_params * params)383 static void ieee80211n_scan_channels_2g4(struct hostapd_iface *iface,
384 					 struct wpa_driver_scan_params *params)
385 {
386 	/* Scan only the affected frequency range */
387 	int pri_freq, sec_freq;
388 	int affected_start, affected_end;
389 	int i, pos;
390 	struct hostapd_hw_modes *mode;
391 
392 	if (iface->current_mode == NULL)
393 		return;
394 
395 	pri_freq = iface->freq;
396 	if (iface->conf->secondary_channel > 0)
397 		sec_freq = pri_freq + 20;
398 	else
399 		sec_freq = pri_freq - 20;
400 	/*
401 	 * Note: Need to find the PRI channel also in cases where the affected
402 	 * channel is the SEC channel of a 40 MHz BSS, so need to include the
403 	 * scanning coverage here to be 40 MHz from the center frequency.
404 	 */
405 	affected_start = (pri_freq + sec_freq) / 2 - 40;
406 	affected_end = (pri_freq + sec_freq) / 2 + 40;
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 
ieee80211n_scan_channels_5g(struct hostapd_iface * iface,struct wpa_driver_scan_params * params)428 static void ieee80211n_scan_channels_5g(struct hostapd_iface *iface,
429 					struct wpa_driver_scan_params *params)
430 {
431 	/* Scan only the affected frequency range */
432 	int pri_freq;
433 	int affected_start, affected_end;
434 	int i, pos;
435 	struct hostapd_hw_modes *mode;
436 
437 	if (iface->current_mode == NULL)
438 		return;
439 
440 	pri_freq = iface->freq;
441 	if (iface->conf->secondary_channel > 0) {
442 		affected_start = pri_freq - 10;
443 		affected_end = pri_freq + 30;
444 	} else {
445 		affected_start = pri_freq - 30;
446 		affected_end = pri_freq + 10;
447 	}
448 	wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
449 		   affected_start, affected_end);
450 
451 	mode = iface->current_mode;
452 	params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
453 	if (params->freqs == NULL)
454 		return;
455 	pos = 0;
456 
457 	for (i = 0; i < mode->num_channels; i++) {
458 		struct hostapd_channel_data *chan = &mode->channels[i];
459 		if (chan->flag & HOSTAPD_CHAN_DISABLED)
460 			continue;
461 		if (chan->freq < affected_start ||
462 		    chan->freq > affected_end)
463 			continue;
464 		params->freqs[pos++] = chan->freq;
465 	}
466 }
467 
468 
ap_ht40_scan_retry(void * eloop_data,void * user_data)469 static void ap_ht40_scan_retry(void *eloop_data, void *user_data)
470 {
471 #define HT2040_COEX_SCAN_RETRY 15
472 	struct hostapd_iface *iface = eloop_data;
473 	struct wpa_driver_scan_params params;
474 	int ret;
475 
476 	os_memset(&params, 0, sizeof(params));
477 	if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
478 		ieee80211n_scan_channels_2g4(iface, &params);
479 	else
480 		ieee80211n_scan_channels_5g(iface, &params);
481 
482 	ret = hostapd_driver_scan(iface->bss[0], &params);
483 	iface->num_ht40_scan_tries++;
484 	os_free(params.freqs);
485 
486 	if (ret == -EBUSY &&
487 	    iface->num_ht40_scan_tries < HT2040_COEX_SCAN_RETRY) {
488 		wpa_printf(MSG_ERROR,
489 			   "Failed to request a scan of neighboring BSSes ret=%d (%s) - try to scan again (attempt %d)",
490 			   ret, strerror(-ret), iface->num_ht40_scan_tries);
491 		eloop_register_timeout(1, 0, ap_ht40_scan_retry, iface, NULL);
492 		return;
493 	}
494 
495 	if (ret == 0) {
496 		iface->scan_cb = ieee80211n_check_scan;
497 		return;
498 	}
499 
500 	wpa_printf(MSG_DEBUG,
501 		   "Failed to request a scan in device, bringing up in HT20 mode");
502 	iface->conf->secondary_channel = 0;
503 	iface->conf->ht_capab &= ~HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
504 	hostapd_setup_interface_complete(iface, 0);
505 }
506 
507 
hostapd_stop_setup_timers(struct hostapd_iface * iface)508 void hostapd_stop_setup_timers(struct hostapd_iface *iface)
509 {
510 	eloop_cancel_timeout(ap_ht40_scan_retry, iface, NULL);
511 }
512 
513 
ieee80211n_check_40mhz(struct hostapd_iface * iface)514 static int ieee80211n_check_40mhz(struct hostapd_iface *iface)
515 {
516 	struct wpa_driver_scan_params params;
517 	int ret;
518 
519 	/* Check that HT40 is used and PRI / SEC switch is allowed */
520 	if (!iface->conf->secondary_channel || iface->conf->no_pri_sec_switch)
521 		return 0;
522 
523 	hostapd_set_state(iface, HAPD_IFACE_HT_SCAN);
524 	wpa_printf(MSG_DEBUG, "Scan for neighboring BSSes prior to enabling "
525 		   "40 MHz channel");
526 	os_memset(&params, 0, sizeof(params));
527 	if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
528 		ieee80211n_scan_channels_2g4(iface, &params);
529 	else
530 		ieee80211n_scan_channels_5g(iface, &params);
531 
532 	ret = hostapd_driver_scan(iface->bss[0], &params);
533 	os_free(params.freqs);
534 
535 	if (ret == -EBUSY) {
536 		wpa_printf(MSG_ERROR,
537 			   "Failed to request a scan of neighboring BSSes ret=%d (%s) - try to scan again",
538 			   ret, strerror(-ret));
539 		iface->num_ht40_scan_tries = 1;
540 		eloop_cancel_timeout(ap_ht40_scan_retry, iface, NULL);
541 		eloop_register_timeout(1, 0, ap_ht40_scan_retry, iface, NULL);
542 		return 1;
543 	}
544 
545 	if (ret < 0) {
546 		wpa_printf(MSG_ERROR,
547 			   "Failed to request a scan of neighboring BSSes ret=%d (%s)",
548 			   ret, strerror(-ret));
549 		return -1;
550 	}
551 
552 	iface->scan_cb = ieee80211n_check_scan;
553 	return 1;
554 }
555 
556 
ieee80211n_supported_ht_capab(struct hostapd_iface * iface)557 static int ieee80211n_supported_ht_capab(struct hostapd_iface *iface)
558 {
559 	u16 hw = iface->current_mode->ht_capab;
560 	u16 conf = iface->conf->ht_capab;
561 
562 	if ((conf & HT_CAP_INFO_LDPC_CODING_CAP) &&
563 	    !(hw & HT_CAP_INFO_LDPC_CODING_CAP)) {
564 		wpa_printf(MSG_ERROR, "Driver does not support configured "
565 			   "HT capability [LDPC]");
566 		return 0;
567 	}
568 
569 	/*
570 	 * Driver ACS chosen channel may not be HT40 due to internal driver
571 	 * restrictions.
572 	 */
573 	if (!iface->conf->acs && (conf & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) &&
574 	    !(hw & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
575 		wpa_printf(MSG_ERROR, "Driver does not support configured "
576 			   "HT capability [HT40*]");
577 		return 0;
578 	}
579 
580 	if ((conf & HT_CAP_INFO_GREEN_FIELD) &&
581 	    !(hw & HT_CAP_INFO_GREEN_FIELD)) {
582 		wpa_printf(MSG_ERROR, "Driver does not support configured "
583 			   "HT capability [GF]");
584 		return 0;
585 	}
586 
587 	if ((conf & HT_CAP_INFO_SHORT_GI20MHZ) &&
588 	    !(hw & HT_CAP_INFO_SHORT_GI20MHZ)) {
589 		wpa_printf(MSG_ERROR, "Driver does not support configured "
590 			   "HT capability [SHORT-GI-20]");
591 		return 0;
592 	}
593 
594 	if ((conf & HT_CAP_INFO_SHORT_GI40MHZ) &&
595 	    !(hw & HT_CAP_INFO_SHORT_GI40MHZ)) {
596 		wpa_printf(MSG_ERROR, "Driver does not support configured "
597 			   "HT capability [SHORT-GI-40]");
598 		return 0;
599 	}
600 
601 	if ((conf & HT_CAP_INFO_TX_STBC) && !(hw & HT_CAP_INFO_TX_STBC)) {
602 		wpa_printf(MSG_ERROR, "Driver does not support configured "
603 			   "HT capability [TX-STBC]");
604 		return 0;
605 	}
606 
607 	if ((conf & HT_CAP_INFO_RX_STBC_MASK) >
608 	    (hw & HT_CAP_INFO_RX_STBC_MASK)) {
609 		wpa_printf(MSG_ERROR, "Driver does not support configured "
610 			   "HT capability [RX-STBC*]");
611 		return 0;
612 	}
613 
614 	if ((conf & HT_CAP_INFO_DELAYED_BA) &&
615 	    !(hw & HT_CAP_INFO_DELAYED_BA)) {
616 		wpa_printf(MSG_ERROR, "Driver does not support configured "
617 			   "HT capability [DELAYED-BA]");
618 		return 0;
619 	}
620 
621 	if ((conf & HT_CAP_INFO_MAX_AMSDU_SIZE) &&
622 	    !(hw & HT_CAP_INFO_MAX_AMSDU_SIZE)) {
623 		wpa_printf(MSG_ERROR, "Driver does not support configured "
624 			   "HT capability [MAX-AMSDU-7935]");
625 		return 0;
626 	}
627 
628 	if ((conf & HT_CAP_INFO_DSSS_CCK40MHZ) &&
629 	    !(hw & HT_CAP_INFO_DSSS_CCK40MHZ)) {
630 		wpa_printf(MSG_ERROR, "Driver does not support configured "
631 			   "HT capability [DSSS_CCK-40]");
632 		return 0;
633 	}
634 
635 	if ((conf & HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT) &&
636 	    !(hw & HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT)) {
637 		wpa_printf(MSG_ERROR, "Driver does not support configured "
638 			   "HT capability [LSIG-TXOP-PROT]");
639 		return 0;
640 	}
641 
642 	return 1;
643 }
644 
645 
646 #ifdef CONFIG_IEEE80211AC
ieee80211ac_supported_vht_capab(struct hostapd_iface * iface)647 static int ieee80211ac_supported_vht_capab(struct hostapd_iface *iface)
648 {
649 	struct hostapd_hw_modes *mode = iface->current_mode;
650 	u32 hw = mode->vht_capab;
651 	u32 conf = iface->conf->vht_capab;
652 
653 	wpa_printf(MSG_DEBUG, "hw vht capab: 0x%x, conf vht capab: 0x%x",
654 		   hw, conf);
655 
656 	if (mode->mode == HOSTAPD_MODE_IEEE80211G &&
657 	    iface->conf->bss[0]->vendor_vht &&
658 	    mode->vht_capab == 0 && iface->hw_features) {
659 		int i;
660 
661 		for (i = 0; i < iface->num_hw_features; i++) {
662 			if (iface->hw_features[i].mode ==
663 			    HOSTAPD_MODE_IEEE80211A) {
664 				mode = &iface->hw_features[i];
665 				hw = mode->vht_capab;
666 				wpa_printf(MSG_DEBUG,
667 					   "update hw vht capab based on 5 GHz band: 0x%x",
668 					   hw);
669 				break;
670 			}
671 		}
672 	}
673 
674 	return ieee80211ac_cap_check(hw, conf);
675 }
676 #endif /* CONFIG_IEEE80211AC */
677 
678 
679 #ifdef CONFIG_IEEE80211AX
ieee80211ax_supported_he_capab(struct hostapd_iface * iface)680 static int ieee80211ax_supported_he_capab(struct hostapd_iface *iface)
681 {
682 	return 1;
683 }
684 #endif /* CONFIG_IEEE80211AX */
685 
686 
hostapd_check_ht_capab(struct hostapd_iface * iface)687 int hostapd_check_ht_capab(struct hostapd_iface *iface)
688 {
689 	int ret;
690 
691 	if (is_6ghz_freq(iface->freq))
692 		return 0;
693 	if (!iface->conf->ieee80211n)
694 		return 0;
695 
696 	if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211B &&
697 	    iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G &&
698 	    (iface->conf->ht_capab & HT_CAP_INFO_DSSS_CCK40MHZ)) {
699 		wpa_printf(MSG_DEBUG,
700 			   "Disable HT capability [DSSS_CCK-40] on 5 GHz band");
701 		iface->conf->ht_capab &= ~HT_CAP_INFO_DSSS_CCK40MHZ;
702 	}
703 
704 	if (!ieee80211n_supported_ht_capab(iface))
705 		return -1;
706 #ifdef CONFIG_IEEE80211AX
707 	if (iface->conf->ieee80211ax &&
708 	    !ieee80211ax_supported_he_capab(iface))
709 		return -1;
710 #endif /* CONFIG_IEEE80211AX */
711 #ifdef CONFIG_IEEE80211AC
712 	if (iface->conf->ieee80211ac &&
713 	    !ieee80211ac_supported_vht_capab(iface))
714 		return -1;
715 #endif /* CONFIG_IEEE80211AC */
716 	ret = ieee80211n_check_40mhz(iface);
717 	if (ret)
718 		return ret;
719 	if (!ieee80211n_allowed_ht40_channel_pair(iface))
720 		return -1;
721 
722 	return 0;
723 }
724 
725 
hostapd_check_edmg_capab(struct hostapd_iface * iface)726 int hostapd_check_edmg_capab(struct hostapd_iface *iface)
727 {
728 	struct hostapd_hw_modes *mode = iface->hw_features;
729 	struct ieee80211_edmg_config edmg;
730 
731 	if (!iface->conf->enable_edmg)
732 		return 0;
733 
734 	hostapd_encode_edmg_chan(iface->conf->enable_edmg,
735 				 iface->conf->edmg_channel,
736 				 iface->conf->channel,
737 				 &edmg);
738 
739 	if (mode->edmg.channels && ieee802_edmg_is_allowed(mode->edmg, edmg))
740 		return 0;
741 
742 	wpa_printf(MSG_WARNING, "Requested EDMG configuration is not valid");
743 	wpa_printf(MSG_INFO, "EDMG capab: channels 0x%x, bw_config %d",
744 		   mode->edmg.channels, mode->edmg.bw_config);
745 	wpa_printf(MSG_INFO,
746 		   "Requested EDMG configuration: channels 0x%x, bw_config %d",
747 		   edmg.channels, edmg.bw_config);
748 	return -1;
749 }
750 
751 
hostapd_check_he_6ghz_capab(struct hostapd_iface * iface)752 int hostapd_check_he_6ghz_capab(struct hostapd_iface *iface)
753 {
754 #ifdef CONFIG_IEEE80211AX
755 	struct he_capabilities *he_cap;
756 	u16 hw;
757 
758 	if (!iface->current_mode || !is_6ghz_freq(iface->freq))
759 		return 0;
760 
761 	he_cap = &iface->current_mode->he_capab[IEEE80211_MODE_AP];
762 	hw = he_cap->he_6ghz_capa;
763 	if (iface->conf->he_6ghz_max_mpdu >
764 	    ((hw & HE_6GHZ_BAND_CAP_MAX_MPDU_LEN_MASK) >>
765 	     HE_6GHZ_BAND_CAP_MAX_MPDU_LEN_SHIFT)) {
766 		wpa_printf(MSG_ERROR,
767 			   "The driver does not support the configured HE 6 GHz Max MPDU length");
768 		return -1;
769 	}
770 
771 	if (iface->conf->he_6ghz_max_ampdu_len_exp >
772 	    ((hw & HE_6GHZ_BAND_CAP_MAX_AMPDU_LEN_EXP_MASK) >>
773 	     HE_6GHZ_BAND_CAP_MAX_AMPDU_LEN_EXP_SHIFT)) {
774 		wpa_printf(MSG_ERROR,
775 			   "The driver does not support the configured HE 6 GHz Max AMPDU Length Exponent");
776 		return -1;
777 	}
778 
779 	if (iface->conf->he_6ghz_rx_ant_pat &&
780 	    !(hw & HE_6GHZ_BAND_CAP_RX_ANTPAT_CONS)) {
781 		wpa_printf(MSG_ERROR,
782 			   "The driver does not support the configured HE 6 GHz Rx Antenna Pattern");
783 		return -1;
784 	}
785 
786 	if (iface->conf->he_6ghz_tx_ant_pat &&
787 	    !(hw & HE_6GHZ_BAND_CAP_TX_ANTPAT_CONS)) {
788 		wpa_printf(MSG_ERROR,
789 			   "The driver does not support the configured HE 6 GHz Tx Antenna Pattern");
790 		return -1;
791 	}
792 #endif /* CONFIG_IEEE80211AX */
793 	return 0;
794 }
795 
796 
hostapd_is_usable_chan(struct hostapd_iface * iface,int frequency,int primary)797 static int hostapd_is_usable_chan(struct hostapd_iface *iface,
798 				  int frequency, int primary)
799 {
800 	struct hostapd_channel_data *chan;
801 
802 	if (!iface->current_mode)
803 		return 0;
804 
805 	chan = hw_get_channel_freq(iface->current_mode->mode, frequency, NULL,
806 				   iface->hw_features, iface->num_hw_features);
807 	if (!chan)
808 		return 0;
809 
810 	if ((primary && chan_pri_allowed(chan)) ||
811 	    (!primary && !(chan->flag & HOSTAPD_CHAN_DISABLED)))
812 		return 1;
813 
814 	wpa_printf(MSG_INFO,
815 		   "Frequency %d (%s) not allowed for AP mode, flags: 0x%x%s%s",
816 		   frequency, primary ? "primary" : "secondary",
817 		   chan->flag,
818 		   chan->flag & HOSTAPD_CHAN_NO_IR ? " NO-IR" : "",
819 		   chan->flag & HOSTAPD_CHAN_RADAR ? " RADAR" : "");
820 	return 0;
821 }
822 
823 
hostapd_is_usable_edmg(struct hostapd_iface * iface)824 static int hostapd_is_usable_edmg(struct hostapd_iface *iface)
825 {
826 	int i, contiguous = 0;
827 	int num_of_enabled = 0;
828 	int max_contiguous = 0;
829 	struct ieee80211_edmg_config edmg;
830 	struct hostapd_channel_data *pri_chan;
831 
832 	if (!iface->conf->enable_edmg)
833 		return 1;
834 
835 	if (!iface->current_mode)
836 		return 0;
837 	pri_chan = hw_get_channel_freq(iface->current_mode->mode,
838 				       iface->freq, NULL,
839 				       iface->hw_features,
840 				       iface->num_hw_features);
841 	if (!pri_chan)
842 		return 0;
843 	hostapd_encode_edmg_chan(iface->conf->enable_edmg,
844 				 iface->conf->edmg_channel,
845 				 pri_chan->chan,
846 				 &edmg);
847 	if (!(edmg.channels & BIT(pri_chan->chan - 1)))
848 		return 0;
849 
850 	/* 60 GHz channels 1..6 */
851 	for (i = 0; i < 6; i++) {
852 		int freq = 56160 + 2160 * (i + 1);
853 
854 		if (edmg.channels & BIT(i)) {
855 			contiguous++;
856 			num_of_enabled++;
857 		} else {
858 			contiguous = 0;
859 			continue;
860 		}
861 
862 		/* P802.11ay defines that the total number of subfields
863 		 * set to one does not exceed 4.
864 		 */
865 		if (num_of_enabled > 4)
866 			return 0;
867 
868 		if (!hostapd_is_usable_chan(iface, freq, 1))
869 			return 0;
870 
871 		if (contiguous > max_contiguous)
872 			max_contiguous = contiguous;
873 	}
874 
875 	/* Check if the EDMG configuration is valid under the limitations
876 	 * of P802.11ay.
877 	 */
878 	/* check bw_config against contiguous EDMG channels */
879 	switch (edmg.bw_config) {
880 	case EDMG_BW_CONFIG_4:
881 		if (!max_contiguous)
882 			return 0;
883 		break;
884 	case EDMG_BW_CONFIG_5:
885 		if (max_contiguous < 2)
886 			return 0;
887 		break;
888 	default:
889 		return 0;
890 	}
891 
892 	return 1;
893 }
894 
895 
hostapd_is_usable_punct_bitmap(struct hostapd_iface * iface)896 static bool hostapd_is_usable_punct_bitmap(struct hostapd_iface *iface)
897 {
898 #ifdef CONFIG_IEEE80211BE
899 	struct hostapd_config *conf = iface->conf;
900 	u8 bw, start_chan;
901 
902 	if (!conf->punct_bitmap)
903 		return true;
904 
905 	if (!conf->ieee80211be) {
906 		wpa_printf(MSG_ERROR,
907 			   "Currently RU puncturing is supported only if ieee80211be is enabled");
908 		return false;
909 	}
910 
911 	if (iface->freq >= 2412 && iface->freq <= 2484) {
912 		wpa_printf(MSG_ERROR,
913 			   "RU puncturing not supported in 2.4 GHz");
914 		return false;
915 	}
916 
917 	switch (conf->eht_oper_chwidth) {
918 	case 0:
919 		wpa_printf(MSG_ERROR,
920 			   "RU puncturing is supported only in 80 MHz and 160 MHz");
921 		return false;
922 	case 1:
923 		bw = 80;
924 		start_chan = conf->eht_oper_centr_freq_seg0_idx - 6;
925 		break;
926 	case 2:
927 		bw = 160;
928 		start_chan = conf->eht_oper_centr_freq_seg0_idx - 14;
929 		break;
930 	default:
931 		return false;
932 	}
933 
934 	if (!is_punct_bitmap_valid(bw, (conf->channel - start_chan) / 4,
935 				   conf->punct_bitmap)) {
936 		wpa_printf(MSG_ERROR, "Invalid puncturing bitmap");
937 		return false;
938 	}
939 #endif /* CONFIG_IEEE80211BE */
940 
941 	return true;
942 }
943 
944 
hostapd_is_usable_chans(struct hostapd_iface * iface)945 static int hostapd_is_usable_chans(struct hostapd_iface *iface)
946 {
947 	int secondary_freq;
948 	struct hostapd_channel_data *pri_chan;
949 
950 	if (!iface->current_mode)
951 		return 0;
952 	pri_chan = hw_get_channel_freq(iface->current_mode->mode,
953 				       iface->freq, NULL,
954 				       iface->hw_features,
955 				       iface->num_hw_features);
956 	if (!pri_chan) {
957 		wpa_printf(MSG_ERROR, "Primary frequency not present");
958 		return 0;
959 	}
960 	if (!hostapd_is_usable_chan(iface, pri_chan->freq, 1)) {
961 		wpa_printf(MSG_ERROR, "Primary frequency not allowed");
962 		return 0;
963 	}
964 	if (!hostapd_is_usable_edmg(iface))
965 		return 0;
966 
967 	if (!hostapd_is_usable_punct_bitmap(iface))
968 		return 0;
969 
970 	if (!iface->conf->secondary_channel)
971 		return 1;
972 
973 	if (hostapd_is_usable_chan(iface, iface->freq +
974 				   iface->conf->secondary_channel * 20, 0)) {
975 		if (iface->conf->secondary_channel == 1 &&
976 		    (pri_chan->allowed_bw & HOSTAPD_CHAN_WIDTH_40P))
977 			return 1;
978 		if (iface->conf->secondary_channel == -1 &&
979 		    (pri_chan->allowed_bw & HOSTAPD_CHAN_WIDTH_40M))
980 			return 1;
981 	}
982 	if (!iface->conf->ht40_plus_minus_allowed)
983 		return 0;
984 
985 	/* Both HT40+ and HT40- are set, pick a valid secondary channel */
986 	secondary_freq = iface->freq + 20;
987 	if (hostapd_is_usable_chan(iface, secondary_freq, 0) &&
988 	    (pri_chan->allowed_bw & HOSTAPD_CHAN_WIDTH_40P)) {
989 		iface->conf->secondary_channel = 1;
990 		return 1;
991 	}
992 
993 	secondary_freq = iface->freq - 20;
994 	if (hostapd_is_usable_chan(iface, secondary_freq, 0) &&
995 	    (pri_chan->allowed_bw & HOSTAPD_CHAN_WIDTH_40M)) {
996 		iface->conf->secondary_channel = -1;
997 		return 1;
998 	}
999 
1000 	return 0;
1001 }
1002 
1003 
skip_mode(struct hostapd_iface * iface,struct hostapd_hw_modes * mode)1004 static bool skip_mode(struct hostapd_iface *iface,
1005 		      struct hostapd_hw_modes *mode)
1006 {
1007 	int chan;
1008 
1009 	if (iface->freq > 0 && !hw_mode_get_channel(mode, iface->freq, &chan))
1010 		return true;
1011 
1012 	if (is_6ghz_op_class(iface->conf->op_class) && iface->freq == 0 &&
1013 	    (mode->mode != HOSTAPD_MODE_IEEE80211A ||
1014 	     mode->num_channels == 0 ||
1015 	     !is_6ghz_freq(mode->channels[0].freq)))
1016 		return true;
1017 
1018 	return false;
1019 }
1020 
1021 
hostapd_determine_mode(struct hostapd_iface * iface)1022 static void hostapd_determine_mode(struct hostapd_iface *iface)
1023 {
1024 	int i;
1025 	enum hostapd_hw_mode target_mode;
1026 
1027 	if (iface->current_mode ||
1028 	    iface->conf->hw_mode != HOSTAPD_MODE_IEEE80211ANY)
1029 		return;
1030 
1031 	if (iface->freq < 4000)
1032 		target_mode = HOSTAPD_MODE_IEEE80211G;
1033 	else if (iface->freq > 50000)
1034 		target_mode = HOSTAPD_MODE_IEEE80211AD;
1035 	else
1036 		target_mode = HOSTAPD_MODE_IEEE80211A;
1037 
1038 	for (i = 0; i < iface->num_hw_features; i++) {
1039 		struct hostapd_hw_modes *mode;
1040 
1041 		mode = &iface->hw_features[i];
1042 		if (mode->mode == target_mode) {
1043 			if (skip_mode(iface, mode))
1044 				continue;
1045 
1046 			iface->current_mode = mode;
1047 			iface->conf->hw_mode = mode->mode;
1048 			break;
1049 		}
1050 	}
1051 
1052 	if (!iface->current_mode)
1053 		wpa_printf(MSG_ERROR, "ACS: Cannot decide mode");
1054 }
1055 
1056 
1057 static enum hostapd_chan_status
hostapd_check_chans(struct hostapd_iface * iface)1058 hostapd_check_chans(struct hostapd_iface *iface)
1059 {
1060 	if (iface->freq) {
1061 		hostapd_determine_mode(iface);
1062 		if (hostapd_is_usable_chans(iface))
1063 			return HOSTAPD_CHAN_VALID;
1064 		else
1065 			return HOSTAPD_CHAN_INVALID;
1066 	}
1067 
1068 	/*
1069 	 * The user set channel=0 or channel=acs_survey
1070 	 * which is used to trigger ACS.
1071 	 */
1072 
1073 	switch (acs_init(iface)) {
1074 	case HOSTAPD_CHAN_ACS:
1075 		return HOSTAPD_CHAN_ACS;
1076 	case HOSTAPD_CHAN_VALID:
1077 	case HOSTAPD_CHAN_INVALID:
1078 	default:
1079 		return HOSTAPD_CHAN_INVALID;
1080 	}
1081 }
1082 
1083 
hostapd_notify_bad_chans(struct hostapd_iface * iface)1084 static void hostapd_notify_bad_chans(struct hostapd_iface *iface)
1085 {
1086 	if (!iface->current_mode) {
1087 		hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
1088 			       HOSTAPD_LEVEL_WARNING,
1089 			       "Hardware does not support configured mode");
1090 		return;
1091 	}
1092 	hostapd_logger(iface->bss[0], NULL,
1093 		       HOSTAPD_MODULE_IEEE80211,
1094 		       HOSTAPD_LEVEL_WARNING,
1095 		       "Configured channel (%d) or frequency (%d) (secondary_channel=%d) not found from the channel list of the current mode (%d) %s",
1096 		       iface->conf->channel,
1097 		       iface->freq, iface->conf->secondary_channel,
1098 		       iface->current_mode->mode,
1099 		       hostapd_hw_mode_txt(iface->current_mode->mode));
1100 	hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
1101 		       HOSTAPD_LEVEL_WARNING,
1102 		       "Hardware does not support configured channel");
1103 }
1104 
1105 
hostapd_acs_completed(struct hostapd_iface * iface,int err)1106 int hostapd_acs_completed(struct hostapd_iface *iface, int err)
1107 {
1108 	int ret = -1;
1109 
1110 	if (err)
1111 		goto out;
1112 
1113 	switch (hostapd_check_chans(iface)) {
1114 	case HOSTAPD_CHAN_VALID:
1115 		wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO,
1116 			ACS_EVENT_COMPLETED "freq=%d channel=%d",
1117 			iface->freq, iface->conf->channel);
1118 		break;
1119 	case HOSTAPD_CHAN_ACS:
1120 		wpa_printf(MSG_ERROR, "ACS error - reported complete, but no result available");
1121 		wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, ACS_EVENT_FAILED);
1122 		hostapd_notify_bad_chans(iface);
1123 		goto out;
1124 	case HOSTAPD_CHAN_INVALID:
1125 	default:
1126 		wpa_printf(MSG_ERROR, "ACS picked unusable channels");
1127 		wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, ACS_EVENT_FAILED);
1128 		hostapd_notify_bad_chans(iface);
1129 		goto out;
1130 	}
1131 
1132 	ret = hostapd_check_ht_capab(iface);
1133 	if (ret < 0)
1134 		goto out;
1135 	if (ret == 1) {
1136 		wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback");
1137 		return 0;
1138 	}
1139 
1140 	ret = 0;
1141 out:
1142 	return hostapd_setup_interface_complete(iface, ret);
1143 }
1144 
1145 
1146 /**
1147  * hostapd_select_hw_mode - Select the hardware mode
1148  * @iface: Pointer to interface data.
1149  * Returns: 0 on success, < 0 on failure
1150  *
1151  * Sets up the hardware mode, channel, rates, and passive scanning
1152  * based on the configuration.
1153  */
hostapd_select_hw_mode(struct hostapd_iface * iface)1154 int hostapd_select_hw_mode(struct hostapd_iface *iface)
1155 {
1156 	int i;
1157 
1158 	if (iface->num_hw_features < 1)
1159 		return -1;
1160 
1161 	if ((iface->conf->hw_mode == HOSTAPD_MODE_IEEE80211G ||
1162 	     iface->conf->ieee80211n || iface->conf->ieee80211ac ||
1163 	     iface->conf->ieee80211ax || iface->conf->ieee80211be) &&
1164 	    iface->conf->channel == 14) {
1165 		wpa_printf(MSG_INFO, "Disable OFDM/HT/VHT/HE/EHT on channel 14");
1166 		iface->conf->hw_mode = HOSTAPD_MODE_IEEE80211B;
1167 		iface->conf->ieee80211n = 0;
1168 		iface->conf->ieee80211ac = 0;
1169 		iface->conf->ieee80211ax = 0;
1170 		iface->conf->ieee80211be = 0;
1171 	}
1172 
1173 	iface->current_mode = NULL;
1174 	for (i = 0; i < iface->num_hw_features; i++) {
1175 		struct hostapd_hw_modes *mode = &iface->hw_features[i];
1176 
1177 		if (mode->mode == iface->conf->hw_mode) {
1178 			if (skip_mode(iface, mode))
1179 				continue;
1180 
1181 			iface->current_mode = mode;
1182 			break;
1183 		}
1184 	}
1185 
1186 	if (iface->current_mode == NULL) {
1187 		if ((iface->drv_flags & WPA_DRIVER_FLAGS_ACS_OFFLOAD) &&
1188 		    (iface->drv_flags & WPA_DRIVER_FLAGS_SUPPORT_HW_MODE_ANY)) {
1189 			wpa_printf(MSG_DEBUG,
1190 				   "Using offloaded hw_mode=any ACS");
1191 		} else if (!(iface->drv_flags & WPA_DRIVER_FLAGS_ACS_OFFLOAD) &&
1192 			   iface->conf->hw_mode == HOSTAPD_MODE_IEEE80211ANY) {
1193 			wpa_printf(MSG_DEBUG,
1194 				   "Using internal ACS for hw_mode=any");
1195 		} else {
1196 			wpa_printf(MSG_ERROR,
1197 				   "Hardware does not support configured mode");
1198 			hostapd_logger(iface->bss[0], NULL,
1199 				       HOSTAPD_MODULE_IEEE80211,
1200 				       HOSTAPD_LEVEL_WARNING,
1201 				       "Hardware does not support configured mode (%d) (hw_mode in hostapd.conf)",
1202 				       (int) iface->conf->hw_mode);
1203 			return -2;
1204 		}
1205 	}
1206 
1207 	switch (hostapd_check_chans(iface)) {
1208 	case HOSTAPD_CHAN_VALID:
1209 		return 0;
1210 	case HOSTAPD_CHAN_ACS: /* ACS will run and later complete */
1211 		return 1;
1212 	case HOSTAPD_CHAN_INVALID:
1213 	default:
1214 		hostapd_notify_bad_chans(iface);
1215 		return -3;
1216 	}
1217 }
1218 
1219 
hostapd_hw_mode_txt(int mode)1220 const char * hostapd_hw_mode_txt(int mode)
1221 {
1222 	switch (mode) {
1223 	case HOSTAPD_MODE_IEEE80211A:
1224 		return "IEEE 802.11a";
1225 	case HOSTAPD_MODE_IEEE80211B:
1226 		return "IEEE 802.11b";
1227 	case HOSTAPD_MODE_IEEE80211G:
1228 		return "IEEE 802.11g";
1229 	case HOSTAPD_MODE_IEEE80211AD:
1230 		return "IEEE 802.11ad";
1231 	default:
1232 		return "UNKNOWN";
1233 	}
1234 }
1235 
1236 
hostapd_hw_get_freq(struct hostapd_data * hapd,int chan)1237 int hostapd_hw_get_freq(struct hostapd_data *hapd, int chan)
1238 {
1239 	return hw_get_freq(hapd->iface->current_mode, chan);
1240 }
1241 
1242 
hostapd_hw_get_channel(struct hostapd_data * hapd,int freq)1243 int hostapd_hw_get_channel(struct hostapd_data *hapd, int freq)
1244 {
1245 	int i, channel;
1246 	struct hostapd_hw_modes *mode;
1247 
1248 	if (hapd->iface->current_mode) {
1249 		channel = hw_get_chan(hapd->iface->current_mode->mode, freq,
1250 				      hapd->iface->hw_features,
1251 				      hapd->iface->num_hw_features);
1252 		if (channel)
1253 			return channel;
1254 	}
1255 
1256 	/* Check other available modes since the channel list for the current
1257 	 * mode did not include the specified frequency. */
1258 	if (!hapd->iface->hw_features)
1259 		return 0;
1260 	for (i = 0; i < hapd->iface->num_hw_features; i++) {
1261 		mode = &hapd->iface->hw_features[i];
1262 		channel = hw_get_chan(mode->mode, freq,
1263 				      hapd->iface->hw_features,
1264 				      hapd->iface->num_hw_features);
1265 		if (channel)
1266 			return channel;
1267 	}
1268 	return 0;
1269 }
1270 
1271 
hostapd_hw_skip_mode(struct hostapd_iface * iface,struct hostapd_hw_modes * mode)1272 int hostapd_hw_skip_mode(struct hostapd_iface *iface,
1273 			 struct hostapd_hw_modes *mode)
1274 {
1275 	int i;
1276 
1277 	if (iface->current_mode)
1278 		return mode != iface->current_mode;
1279 	if (mode->mode != HOSTAPD_MODE_IEEE80211B)
1280 		return 0;
1281 	for (i = 0; i < iface->num_hw_features; i++) {
1282 		if (iface->hw_features[i].mode == HOSTAPD_MODE_IEEE80211G)
1283 			return 1;
1284 	}
1285 	return 0;
1286 }
1287