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