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