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