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