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 #ifdef __ZEPHYR__
28 #include <supp_events.h>
29 #endif /* __ZEPHYR__ */
30 
31 
hostapd_free_hw_features(struct hostapd_hw_modes * hw_features,size_t num_hw_features)32 void hostapd_free_hw_features(struct hostapd_hw_modes *hw_features,
33 			      size_t num_hw_features)
34 {
35 	size_t i;
36 
37 	if (hw_features == NULL)
38 		return;
39 
40 	for (i = 0; i < num_hw_features; i++) {
41 		os_free(hw_features[i].channels);
42 		os_free(hw_features[i].rates);
43 	}
44 
45 	os_free(hw_features);
46 }
47 
48 
49 #ifndef CONFIG_NO_STDOUT_DEBUG
dfs_info(struct hostapd_channel_data * chan)50 static char * dfs_info(struct hostapd_channel_data *chan)
51 {
52 	static char info[256];
53 	char *state;
54 
55 	switch (chan->flag & HOSTAPD_CHAN_DFS_MASK) {
56 	case HOSTAPD_CHAN_DFS_UNKNOWN:
57 		state = "unknown";
58 		break;
59 	case HOSTAPD_CHAN_DFS_USABLE:
60 		state = "usable";
61 		break;
62 	case HOSTAPD_CHAN_DFS_UNAVAILABLE:
63 		state = "unavailable";
64 		break;
65 	case HOSTAPD_CHAN_DFS_AVAILABLE:
66 		state = "available";
67 		break;
68 	default:
69 		return "";
70 	}
71 	os_snprintf(info, sizeof(info), " (DFS state = %s)", state);
72 	info[sizeof(info) - 1] = '\0';
73 
74 	return info;
75 }
76 #endif /* CONFIG_NO_STDOUT_DEBUG */
77 
78 
hostapd_get_hw_features(struct hostapd_iface * iface)79 int hostapd_get_hw_features(struct hostapd_iface *iface)
80 {
81 	struct hostapd_data *hapd = iface->bss[0];
82 	int i, j;
83 	u16 num_modes, flags;
84 	struct hostapd_hw_modes *modes;
85 	u8 dfs_domain;
86 	enum hostapd_hw_mode mode = HOSTAPD_MODE_IEEE80211ANY;
87 	bool is_6ghz = false;
88 	bool orig_mode_valid = false;
89 
90 	if (hostapd_drv_none(hapd))
91 		return -1;
92 	modes = hostapd_get_hw_feature_data(hapd, &num_modes, &flags,
93 					    &dfs_domain);
94 	if (modes == NULL) {
95 		hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
96 			       HOSTAPD_LEVEL_DEBUG,
97 			       "Fetching hardware channel/rate support not "
98 			       "supported.");
99 		return -1;
100 	}
101 
102 	iface->hw_flags = flags;
103 	iface->dfs_domain = dfs_domain;
104 
105 	if (iface->current_mode) {
106 		/*
107 		 * Received driver event CHANNEL_LIST_CHANGED when the current
108 		 * hw mode is valid. Clear iface->current_mode temporarily as
109 		 * the mode instance will be replaced with a new instance and
110 		 * the current pointer would be pointing to freed memory.
111 		 */
112 		orig_mode_valid = true;
113 		mode = iface->current_mode->mode;
114 		is_6ghz = iface->current_mode->is_6ghz;
115 		iface->current_mode = NULL;
116 	}
117 	hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
118 	iface->hw_features = modes;
119 	iface->num_hw_features = num_modes;
120 
121 	for (i = 0; i < num_modes; i++) {
122 		struct hostapd_hw_modes *feature = &modes[i];
123 		int dfs_enabled = hapd->iconf->ieee80211h &&
124 			(iface->drv_flags & WPA_DRIVER_FLAGS_RADAR);
125 
126 		/* Restore orignal mode if possible */
127 		if (orig_mode_valid && feature->mode == mode &&
128 		    feature->num_channels > 0 &&
129 		    is_6ghz == is_6ghz_freq(feature->channels[0].freq))
130 			iface->current_mode = feature;
131 
132 		/* set flag for channels we can use in current regulatory
133 		 * domain */
134 		for (j = 0; j < feature->num_channels; j++) {
135 			int dfs = 0;
136 
137 			/*
138 			 * Disable all channels that are marked not to allow
139 			 * to initiate radiation (a.k.a. passive scan and no
140 			 * IBSS).
141 			 * Use radar channels only if the driver supports DFS.
142 			 */
143 			if ((feature->channels[j].flag &
144 			     HOSTAPD_CHAN_RADAR) && dfs_enabled) {
145 				dfs = 1;
146 			} else if (((feature->channels[j].flag &
147 				     HOSTAPD_CHAN_RADAR) &&
148 				    !(iface->drv_flags &
149 				      WPA_DRIVER_FLAGS_DFS_OFFLOAD)) ||
150 				   (feature->channels[j].flag &
151 				    HOSTAPD_CHAN_NO_IR)) {
152 				feature->channels[j].flag |=
153 					HOSTAPD_CHAN_DISABLED;
154 			}
155 
156 			if (feature->channels[j].flag & HOSTAPD_CHAN_DISABLED)
157 				continue;
158 
159 			wpa_printf(MSG_MSGDUMP, "Allowed channel: mode=%d "
160 				   "chan=%d freq=%d MHz max_tx_power=%d dBm%s",
161 				   feature->mode,
162 				   feature->channels[j].chan,
163 				   feature->channels[j].freq,
164 				   feature->channels[j].max_tx_power,
165 				   dfs ? dfs_info(&feature->channels[j]) : "");
166 		}
167 	}
168 
169 	if (orig_mode_valid && !iface->current_mode) {
170 		wpa_printf(MSG_ERROR,
171 			   "%s: Could not update iface->current_mode",
172 			   __func__);
173 	}
174 
175 	return 0;
176 }
177 
178 
hostapd_prepare_rates(struct hostapd_iface * iface,struct hostapd_hw_modes * mode)179 int hostapd_prepare_rates(struct hostapd_iface *iface,
180 			  struct hostapd_hw_modes *mode)
181 {
182 	int i, num_basic_rates = 0;
183 	int basic_rates_a[] = { 60, 120, 240, -1 };
184 	int basic_rates_b[] = { 10, 20, -1 };
185 	int basic_rates_g[] = { 10, 20, 55, 110, -1 };
186 	int *basic_rates;
187 
188 	if (iface->conf->basic_rates)
189 		basic_rates = iface->conf->basic_rates;
190 	else switch (mode->mode) {
191 	case HOSTAPD_MODE_IEEE80211A:
192 		basic_rates = basic_rates_a;
193 		break;
194 	case HOSTAPD_MODE_IEEE80211B:
195 		basic_rates = basic_rates_b;
196 		break;
197 	case HOSTAPD_MODE_IEEE80211G:
198 		basic_rates = basic_rates_g;
199 		break;
200 	case HOSTAPD_MODE_IEEE80211AD:
201 		return 0; /* No basic rates for 11ad */
202 	default:
203 		return -1;
204 	}
205 
206 	i = 0;
207 	while (basic_rates[i] >= 0)
208 		i++;
209 	if (i)
210 		i++; /* -1 termination */
211 	os_free(iface->basic_rates);
212 	iface->basic_rates = os_malloc(i * sizeof(int));
213 	if (iface->basic_rates)
214 		os_memcpy(iface->basic_rates, basic_rates, i * sizeof(int));
215 
216 	os_free(iface->current_rates);
217 	iface->num_rates = 0;
218 
219 	iface->current_rates =
220 		os_calloc(mode->num_rates, sizeof(struct hostapd_rate_data));
221 	if (!iface->current_rates) {
222 		wpa_printf(MSG_ERROR, "Failed to allocate memory for rate "
223 			   "table.");
224 		return -1;
225 	}
226 
227 	for (i = 0; i < mode->num_rates; i++) {
228 		struct hostapd_rate_data *rate;
229 
230 		if (iface->conf->supported_rates &&
231 		    !hostapd_rate_found(iface->conf->supported_rates,
232 					mode->rates[i]))
233 			continue;
234 
235 		rate = &iface->current_rates[iface->num_rates];
236 		rate->rate = mode->rates[i];
237 		if (hostapd_rate_found(basic_rates, rate->rate)) {
238 			rate->flags |= HOSTAPD_RATE_BASIC;
239 			num_basic_rates++;
240 		}
241 		wpa_printf(MSG_DEBUG, "RATE[%d] rate=%d flags=0x%x",
242 			   iface->num_rates, rate->rate, rate->flags);
243 		iface->num_rates++;
244 	}
245 
246 	if ((iface->num_rates == 0 || num_basic_rates == 0) &&
247 	    (!iface->conf->ieee80211n || !iface->conf->require_ht)) {
248 		wpa_printf(MSG_ERROR, "No rates remaining in supported/basic "
249 			   "rate sets (%d,%d).",
250 			   iface->num_rates, num_basic_rates);
251 		return -1;
252 	}
253 
254 	return 0;
255 }
256 
257 
ieee80211n_allowed_ht40_channel_pair(struct hostapd_iface * iface)258 static int ieee80211n_allowed_ht40_channel_pair(struct hostapd_iface *iface)
259 {
260 	int pri_freq, sec_freq;
261 	struct hostapd_channel_data *p_chan, *s_chan;
262 
263 	pri_freq = iface->freq;
264 	sec_freq = pri_freq + iface->conf->secondary_channel * 20;
265 
266 	if (!iface->current_mode)
267 		return 0;
268 
269 	p_chan = hw_get_channel_freq(iface->current_mode->mode, pri_freq, NULL,
270 				     iface->hw_features,
271 				     iface->num_hw_features);
272 
273 	s_chan = hw_get_channel_freq(iface->current_mode->mode, sec_freq, NULL,
274 				     iface->hw_features,
275 				     iface->num_hw_features);
276 
277 	return allowed_ht40_channel_pair(iface->current_mode->mode,
278 					 p_chan, s_chan);
279 }
280 
281 
ieee80211n_switch_pri_sec(struct hostapd_iface * iface)282 static void ieee80211n_switch_pri_sec(struct hostapd_iface *iface)
283 {
284 	if (iface->conf->secondary_channel > 0) {
285 		iface->conf->channel += 4;
286 		iface->freq += 20;
287 		iface->conf->secondary_channel = -1;
288 	} else {
289 		iface->conf->channel -= 4;
290 		iface->freq -= 20;
291 		iface->conf->secondary_channel = 1;
292 	}
293 }
294 
295 
ieee80211n_check_40mhz_5g(struct hostapd_iface * iface,struct wpa_scan_results * scan_res)296 static int ieee80211n_check_40mhz_5g(struct hostapd_iface *iface,
297 				     struct wpa_scan_results *scan_res)
298 {
299 	unsigned int pri_freq, sec_freq;
300 	int res;
301 	struct hostapd_channel_data *pri_chan, *sec_chan;
302 
303 	pri_freq = iface->freq;
304 	sec_freq = pri_freq + iface->conf->secondary_channel * 20;
305 
306 	if (!iface->current_mode)
307 		return 0;
308 	pri_chan = hw_get_channel_freq(iface->current_mode->mode, pri_freq,
309 				       NULL, iface->hw_features,
310 				       iface->num_hw_features);
311 	sec_chan = hw_get_channel_freq(iface->current_mode->mode, sec_freq,
312 				       NULL, iface->hw_features,
313 				       iface->num_hw_features);
314 
315 	res = check_40mhz_5g(scan_res, pri_chan, sec_chan);
316 
317 	if (res == 2) {
318 		if (iface->conf->no_pri_sec_switch) {
319 			wpa_printf(MSG_DEBUG,
320 				   "Cannot switch PRI/SEC channels due to local constraint");
321 		} else {
322 			ieee80211n_switch_pri_sec(iface);
323 		}
324 	}
325 
326 	return !!res;
327 }
328 
329 
ieee80211n_check_40mhz_2g4(struct hostapd_iface * iface,struct wpa_scan_results * scan_res)330 static int ieee80211n_check_40mhz_2g4(struct hostapd_iface *iface,
331 				      struct wpa_scan_results *scan_res)
332 {
333 	int pri_chan, sec_chan;
334 
335 	pri_chan = iface->conf->channel;
336 	sec_chan = pri_chan + iface->conf->secondary_channel * 4;
337 
338 	return check_40mhz_2g4(iface->current_mode, scan_res, pri_chan,
339 			       sec_chan);
340 }
341 
342 
ieee80211n_check_scan(struct hostapd_iface * iface)343 static void ieee80211n_check_scan(struct hostapd_iface *iface)
344 {
345 	struct wpa_scan_results *scan_res;
346 	int oper40;
347 	int res = 0;
348 
349 	/* Check list of neighboring BSSes (from scan) to see whether 40 MHz is
350 	 * allowed per IEEE Std 802.11-2012, 10.15.3.2 */
351 
352 	iface->scan_cb = NULL;
353 
354 	scan_res = hostapd_driver_get_scan_results(iface->bss[0]);
355 	if (scan_res == NULL) {
356 		hostapd_setup_interface_complete(iface, 1);
357 		return;
358 	}
359 
360 	if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A)
361 		oper40 = ieee80211n_check_40mhz_5g(iface, scan_res);
362 	else
363 		oper40 = ieee80211n_check_40mhz_2g4(iface, scan_res);
364 	wpa_scan_results_free(scan_res);
365 
366 	iface->secondary_ch = iface->conf->secondary_channel;
367 	if (!oper40) {
368 		wpa_printf(MSG_INFO, "20/40 MHz operation not permitted on "
369 			   "channel pri=%d sec=%d based on overlapping BSSes",
370 			   iface->conf->channel,
371 			   iface->conf->channel +
372 			   iface->conf->secondary_channel * 4);
373 		iface->conf->secondary_channel = 0;
374 		if (iface->drv_flags & WPA_DRIVER_FLAGS_HT_2040_COEX) {
375 			/*
376 			 * TODO: Could consider scheduling another scan to check
377 			 * if channel width can be changed if no coex reports
378 			 * are received from associating stations.
379 			 */
380 		}
381 	}
382 
383 #ifdef CONFIG_IEEE80211AX
384 	if (iface->conf->secondary_channel &&
385 	    iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
386 	    iface->conf->ieee80211ax) {
387 		struct he_capabilities *he_cap;
388 
389 		he_cap = &iface->current_mode->he_capab[IEEE80211_MODE_AP];
390 		if (!(he_cap->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
391 		      HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_IN_2G)) {
392 			wpa_printf(MSG_DEBUG,
393 				   "HE: 40 MHz channel width is not supported in 2.4 GHz; clear secondary channel configuration");
394 			iface->conf->secondary_channel = 0;
395 		}
396 	}
397 #endif /* CONFIG_IEEE80211AX */
398 
399 	if (iface->conf->secondary_channel)
400 		res = ieee80211n_allowed_ht40_channel_pair(iface);
401 	if (!res) {
402 		iface->conf->secondary_channel = 0;
403 		hostapd_set_oper_centr_freq_seg0_idx(iface->conf, 0);
404 		hostapd_set_oper_centr_freq_seg1_idx(iface->conf, 0);
405 		hostapd_set_oper_chwidth(iface->conf, CONF_OPER_CHWIDTH_USE_HT);
406 		res = 1;
407 		wpa_printf(MSG_INFO, "Fallback to 20 MHz");
408 	}
409 
410 	hostapd_setup_interface_complete(iface, !res);
411 }
412 
413 
ieee80211n_scan_channels_2g4(struct hostapd_iface * iface,struct wpa_driver_scan_params * params)414 static void ieee80211n_scan_channels_2g4(struct hostapd_iface *iface,
415 					 struct wpa_driver_scan_params *params)
416 {
417 	/* Scan only the affected frequency range */
418 	int pri_freq, sec_freq;
419 	int affected_start, affected_end;
420 	int i, pos;
421 	struct hostapd_hw_modes *mode;
422 
423 	if (iface->current_mode == NULL)
424 		return;
425 
426 	pri_freq = iface->freq;
427 	if (iface->conf->secondary_channel > 0)
428 		sec_freq = pri_freq + 20;
429 	else
430 		sec_freq = pri_freq - 20;
431 	/*
432 	 * Note: Need to find the PRI channel also in cases where the affected
433 	 * channel is the SEC channel of a 40 MHz BSS, so need to include the
434 	 * scanning coverage here to be 40 MHz from the center frequency.
435 	 */
436 	affected_start = (pri_freq + sec_freq) / 2 - 40;
437 	affected_end = (pri_freq + sec_freq) / 2 + 40;
438 	wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
439 		   affected_start, affected_end);
440 
441 	mode = iface->current_mode;
442 	params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
443 	if (params->freqs == NULL)
444 		return;
445 	pos = 0;
446 
447 	for (i = 0; i < mode->num_channels; i++) {
448 		struct hostapd_channel_data *chan = &mode->channels[i];
449 		if (chan->flag & HOSTAPD_CHAN_DISABLED)
450 			continue;
451 		if (chan->freq < affected_start ||
452 		    chan->freq > affected_end)
453 			continue;
454 		params->freqs[pos++] = chan->freq;
455 	}
456 }
457 
458 
ieee80211n_scan_channels_5g(struct hostapd_iface * iface,struct wpa_driver_scan_params * params)459 static void ieee80211n_scan_channels_5g(struct hostapd_iface *iface,
460 					struct wpa_driver_scan_params *params)
461 {
462 	/* Scan only the affected frequency range */
463 	int pri_freq;
464 	int affected_start, affected_end;
465 	int i, pos;
466 	struct hostapd_hw_modes *mode;
467 
468 	if (iface->current_mode == NULL)
469 		return;
470 
471 	pri_freq = iface->freq;
472 	if (iface->conf->secondary_channel > 0) {
473 		affected_start = pri_freq - 10;
474 		affected_end = pri_freq + 30;
475 	} else {
476 		affected_start = pri_freq - 30;
477 		affected_end = pri_freq + 10;
478 	}
479 	wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
480 		   affected_start, affected_end);
481 
482 	mode = iface->current_mode;
483 	params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
484 	if (params->freqs == NULL)
485 		return;
486 	pos = 0;
487 
488 	for (i = 0; i < mode->num_channels; i++) {
489 		struct hostapd_channel_data *chan = &mode->channels[i];
490 		if (chan->flag & HOSTAPD_CHAN_DISABLED)
491 			continue;
492 		if (chan->freq < affected_start ||
493 		    chan->freq > affected_end)
494 			continue;
495 		params->freqs[pos++] = chan->freq;
496 	}
497 }
498 
499 
ap_ht40_scan_retry(void * eloop_data,void * user_data)500 static void ap_ht40_scan_retry(void *eloop_data, void *user_data)
501 {
502 #define HT2040_COEX_SCAN_RETRY 15
503 	struct hostapd_iface *iface = eloop_data;
504 	struct wpa_driver_scan_params params;
505 	int ret;
506 
507 	os_memset(&params, 0, sizeof(params));
508 	if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
509 		ieee80211n_scan_channels_2g4(iface, &params);
510 	else
511 		ieee80211n_scan_channels_5g(iface, &params);
512 
513 	params.link_id = -1;
514 #ifdef CONFIG_IEEE80211BE
515 	if (iface->bss[0]->conf->mld_ap)
516 		params.link_id = iface->bss[0]->mld_link_id;
517 #endif /* CONFIG_IEEE80211BE */
518 
519 	ret = hostapd_driver_scan(iface->bss[0], &params);
520 	iface->num_ht40_scan_tries++;
521 	os_free(params.freqs);
522 
523 	if (ret == -EBUSY &&
524 	    iface->num_ht40_scan_tries < HT2040_COEX_SCAN_RETRY) {
525 		wpa_printf(MSG_ERROR,
526 			   "Failed to request a scan of neighboring BSSes ret=%d (%s) - try to scan again (attempt %d)",
527 			   ret, strerror(-ret), iface->num_ht40_scan_tries);
528 		eloop_register_timeout(1, 0, ap_ht40_scan_retry, iface, NULL);
529 		return;
530 	}
531 
532 	if (ret == 0) {
533 		iface->scan_cb = ieee80211n_check_scan;
534 		iface->bss[0]->scan_cookie = params.scan_cookie;
535 		return;
536 	}
537 
538 	wpa_printf(MSG_DEBUG,
539 		   "Failed to request a scan in device, bringing up in HT20 mode");
540 	iface->conf->secondary_channel = 0;
541 	iface->conf->ht_capab &= ~HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
542 	hostapd_setup_interface_complete(iface, 0);
543 }
544 
545 
hostapd_stop_setup_timers(struct hostapd_iface * iface)546 void hostapd_stop_setup_timers(struct hostapd_iface *iface)
547 {
548 	eloop_cancel_timeout(ap_ht40_scan_retry, iface, NULL);
549 }
550 
551 
ieee80211n_check_40mhz(struct hostapd_iface * iface)552 static int ieee80211n_check_40mhz(struct hostapd_iface *iface)
553 {
554 	struct wpa_driver_scan_params params;
555 	int ret;
556 
557 	/* Check that HT40 is used and PRI / SEC switch is allowed */
558 	if (!iface->conf->secondary_channel || iface->conf->no_pri_sec_switch)
559 		return 0;
560 
561 	hostapd_set_state(iface, HAPD_IFACE_HT_SCAN);
562 	wpa_printf(MSG_DEBUG, "Scan for neighboring BSSes prior to enabling "
563 		   "40 MHz channel");
564 	os_memset(&params, 0, sizeof(params));
565 	if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
566 		ieee80211n_scan_channels_2g4(iface, &params);
567 	else
568 		ieee80211n_scan_channels_5g(iface, &params);
569 
570 	params.link_id = -1;
571 #ifdef CONFIG_IEEE80211BE
572 	if (iface->bss[0]->conf->mld_ap)
573 		params.link_id = iface->bss[0]->mld_link_id;
574 #endif /* CONFIG_IEEE80211BE */
575 	ret = hostapd_driver_scan(iface->bss[0], &params);
576 	os_free(params.freqs);
577 
578 	if (ret == -EBUSY) {
579 		wpa_printf(MSG_ERROR,
580 			   "Failed to request a scan of neighboring BSSes ret=%d (%s) - try to scan again",
581 			   ret, strerror(-ret));
582 		iface->num_ht40_scan_tries = 1;
583 		eloop_cancel_timeout(ap_ht40_scan_retry, iface, NULL);
584 		eloop_register_timeout(1, 0, ap_ht40_scan_retry, iface, NULL);
585 		return 1;
586 	}
587 
588 	if (ret < 0) {
589 		wpa_printf(MSG_ERROR,
590 			   "Failed to request a scan of neighboring BSSes ret=%d (%s)",
591 			   ret, strerror(-ret));
592 		return -1;
593 	}
594 
595 	iface->scan_cb = ieee80211n_check_scan;
596 	iface->bss[0]->scan_cookie = params.scan_cookie;
597 	return 1;
598 }
599 
600 
ieee80211n_supported_ht_capab(struct hostapd_iface * iface)601 static int ieee80211n_supported_ht_capab(struct hostapd_iface *iface)
602 {
603 	u16 hw = iface->current_mode->ht_capab;
604 	u16 conf = iface->conf->ht_capab;
605 
606 	if ((conf & HT_CAP_INFO_LDPC_CODING_CAP) &&
607 	    !(hw & HT_CAP_INFO_LDPC_CODING_CAP)) {
608 		wpa_printf(MSG_ERROR, "Driver does not support configured "
609 			   "HT capability [LDPC]");
610 		return 0;
611 	}
612 
613 	/*
614 	 * Driver ACS chosen channel may not be HT40 due to internal driver
615 	 * restrictions.
616 	 */
617 	if (!iface->conf->acs && (conf & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) &&
618 	    !(hw & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
619 		wpa_printf(MSG_ERROR, "Driver does not support configured "
620 			   "HT capability [HT40*]");
621 		return 0;
622 	}
623 
624 	if ((conf & HT_CAP_INFO_GREEN_FIELD) &&
625 	    !(hw & HT_CAP_INFO_GREEN_FIELD)) {
626 		wpa_printf(MSG_ERROR, "Driver does not support configured "
627 			   "HT capability [GF]");
628 		return 0;
629 	}
630 
631 	if ((conf & HT_CAP_INFO_SHORT_GI20MHZ) &&
632 	    !(hw & HT_CAP_INFO_SHORT_GI20MHZ)) {
633 		wpa_printf(MSG_ERROR, "Driver does not support configured "
634 			   "HT capability [SHORT-GI-20]");
635 		return 0;
636 	}
637 
638 	if ((conf & HT_CAP_INFO_SHORT_GI40MHZ) &&
639 	    !(hw & HT_CAP_INFO_SHORT_GI40MHZ)) {
640 		wpa_printf(MSG_ERROR, "Driver does not support configured "
641 			   "HT capability [SHORT-GI-40]");
642 		return 0;
643 	}
644 
645 	if ((conf & HT_CAP_INFO_TX_STBC) && !(hw & HT_CAP_INFO_TX_STBC)) {
646 		wpa_printf(MSG_ERROR, "Driver does not support configured "
647 			   "HT capability [TX-STBC]");
648 		return 0;
649 	}
650 
651 	if ((conf & HT_CAP_INFO_RX_STBC_MASK) >
652 	    (hw & HT_CAP_INFO_RX_STBC_MASK)) {
653 		wpa_printf(MSG_ERROR, "Driver does not support configured "
654 			   "HT capability [RX-STBC*]");
655 		return 0;
656 	}
657 
658 	if ((conf & HT_CAP_INFO_DELAYED_BA) &&
659 	    !(hw & HT_CAP_INFO_DELAYED_BA)) {
660 		wpa_printf(MSG_ERROR, "Driver does not support configured "
661 			   "HT capability [DELAYED-BA]");
662 		return 0;
663 	}
664 
665 	if ((conf & HT_CAP_INFO_MAX_AMSDU_SIZE) &&
666 	    !(hw & HT_CAP_INFO_MAX_AMSDU_SIZE)) {
667 		wpa_printf(MSG_ERROR, "Driver does not support configured "
668 			   "HT capability [MAX-AMSDU-7935]");
669 		return 0;
670 	}
671 
672 	if ((conf & HT_CAP_INFO_DSSS_CCK40MHZ) &&
673 	    !(hw & HT_CAP_INFO_DSSS_CCK40MHZ)) {
674 		wpa_printf(MSG_ERROR, "Driver does not support configured "
675 			   "HT capability [DSSS_CCK-40]");
676 		return 0;
677 	}
678 
679 	if ((conf & HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT) &&
680 	    !(hw & HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT)) {
681 		wpa_printf(MSG_ERROR, "Driver does not support configured "
682 			   "HT capability [LSIG-TXOP-PROT]");
683 		return 0;
684 	}
685 
686 	return 1;
687 }
688 
689 
690 #ifdef CONFIG_IEEE80211AC
ieee80211ac_supported_vht_capab(struct hostapd_iface * iface)691 static int ieee80211ac_supported_vht_capab(struct hostapd_iface *iface)
692 {
693 	struct hostapd_hw_modes *mode = iface->current_mode;
694 	u32 hw = mode->vht_capab;
695 	u32 conf = iface->conf->vht_capab;
696 
697 	wpa_printf(MSG_DEBUG, "hw vht capab: 0x%x, conf vht capab: 0x%x",
698 		   hw, conf);
699 
700 	if (mode->mode == HOSTAPD_MODE_IEEE80211G &&
701 	    iface->conf->bss[0]->vendor_vht &&
702 	    mode->vht_capab == 0 && iface->hw_features) {
703 		int i;
704 
705 		for (i = 0; i < iface->num_hw_features; i++) {
706 			if (iface->hw_features[i].mode ==
707 			    HOSTAPD_MODE_IEEE80211A) {
708 				mode = &iface->hw_features[i];
709 				hw = mode->vht_capab;
710 				wpa_printf(MSG_DEBUG,
711 					   "update hw vht capab based on 5 GHz band: 0x%x",
712 					   hw);
713 				break;
714 			}
715 		}
716 	}
717 
718 	return ieee80211ac_cap_check(hw, conf);
719 }
720 #endif /* CONFIG_IEEE80211AC */
721 
722 
723 #ifdef CONFIG_IEEE80211AX
ieee80211ax_supported_he_capab(struct hostapd_iface * iface)724 static int ieee80211ax_supported_he_capab(struct hostapd_iface *iface)
725 {
726 	return 1;
727 }
728 #endif /* CONFIG_IEEE80211AX */
729 
730 
hostapd_check_ht_capab(struct hostapd_iface * iface)731 int hostapd_check_ht_capab(struct hostapd_iface *iface)
732 {
733 	int ret;
734 
735 	if (is_6ghz_freq(iface->freq))
736 		return 0;
737 	if (!iface->conf->ieee80211n)
738 		return 0;
739 
740 	if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211B &&
741 	    iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G &&
742 	    (iface->conf->ht_capab & HT_CAP_INFO_DSSS_CCK40MHZ)) {
743 		wpa_printf(MSG_DEBUG,
744 			   "Disable HT capability [DSSS_CCK-40] on 5 GHz band");
745 		iface->conf->ht_capab &= ~HT_CAP_INFO_DSSS_CCK40MHZ;
746 	}
747 
748 	if (!ieee80211n_supported_ht_capab(iface))
749 		return -1;
750 #ifdef CONFIG_IEEE80211AX
751 	if (iface->conf->ieee80211ax &&
752 	    !ieee80211ax_supported_he_capab(iface))
753 		return -1;
754 #endif /* CONFIG_IEEE80211AX */
755 #ifdef CONFIG_IEEE80211AC
756 	if (iface->conf->ieee80211ac &&
757 	    !ieee80211ac_supported_vht_capab(iface))
758 		return -1;
759 #endif /* CONFIG_IEEE80211AC */
760 	ret = ieee80211n_check_40mhz(iface);
761 	if (ret)
762 		return ret;
763 	if (!ieee80211n_allowed_ht40_channel_pair(iface))
764 		return -1;
765 
766 	return 0;
767 }
768 
769 
hostapd_check_edmg_capab(struct hostapd_iface * iface)770 int hostapd_check_edmg_capab(struct hostapd_iface *iface)
771 {
772 	struct hostapd_hw_modes *mode = iface->hw_features;
773 	struct ieee80211_edmg_config edmg;
774 
775 	if (!iface->conf->enable_edmg)
776 		return 0;
777 
778 	hostapd_encode_edmg_chan(iface->conf->enable_edmg,
779 				 iface->conf->edmg_channel,
780 				 iface->conf->channel,
781 				 &edmg);
782 
783 	if (mode->edmg.channels && ieee802_edmg_is_allowed(mode->edmg, edmg))
784 		return 0;
785 
786 	wpa_printf(MSG_WARNING, "Requested EDMG configuration is not valid");
787 	wpa_printf(MSG_INFO, "EDMG capab: channels 0x%x, bw_config %d",
788 		   mode->edmg.channels, mode->edmg.bw_config);
789 	wpa_printf(MSG_INFO,
790 		   "Requested EDMG configuration: channels 0x%x, bw_config %d",
791 		   edmg.channels, edmg.bw_config);
792 	return -1;
793 }
794 
795 
hostapd_check_he_6ghz_capab(struct hostapd_iface * iface)796 int hostapd_check_he_6ghz_capab(struct hostapd_iface *iface)
797 {
798 #ifdef CONFIG_IEEE80211AX
799 	struct he_capabilities *he_cap;
800 	u16 hw;
801 
802 	if (!iface->current_mode || !is_6ghz_freq(iface->freq))
803 		return 0;
804 
805 	he_cap = &iface->current_mode->he_capab[IEEE80211_MODE_AP];
806 	hw = he_cap->he_6ghz_capa;
807 	if (iface->conf->he_6ghz_max_mpdu >
808 	    ((hw & HE_6GHZ_BAND_CAP_MAX_MPDU_LEN_MASK) >>
809 	     HE_6GHZ_BAND_CAP_MAX_MPDU_LEN_SHIFT)) {
810 		wpa_printf(MSG_ERROR,
811 			   "The driver does not support the configured HE 6 GHz Max MPDU length");
812 		return -1;
813 	}
814 
815 	if (iface->conf->he_6ghz_max_ampdu_len_exp >
816 	    ((hw & HE_6GHZ_BAND_CAP_MAX_AMPDU_LEN_EXP_MASK) >>
817 	     HE_6GHZ_BAND_CAP_MAX_AMPDU_LEN_EXP_SHIFT)) {
818 		wpa_printf(MSG_ERROR,
819 			   "The driver does not support the configured HE 6 GHz Max AMPDU Length Exponent");
820 		return -1;
821 	}
822 
823 	if (iface->conf->he_6ghz_rx_ant_pat &&
824 	    !(hw & HE_6GHZ_BAND_CAP_RX_ANTPAT_CONS)) {
825 		wpa_printf(MSG_ERROR,
826 			   "The driver does not support the configured HE 6 GHz Rx Antenna Pattern");
827 		return -1;
828 	}
829 
830 	if (iface->conf->he_6ghz_tx_ant_pat &&
831 	    !(hw & HE_6GHZ_BAND_CAP_TX_ANTPAT_CONS)) {
832 		wpa_printf(MSG_ERROR,
833 			   "The driver does not support the configured HE 6 GHz Tx Antenna Pattern");
834 		return -1;
835 	}
836 #endif /* CONFIG_IEEE80211AX */
837 	return 0;
838 }
839 
840 
841 /* Returns:
842  * 1 = usable
843  * 0 = not usable
844  * -1 = not currently usable due to 6 GHz NO-IR
845  */
hostapd_is_usable_chan(struct hostapd_iface * iface,int frequency,int primary)846 static int hostapd_is_usable_chan(struct hostapd_iface *iface,
847 				  int frequency, int primary)
848 {
849 	struct hostapd_channel_data *chan;
850 
851 	if (!iface->current_mode)
852 		return 0;
853 
854 	chan = hw_get_channel_freq(iface->current_mode->mode, frequency, NULL,
855 				   iface->hw_features, iface->num_hw_features);
856 	if (!chan)
857 		return 0;
858 
859 	if ((primary && chan_pri_allowed(chan)) ||
860 	    (!primary && !(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 #ifdef __ZEPHYR__
1144 			supplicant_send_wifi_mgmt_ap_status(iface->owner,
1145 				NET_EVENT_WIFI_CMD_AP_ENABLE_RESULT,
1146 				WIFI_STATUS_AP_CHANNEL_NOT_ALLOWED);
1147 #endif /* __ZEPHYR__ */
1148 			if (!err)
1149 				return HOSTAPD_CHAN_INVALID;
1150 			return HOSTAPD_CHAN_INVALID_NO_IR;
1151 		}
1152 		return HOSTAPD_CHAN_VALID;
1153 	}
1154 
1155 	/*
1156 	 * The user set channel=0 or channel=acs_survey
1157 	 * which is used to trigger ACS.
1158 	 */
1159 
1160 	switch (acs_init(iface)) {
1161 	case HOSTAPD_CHAN_ACS:
1162 		return HOSTAPD_CHAN_ACS;
1163 	case HOSTAPD_CHAN_INVALID_NO_IR:
1164 		return HOSTAPD_CHAN_INVALID_NO_IR;
1165 	case HOSTAPD_CHAN_VALID:
1166 	case HOSTAPD_CHAN_INVALID:
1167 	default:
1168 #ifdef __ZEPHYR__
1169 		supplicant_send_wifi_mgmt_ap_status(iface->owner,
1170 			NET_EVENT_WIFI_CMD_AP_ENABLE_RESULT,
1171 			WIFI_STATUS_AP_CHANNEL_NOT_ALLOWED);
1172 #endif /* __ZEPHYR__ */
1173 		return HOSTAPD_CHAN_INVALID;
1174 	}
1175 }
1176 
1177 
hostapd_notify_bad_chans(struct hostapd_iface * iface)1178 static void hostapd_notify_bad_chans(struct hostapd_iface *iface)
1179 {
1180 	if (!iface->current_mode) {
1181 		hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
1182 			       HOSTAPD_LEVEL_WARNING,
1183 			       "Hardware does not support configured mode");
1184 		return;
1185 	}
1186 	hostapd_logger(iface->bss[0], NULL,
1187 		       HOSTAPD_MODULE_IEEE80211,
1188 		       HOSTAPD_LEVEL_WARNING,
1189 		       "Configured channel (%d) or frequency (%d) (secondary_channel=%d) not found from the channel list of the current mode (%d) %s",
1190 		       iface->conf->channel,
1191 		       iface->freq, iface->conf->secondary_channel,
1192 		       iface->current_mode->mode,
1193 		       hostapd_hw_mode_txt(iface->current_mode->mode));
1194 	hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
1195 		       HOSTAPD_LEVEL_WARNING,
1196 		       "Hardware does not support configured channel");
1197 }
1198 
1199 
hostapd_acs_completed(struct hostapd_iface * iface,int err)1200 int hostapd_acs_completed(struct hostapd_iface *iface, int err)
1201 {
1202 	int ret = -1;
1203 
1204 	if (err)
1205 		goto out;
1206 
1207 	switch (hostapd_check_chans(iface)) {
1208 	case HOSTAPD_CHAN_VALID:
1209 		iface->is_no_ir = false;
1210 		wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO,
1211 			ACS_EVENT_COMPLETED "freq=%d channel=%d",
1212 			iface->freq, iface->conf->channel);
1213 		break;
1214 	case HOSTAPD_CHAN_ACS:
1215 		wpa_printf(MSG_ERROR, "ACS error - reported complete, but no result available");
1216 		wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, ACS_EVENT_FAILED);
1217 		hostapd_notify_bad_chans(iface);
1218 		goto out;
1219 	case HOSTAPD_CHAN_INVALID_NO_IR:
1220 		iface->is_no_ir = true;
1221 		/* fall through */
1222 	case HOSTAPD_CHAN_INVALID:
1223 	default:
1224 		wpa_printf(MSG_ERROR, "ACS picked unusable channels");
1225 		wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, ACS_EVENT_FAILED);
1226 		hostapd_notify_bad_chans(iface);
1227 		goto out;
1228 	}
1229 
1230 	ret = hostapd_check_ht_capab(iface);
1231 	if (ret < 0)
1232 		goto out;
1233 	if (ret == 1) {
1234 		wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback");
1235 		return 0;
1236 	}
1237 
1238 	ret = 0;
1239 out:
1240 	return hostapd_setup_interface_complete(iface, ret);
1241 }
1242 
1243 
1244 /**
1245  * hostapd_csa_update_hwmode - Update hardware mode
1246  * @iface: Pointer to interface data.
1247  * Returns: 0 on success, < 0 on failure
1248  *
1249  * Update hardware mode when the operating channel changed because of CSA.
1250  */
hostapd_csa_update_hwmode(struct hostapd_iface * iface)1251 int hostapd_csa_update_hwmode(struct hostapd_iface *iface)
1252 {
1253 	if (!iface || !iface->conf)
1254 		return -1;
1255 
1256 	iface->current_mode = NULL;
1257 	iface->conf->hw_mode = HOSTAPD_MODE_IEEE80211ANY;
1258 
1259 	return hostapd_determine_mode(iface);
1260 }
1261 
1262 
1263 /**
1264  * hostapd_select_hw_mode - Select the hardware mode
1265  * @iface: Pointer to interface data.
1266  * Returns: 0 on success, < 0 on failure
1267  *
1268  * Sets up the hardware mode, channel, rates, and passive scanning
1269  * based on the configuration.
1270  */
hostapd_select_hw_mode(struct hostapd_iface * iface)1271 int hostapd_select_hw_mode(struct hostapd_iface *iface)
1272 {
1273 	int i;
1274 
1275 	if (iface->num_hw_features < 1)
1276 		return -1;
1277 
1278 	if ((iface->conf->hw_mode == HOSTAPD_MODE_IEEE80211G ||
1279 	     iface->conf->ieee80211n || iface->conf->ieee80211ac ||
1280 	     iface->conf->ieee80211ax || iface->conf->ieee80211be) &&
1281 	    iface->conf->channel == 14) {
1282 		wpa_printf(MSG_INFO, "Disable OFDM/HT/VHT/HE/EHT on channel 14");
1283 		iface->conf->hw_mode = HOSTAPD_MODE_IEEE80211B;
1284 		iface->conf->ieee80211n = 0;
1285 		iface->conf->ieee80211ac = 0;
1286 		iface->conf->ieee80211ax = 0;
1287 		iface->conf->ieee80211be = 0;
1288 	}
1289 
1290 	iface->current_mode = NULL;
1291 	for (i = 0; i < iface->num_hw_features; i++) {
1292 		struct hostapd_hw_modes *mode = &iface->hw_features[i];
1293 
1294 		if (mode->mode == iface->conf->hw_mode) {
1295 			if (skip_mode(iface, mode))
1296 				continue;
1297 
1298 			iface->current_mode = mode;
1299 			break;
1300 		}
1301 	}
1302 
1303 	if (iface->current_mode == NULL) {
1304 		if ((iface->drv_flags & WPA_DRIVER_FLAGS_ACS_OFFLOAD) &&
1305 		    (iface->drv_flags & WPA_DRIVER_FLAGS_SUPPORT_HW_MODE_ANY)) {
1306 			wpa_printf(MSG_DEBUG,
1307 				   "Using offloaded hw_mode=any ACS");
1308 		} else if (!(iface->drv_flags & WPA_DRIVER_FLAGS_ACS_OFFLOAD) &&
1309 			   iface->conf->hw_mode == HOSTAPD_MODE_IEEE80211ANY) {
1310 			wpa_printf(MSG_DEBUG,
1311 				   "Using internal ACS for hw_mode=any");
1312 		} else {
1313 			wpa_printf(MSG_ERROR,
1314 				   "Hardware does not support configured mode");
1315 			hostapd_logger(iface->bss[0], NULL,
1316 				       HOSTAPD_MODULE_IEEE80211,
1317 				       HOSTAPD_LEVEL_WARNING,
1318 				       "Hardware does not support configured mode (%d) (hw_mode in hostapd.conf)",
1319 				       (int) iface->conf->hw_mode);
1320 #ifdef __ZEPHYR__
1321 			supplicant_send_wifi_mgmt_ap_status(iface->owner,
1322 				NET_EVENT_WIFI_CMD_AP_ENABLE_RESULT,
1323 				WIFI_STATUS_AP_CHANNEL_NOT_SUPPORTED);
1324 #endif /* __ZEPHYR__ */
1325 			return -2;
1326 		}
1327 	}
1328 
1329 	switch (hostapd_check_chans(iface)) {
1330 	case HOSTAPD_CHAN_VALID:
1331 		iface->is_no_ir = false;
1332 		return 0;
1333 	case HOSTAPD_CHAN_ACS: /* ACS will run and later complete */
1334 		return 1;
1335 	case HOSTAPD_CHAN_INVALID_NO_IR:
1336 		iface->is_no_ir = true;
1337 		/* fall through */
1338 	case HOSTAPD_CHAN_INVALID:
1339 	default:
1340 		hostapd_notify_bad_chans(iface);
1341 		return -3;
1342 	}
1343 }
1344 
1345 
hostapd_hw_mode_txt(int mode)1346 const char * hostapd_hw_mode_txt(int mode)
1347 {
1348 	switch (mode) {
1349 	case HOSTAPD_MODE_IEEE80211A:
1350 		return "IEEE 802.11a";
1351 	case HOSTAPD_MODE_IEEE80211B:
1352 		return "IEEE 802.11b";
1353 	case HOSTAPD_MODE_IEEE80211G:
1354 		return "IEEE 802.11g";
1355 	case HOSTAPD_MODE_IEEE80211AD:
1356 		return "IEEE 802.11ad";
1357 	default:
1358 		return "UNKNOWN";
1359 	}
1360 }
1361 
1362 
hostapd_hw_get_freq(struct hostapd_data * hapd,int chan)1363 int hostapd_hw_get_freq(struct hostapd_data *hapd, int chan)
1364 {
1365 	return hw_get_freq(hapd->iface->current_mode, chan);
1366 }
1367 
1368 
hostapd_hw_get_channel(struct hostapd_data * hapd,int freq)1369 int hostapd_hw_get_channel(struct hostapd_data *hapd, int freq)
1370 {
1371 	int i, channel;
1372 	struct hostapd_hw_modes *mode;
1373 
1374 	if (hapd->iface->current_mode) {
1375 		channel = hw_get_chan(hapd->iface->current_mode->mode, freq,
1376 				      hapd->iface->hw_features,
1377 				      hapd->iface->num_hw_features);
1378 		if (channel)
1379 			return channel;
1380 	}
1381 
1382 	/* Check other available modes since the channel list for the current
1383 	 * mode did not include the specified frequency. */
1384 	if (!hapd->iface->hw_features)
1385 		return 0;
1386 	for (i = 0; i < hapd->iface->num_hw_features; i++) {
1387 		mode = &hapd->iface->hw_features[i];
1388 		channel = hw_get_chan(mode->mode, freq,
1389 				      hapd->iface->hw_features,
1390 				      hapd->iface->num_hw_features);
1391 		if (channel)
1392 			return channel;
1393 	}
1394 	return 0;
1395 }
1396 
1397 
hostapd_hw_skip_mode(struct hostapd_iface * iface,struct hostapd_hw_modes * mode)1398 int hostapd_hw_skip_mode(struct hostapd_iface *iface,
1399 			 struct hostapd_hw_modes *mode)
1400 {
1401 	int i;
1402 
1403 	if (iface->current_mode)
1404 		return mode != iface->current_mode;
1405 	if (mode->mode != HOSTAPD_MODE_IEEE80211B)
1406 		return 0;
1407 	for (i = 0; i < iface->num_hw_features; i++) {
1408 		if (iface->hw_features[i].mode == HOSTAPD_MODE_IEEE80211G)
1409 			return 1;
1410 	}
1411 	return 0;
1412 }
1413