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
28void 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
46static 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
75int 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
82	if (hostapd_drv_none(hapd))
83		return -1;
84	modes = hostapd_get_hw_feature_data(hapd, &num_modes, &flags);
85	if (modes == NULL) {
86		hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
87			       HOSTAPD_LEVEL_DEBUG,
88			       "Fetching hardware channel/rate support not "
89			       "supported.");
90		return -1;
91	}
92
93	iface->hw_flags = flags;
94
95	hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
96	iface->hw_features = modes;
97	iface->num_hw_features = num_modes;
98
99	for (i = 0; i < num_modes; i++) {
100		struct hostapd_hw_modes *feature = &modes[i];
101		int dfs_enabled = hapd->iconf->ieee80211h &&
102			(iface->drv_flags & WPA_DRIVER_FLAGS_RADAR);
103
104		/* set flag for channels we can use in current regulatory
105		 * domain */
106		for (j = 0; j < feature->num_channels; j++) {
107			int dfs = 0;
108
109			/*
110			 * Disable all channels that are marked not to allow
111			 * to initiate radiation (a.k.a. passive scan and no
112			 * IBSS).
113			 * Use radar channels only if the driver supports DFS.
114			 */
115			if ((feature->channels[j].flag &
116			     HOSTAPD_CHAN_RADAR) && dfs_enabled) {
117				dfs = 1;
118			} else if (((feature->channels[j].flag &
119				     HOSTAPD_CHAN_RADAR) &&
120				    !(iface->drv_flags &
121				      WPA_DRIVER_FLAGS_DFS_OFFLOAD)) ||
122				   (feature->channels[j].flag &
123				    HOSTAPD_CHAN_NO_IR)) {
124				feature->channels[j].flag |=
125					HOSTAPD_CHAN_DISABLED;
126			}
127
128			if (feature->channels[j].flag & HOSTAPD_CHAN_DISABLED)
129				continue;
130
131			wpa_printf(MSG_MSGDUMP, "Allowed channel: mode=%d "
132				   "chan=%d freq=%d MHz max_tx_power=%d dBm%s",
133				   feature->mode,
134				   feature->channels[j].chan,
135				   feature->channels[j].freq,
136				   feature->channels[j].max_tx_power,
137				   dfs ? dfs_info(&feature->channels[j]) : "");
138		}
139	}
140
141	return 0;
142}
143
144
145int hostapd_prepare_rates(struct hostapd_iface *iface,
146			  struct hostapd_hw_modes *mode)
147{
148	int i, num_basic_rates = 0;
149	int basic_rates_a[] = { 60, 120, 240, -1 };
150	int basic_rates_b[] = { 10, 20, -1 };
151	int basic_rates_g[] = { 10, 20, 55, 110, -1 };
152	int *basic_rates;
153
154	if (iface->conf->basic_rates)
155		basic_rates = iface->conf->basic_rates;
156	else switch (mode->mode) {
157	case HOSTAPD_MODE_IEEE80211A:
158		basic_rates = basic_rates_a;
159		break;
160	case HOSTAPD_MODE_IEEE80211B:
161		basic_rates = basic_rates_b;
162		break;
163	case HOSTAPD_MODE_IEEE80211G:
164		basic_rates = basic_rates_g;
165		break;
166	case HOSTAPD_MODE_IEEE80211AD:
167		return 0; /* No basic rates for 11ad */
168	default:
169		return -1;
170	}
171
172	i = 0;
173	while (basic_rates[i] >= 0)
174		i++;
175	if (i)
176		i++; /* -1 termination */
177	os_free(iface->basic_rates);
178	iface->basic_rates = os_malloc(i * sizeof(int));
179	if (iface->basic_rates)
180		os_memcpy(iface->basic_rates, basic_rates, i * sizeof(int));
181
182	os_free(iface->current_rates);
183	iface->num_rates = 0;
184
185	iface->current_rates =
186		os_calloc(mode->num_rates, sizeof(struct hostapd_rate_data));
187	if (!iface->current_rates) {
188		wpa_printf(MSG_ERROR, "Failed to allocate memory for rate "
189			   "table.");
190		return -1;
191	}
192
193	for (i = 0; i < mode->num_rates; i++) {
194		struct hostapd_rate_data *rate;
195
196		if (iface->conf->supported_rates &&
197		    !hostapd_rate_found(iface->conf->supported_rates,
198					mode->rates[i]))
199			continue;
200
201		rate = &iface->current_rates[iface->num_rates];
202		rate->rate = mode->rates[i];
203		if (hostapd_rate_found(basic_rates, rate->rate)) {
204			rate->flags |= HOSTAPD_RATE_BASIC;
205			num_basic_rates++;
206		}
207		wpa_printf(MSG_DEBUG, "RATE[%d] rate=%d flags=0x%x",
208			   iface->num_rates, rate->rate, rate->flags);
209		iface->num_rates++;
210	}
211
212	if ((iface->num_rates == 0 || num_basic_rates == 0) &&
213	    (!iface->conf->ieee80211n || !iface->conf->require_ht)) {
214		wpa_printf(MSG_ERROR, "No rates remaining in supported/basic "
215			   "rate sets (%d,%d).",
216			   iface->num_rates, num_basic_rates);
217		return -1;
218	}
219
220	return 0;
221}
222
223
224#ifdef CONFIG_IEEE80211N
225static int ieee80211n_allowed_ht40_channel_pair(struct hostapd_iface *iface)
226{
227	int pri_chan, sec_chan;
228
229	if (!iface->conf->secondary_channel)
230		return 1; /* HT40 not used */
231
232	pri_chan = iface->conf->channel;
233	sec_chan = pri_chan + iface->conf->secondary_channel * 4;
234
235	return allowed_ht40_channel_pair(iface->current_mode, pri_chan,
236					 sec_chan);
237}
238
239
240static void ieee80211n_switch_pri_sec(struct hostapd_iface *iface)
241{
242	if (iface->conf->secondary_channel > 0) {
243		iface->conf->channel += 4;
244		iface->conf->secondary_channel = -1;
245	} else {
246		iface->conf->channel -= 4;
247		iface->conf->secondary_channel = 1;
248	}
249}
250
251
252static int ieee80211n_check_40mhz_5g(struct hostapd_iface *iface,
253				     struct wpa_scan_results *scan_res)
254{
255	int pri_chan, sec_chan;
256	int res;
257
258	pri_chan = iface->conf->channel;
259	sec_chan = pri_chan + iface->conf->secondary_channel * 4;
260
261	res = check_40mhz_5g(iface->current_mode, scan_res, pri_chan, sec_chan);
262
263	if (res == 2)
264		ieee80211n_switch_pri_sec(iface);
265
266	return !!res;
267}
268
269
270static int ieee80211n_check_40mhz_2g4(struct hostapd_iface *iface,
271				      struct wpa_scan_results *scan_res)
272{
273	int pri_chan, sec_chan;
274
275	pri_chan = iface->conf->channel;
276	sec_chan = pri_chan + iface->conf->secondary_channel * 4;
277
278	return check_40mhz_2g4(iface->current_mode, scan_res, pri_chan,
279			       sec_chan);
280}
281
282
283static void ieee80211n_check_scan(struct hostapd_iface *iface)
284{
285	struct wpa_scan_results *scan_res;
286	int oper40;
287	int res;
288
289	/* Check list of neighboring BSSes (from scan) to see whether 40 MHz is
290	 * allowed per IEEE Std 802.11-2012, 10.15.3.2 */
291
292	iface->scan_cb = NULL;
293
294	scan_res = hostapd_driver_get_scan_results(iface->bss[0]);
295	if (scan_res == NULL) {
296		hostapd_setup_interface_complete(iface, 1);
297		return;
298	}
299
300	if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A)
301		oper40 = ieee80211n_check_40mhz_5g(iface, scan_res);
302	else
303		oper40 = ieee80211n_check_40mhz_2g4(iface, scan_res);
304	wpa_scan_results_free(scan_res);
305
306	iface->secondary_ch = iface->conf->secondary_channel;
307	if (!oper40) {
308		wpa_printf(MSG_INFO, "20/40 MHz operation not permitted on "
309			   "channel pri=%d sec=%d based on overlapping BSSes",
310			   iface->conf->channel,
311			   iface->conf->channel +
312			   iface->conf->secondary_channel * 4);
313		iface->conf->secondary_channel = 0;
314		if (iface->drv_flags & WPA_DRIVER_FLAGS_HT_2040_COEX) {
315			/*
316			 * TODO: Could consider scheduling another scan to check
317			 * if channel width can be changed if no coex reports
318			 * are received from associating stations.
319			 */
320		}
321	}
322
323	res = ieee80211n_allowed_ht40_channel_pair(iface);
324	if (!res) {
325		iface->conf->secondary_channel = 0;
326		wpa_printf(MSG_INFO, "Fallback to 20 MHz");
327	}
328
329	hostapd_setup_interface_complete(iface, !res);
330}
331
332
333static void ieee80211n_scan_channels_2g4(struct hostapd_iface *iface,
334					 struct wpa_driver_scan_params *params)
335{
336	/* Scan only the affected frequency range */
337	int pri_freq, sec_freq;
338	int affected_start, affected_end;
339	int i, pos;
340	struct hostapd_hw_modes *mode;
341
342	if (iface->current_mode == NULL)
343		return;
344
345	pri_freq = hostapd_hw_get_freq(iface->bss[0], iface->conf->channel);
346	if (iface->conf->secondary_channel > 0)
347		sec_freq = pri_freq + 20;
348	else
349		sec_freq = pri_freq - 20;
350	/*
351	 * Note: Need to find the PRI channel also in cases where the affected
352	 * channel is the SEC channel of a 40 MHz BSS, so need to include the
353	 * scanning coverage here to be 40 MHz from the center frequency.
354	 */
355	affected_start = (pri_freq + sec_freq) / 2 - 40;
356	affected_end = (pri_freq + sec_freq) / 2 + 40;
357	wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
358		   affected_start, affected_end);
359
360	mode = iface->current_mode;
361	params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
362	if (params->freqs == NULL)
363		return;
364	pos = 0;
365
366	for (i = 0; i < mode->num_channels; i++) {
367		struct hostapd_channel_data *chan = &mode->channels[i];
368		if (chan->flag & HOSTAPD_CHAN_DISABLED)
369			continue;
370		if (chan->freq < affected_start ||
371		    chan->freq > affected_end)
372			continue;
373		params->freqs[pos++] = chan->freq;
374	}
375}
376
377
378static void ieee80211n_scan_channels_5g(struct hostapd_iface *iface,
379					struct wpa_driver_scan_params *params)
380{
381	/* Scan only the affected frequency range */
382	int pri_freq;
383	int affected_start, affected_end;
384	int i, pos;
385	struct hostapd_hw_modes *mode;
386
387	if (iface->current_mode == NULL)
388		return;
389
390	pri_freq = hostapd_hw_get_freq(iface->bss[0], iface->conf->channel);
391	if (iface->conf->secondary_channel > 0) {
392		affected_start = pri_freq - 10;
393		affected_end = pri_freq + 30;
394	} else {
395		affected_start = pri_freq - 30;
396		affected_end = pri_freq + 10;
397	}
398	wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
399		   affected_start, affected_end);
400
401	mode = iface->current_mode;
402	params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
403	if (params->freqs == NULL)
404		return;
405	pos = 0;
406
407	for (i = 0; i < mode->num_channels; i++) {
408		struct hostapd_channel_data *chan = &mode->channels[i];
409		if (chan->flag & HOSTAPD_CHAN_DISABLED)
410			continue;
411		if (chan->freq < affected_start ||
412		    chan->freq > affected_end)
413			continue;
414		params->freqs[pos++] = chan->freq;
415	}
416}
417
418
419static void ap_ht40_scan_retry(void *eloop_data, void *user_data)
420{
421#define HT2040_COEX_SCAN_RETRY 15
422	struct hostapd_iface *iface = eloop_data;
423	struct wpa_driver_scan_params params;
424	int ret;
425
426	os_memset(&params, 0, sizeof(params));
427	if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
428		ieee80211n_scan_channels_2g4(iface, &params);
429	else
430		ieee80211n_scan_channels_5g(iface, &params);
431
432	ret = hostapd_driver_scan(iface->bss[0], &params);
433	iface->num_ht40_scan_tries++;
434	os_free(params.freqs);
435
436	if (ret == -EBUSY &&
437	    iface->num_ht40_scan_tries < HT2040_COEX_SCAN_RETRY) {
438		wpa_printf(MSG_ERROR,
439			   "Failed to request a scan of neighboring BSSes ret=%d (%s) - try to scan again (attempt %d)",
440			   ret, strerror(-ret), iface->num_ht40_scan_tries);
441		eloop_register_timeout(1, 0, ap_ht40_scan_retry, iface, NULL);
442		return;
443	}
444
445	if (ret == 0) {
446		iface->scan_cb = ieee80211n_check_scan;
447		return;
448	}
449
450	wpa_printf(MSG_DEBUG,
451		   "Failed to request a scan in device, bringing up in HT20 mode");
452	iface->conf->secondary_channel = 0;
453	iface->conf->ht_capab &= ~HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
454	hostapd_setup_interface_complete(iface, 0);
455}
456
457
458void hostapd_stop_setup_timers(struct hostapd_iface *iface)
459{
460	eloop_cancel_timeout(ap_ht40_scan_retry, iface, NULL);
461}
462
463
464static int ieee80211n_check_40mhz(struct hostapd_iface *iface)
465{
466	struct wpa_driver_scan_params params;
467	int ret;
468
469	if (!iface->conf->secondary_channel)
470		return 0; /* HT40 not used */
471
472	hostapd_set_state(iface, HAPD_IFACE_HT_SCAN);
473	wpa_printf(MSG_DEBUG, "Scan for neighboring BSSes prior to enabling "
474		   "40 MHz channel");
475	os_memset(&params, 0, sizeof(params));
476	if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
477		ieee80211n_scan_channels_2g4(iface, &params);
478	else
479		ieee80211n_scan_channels_5g(iface, &params);
480
481	ret = hostapd_driver_scan(iface->bss[0], &params);
482	os_free(params.freqs);
483
484	if (ret == -EBUSY) {
485		wpa_printf(MSG_ERROR,
486			   "Failed to request a scan of neighboring BSSes ret=%d (%s) - try to scan again",
487			   ret, strerror(-ret));
488		iface->num_ht40_scan_tries = 1;
489		eloop_cancel_timeout(ap_ht40_scan_retry, iface, NULL);
490		eloop_register_timeout(1, 0, ap_ht40_scan_retry, iface, NULL);
491		return 1;
492	}
493
494	if (ret < 0) {
495		wpa_printf(MSG_ERROR,
496			   "Failed to request a scan of neighboring BSSes ret=%d (%s)",
497			   ret, strerror(-ret));
498		return -1;
499	}
500
501	iface->scan_cb = ieee80211n_check_scan;
502	return 1;
503}
504
505
506static int ieee80211n_supported_ht_capab(struct hostapd_iface *iface)
507{
508	u16 hw = iface->current_mode->ht_capab;
509	u16 conf = iface->conf->ht_capab;
510
511	if ((conf & HT_CAP_INFO_LDPC_CODING_CAP) &&
512	    !(hw & HT_CAP_INFO_LDPC_CODING_CAP)) {
513		wpa_printf(MSG_ERROR, "Driver does not support configured "
514			   "HT capability [LDPC]");
515		return 0;
516	}
517
518	/*
519	 * Driver ACS chosen channel may not be HT40 due to internal driver
520	 * restrictions.
521	 */
522	if (!iface->conf->acs && (conf & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) &&
523	    !(hw & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
524		wpa_printf(MSG_ERROR, "Driver does not support configured "
525			   "HT capability [HT40*]");
526		return 0;
527	}
528
529	switch (conf & HT_CAP_INFO_SMPS_MASK) {
530	case HT_CAP_INFO_SMPS_STATIC:
531		if (!(iface->smps_modes & WPA_DRIVER_SMPS_MODE_STATIC)) {
532			wpa_printf(MSG_ERROR,
533				   "Driver does not support configured HT capability [SMPS-STATIC]");
534			return 0;
535		}
536		break;
537	case HT_CAP_INFO_SMPS_DYNAMIC:
538		if (!(iface->smps_modes & WPA_DRIVER_SMPS_MODE_DYNAMIC)) {
539			wpa_printf(MSG_ERROR,
540				   "Driver does not support configured HT capability [SMPS-DYNAMIC]");
541			return 0;
542		}
543		break;
544	case HT_CAP_INFO_SMPS_DISABLED:
545	default:
546		break;
547	}
548
549	if ((conf & HT_CAP_INFO_GREEN_FIELD) &&
550	    !(hw & HT_CAP_INFO_GREEN_FIELD)) {
551		wpa_printf(MSG_ERROR, "Driver does not support configured "
552			   "HT capability [GF]");
553		return 0;
554	}
555
556	if ((conf & HT_CAP_INFO_SHORT_GI20MHZ) &&
557	    !(hw & HT_CAP_INFO_SHORT_GI20MHZ)) {
558		wpa_printf(MSG_ERROR, "Driver does not support configured "
559			   "HT capability [SHORT-GI-20]");
560		return 0;
561	}
562
563	if ((conf & HT_CAP_INFO_SHORT_GI40MHZ) &&
564	    !(hw & HT_CAP_INFO_SHORT_GI40MHZ)) {
565		wpa_printf(MSG_ERROR, "Driver does not support configured "
566			   "HT capability [SHORT-GI-40]");
567		return 0;
568	}
569
570	if ((conf & HT_CAP_INFO_TX_STBC) && !(hw & HT_CAP_INFO_TX_STBC)) {
571		wpa_printf(MSG_ERROR, "Driver does not support configured "
572			   "HT capability [TX-STBC]");
573		return 0;
574	}
575
576	if ((conf & HT_CAP_INFO_RX_STBC_MASK) >
577	    (hw & HT_CAP_INFO_RX_STBC_MASK)) {
578		wpa_printf(MSG_ERROR, "Driver does not support configured "
579			   "HT capability [RX-STBC*]");
580		return 0;
581	}
582
583	if ((conf & HT_CAP_INFO_DELAYED_BA) &&
584	    !(hw & HT_CAP_INFO_DELAYED_BA)) {
585		wpa_printf(MSG_ERROR, "Driver does not support configured "
586			   "HT capability [DELAYED-BA]");
587		return 0;
588	}
589
590	if ((conf & HT_CAP_INFO_MAX_AMSDU_SIZE) &&
591	    !(hw & HT_CAP_INFO_MAX_AMSDU_SIZE)) {
592		wpa_printf(MSG_ERROR, "Driver does not support configured "
593			   "HT capability [MAX-AMSDU-7935]");
594		return 0;
595	}
596
597	if ((conf & HT_CAP_INFO_DSSS_CCK40MHZ) &&
598	    !(hw & HT_CAP_INFO_DSSS_CCK40MHZ)) {
599		wpa_printf(MSG_ERROR, "Driver does not support configured "
600			   "HT capability [DSSS_CCK-40]");
601		return 0;
602	}
603
604	if ((conf & HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT) &&
605	    !(hw & HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT)) {
606		wpa_printf(MSG_ERROR, "Driver does not support configured "
607			   "HT capability [LSIG-TXOP-PROT]");
608		return 0;
609	}
610
611	return 1;
612}
613
614
615#ifdef CONFIG_IEEE80211AC
616
617static int ieee80211ac_cap_check(u32 hw, u32 conf, u32 cap, const char *name)
618{
619	u32 req_cap = conf & cap;
620
621	/*
622	 * Make sure we support all requested capabilities.
623	 * NOTE: We assume that 'cap' represents a capability mask,
624	 * not a discrete value.
625	 */
626	if ((hw & req_cap) != req_cap) {
627		wpa_printf(MSG_ERROR, "Driver does not support configured VHT capability [%s]",
628			   name);
629		return 0;
630	}
631	return 1;
632}
633
634
635static int ieee80211ac_cap_check_max(u32 hw, u32 conf, u32 mask,
636				     unsigned int shift,
637				     const char *name)
638{
639	u32 hw_max = hw & mask;
640	u32 conf_val = conf & mask;
641
642	if (conf_val > hw_max) {
643		wpa_printf(MSG_ERROR, "Configured VHT capability [%s] exceeds max value supported by the driver (%d > %d)",
644			   name, conf_val >> shift, hw_max >> shift);
645		return 0;
646	}
647	return 1;
648}
649
650
651static int ieee80211ac_supported_vht_capab(struct hostapd_iface *iface)
652{
653	struct hostapd_hw_modes *mode = iface->current_mode;
654	u32 hw = mode->vht_capab;
655	u32 conf = iface->conf->vht_capab;
656
657	wpa_printf(MSG_DEBUG, "hw vht capab: 0x%x, conf vht capab: 0x%x",
658		   hw, conf);
659
660	if (mode->mode == HOSTAPD_MODE_IEEE80211G &&
661	    iface->conf->bss[0]->vendor_vht &&
662	    mode->vht_capab == 0 && iface->hw_features) {
663		int i;
664
665		for (i = 0; i < iface->num_hw_features; i++) {
666			if (iface->hw_features[i].mode ==
667			    HOSTAPD_MODE_IEEE80211A) {
668				mode = &iface->hw_features[i];
669				hw = mode->vht_capab;
670				wpa_printf(MSG_DEBUG,
671					   "update hw vht capab based on 5 GHz band: 0x%x",
672					   hw);
673				break;
674			}
675		}
676	}
677
678#define VHT_CAP_CHECK(cap) \
679	do { \
680		if (!ieee80211ac_cap_check(hw, conf, cap, #cap)) \
681			return 0; \
682	} while (0)
683
684#define VHT_CAP_CHECK_MAX(cap) \
685	do { \
686		if (!ieee80211ac_cap_check_max(hw, conf, cap, cap ## _SHIFT, \
687					       #cap)) \
688			return 0; \
689	} while (0)
690
691	VHT_CAP_CHECK_MAX(VHT_CAP_MAX_MPDU_LENGTH_MASK);
692	VHT_CAP_CHECK(VHT_CAP_SUPP_CHAN_WIDTH_160MHZ);
693	VHT_CAP_CHECK(VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ);
694	VHT_CAP_CHECK(VHT_CAP_RXLDPC);
695	VHT_CAP_CHECK(VHT_CAP_SHORT_GI_80);
696	VHT_CAP_CHECK(VHT_CAP_SHORT_GI_160);
697	VHT_CAP_CHECK(VHT_CAP_TXSTBC);
698	VHT_CAP_CHECK_MAX(VHT_CAP_RXSTBC_MASK);
699	VHT_CAP_CHECK(VHT_CAP_SU_BEAMFORMER_CAPABLE);
700	VHT_CAP_CHECK(VHT_CAP_SU_BEAMFORMEE_CAPABLE);
701	VHT_CAP_CHECK_MAX(VHT_CAP_BEAMFORMEE_STS_MAX);
702	VHT_CAP_CHECK_MAX(VHT_CAP_SOUNDING_DIMENSION_MAX);
703	VHT_CAP_CHECK(VHT_CAP_MU_BEAMFORMER_CAPABLE);
704	VHT_CAP_CHECK(VHT_CAP_MU_BEAMFORMEE_CAPABLE);
705	VHT_CAP_CHECK(VHT_CAP_VHT_TXOP_PS);
706	VHT_CAP_CHECK(VHT_CAP_HTC_VHT);
707	VHT_CAP_CHECK_MAX(VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX);
708	VHT_CAP_CHECK(VHT_CAP_VHT_LINK_ADAPTATION_VHT_UNSOL_MFB);
709	VHT_CAP_CHECK(VHT_CAP_VHT_LINK_ADAPTATION_VHT_MRQ_MFB);
710	VHT_CAP_CHECK(VHT_CAP_RX_ANTENNA_PATTERN);
711	VHT_CAP_CHECK(VHT_CAP_TX_ANTENNA_PATTERN);
712
713#undef VHT_CAP_CHECK
714#undef VHT_CAP_CHECK_MAX
715
716	return 1;
717}
718#endif /* CONFIG_IEEE80211AC */
719
720#endif /* CONFIG_IEEE80211N */
721
722
723int hostapd_check_ht_capab(struct hostapd_iface *iface)
724{
725#ifdef CONFIG_IEEE80211N
726	int ret;
727	if (!iface->conf->ieee80211n)
728		return 0;
729	if (!ieee80211n_supported_ht_capab(iface))
730		return -1;
731#ifdef CONFIG_IEEE80211AC
732	if (!ieee80211ac_supported_vht_capab(iface))
733		return -1;
734#endif /* CONFIG_IEEE80211AC */
735	ret = ieee80211n_check_40mhz(iface);
736	if (ret)
737		return ret;
738	if (!ieee80211n_allowed_ht40_channel_pair(iface))
739		return -1;
740#endif /* CONFIG_IEEE80211N */
741
742	return 0;
743}
744
745
746static int hostapd_is_usable_chan(struct hostapd_iface *iface,
747				  int channel, int primary)
748{
749	int i;
750	struct hostapd_channel_data *chan;
751
752	if (!iface->current_mode)
753		return 0;
754
755	for (i = 0; i < iface->current_mode->num_channels; i++) {
756		chan = &iface->current_mode->channels[i];
757		if (chan->chan != channel)
758			continue;
759
760		if (!(chan->flag & HOSTAPD_CHAN_DISABLED))
761			return 1;
762
763		wpa_printf(MSG_DEBUG,
764			   "%schannel [%i] (%i) is disabled for use in AP mode, flags: 0x%x%s%s",
765			   primary ? "" : "Configured HT40 secondary ",
766			   i, chan->chan, chan->flag,
767			   chan->flag & HOSTAPD_CHAN_NO_IR ? " NO-IR" : "",
768			   chan->flag & HOSTAPD_CHAN_RADAR ? " RADAR" : "");
769	}
770
771	return 0;
772}
773
774
775static int hostapd_is_usable_chans(struct hostapd_iface *iface)
776{
777	if (!hostapd_is_usable_chan(iface, iface->conf->channel, 1))
778		return 0;
779
780	if (!iface->conf->secondary_channel)
781		return 1;
782
783	return hostapd_is_usable_chan(iface, iface->conf->channel +
784				      iface->conf->secondary_channel * 4, 0);
785}
786
787
788static enum hostapd_chan_status
789hostapd_check_chans(struct hostapd_iface *iface)
790{
791	if (iface->conf->channel) {
792		if (hostapd_is_usable_chans(iface))
793			return HOSTAPD_CHAN_VALID;
794		else
795			return HOSTAPD_CHAN_INVALID;
796	}
797
798	/*
799	 * The user set channel=0 or channel=acs_survey
800	 * which is used to trigger ACS.
801	 */
802
803	switch (acs_init(iface)) {
804	case HOSTAPD_CHAN_ACS:
805		return HOSTAPD_CHAN_ACS;
806	case HOSTAPD_CHAN_VALID:
807	case HOSTAPD_CHAN_INVALID:
808	default:
809		return HOSTAPD_CHAN_INVALID;
810	}
811}
812
813
814static void hostapd_notify_bad_chans(struct hostapd_iface *iface)
815{
816	if (!iface->current_mode) {
817		hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
818			       HOSTAPD_LEVEL_WARNING,
819			       "Hardware does not support configured mode");
820		return;
821	}
822	hostapd_logger(iface->bss[0], NULL,
823		       HOSTAPD_MODULE_IEEE80211,
824		       HOSTAPD_LEVEL_WARNING,
825		       "Configured channel (%d) not found from the "
826		       "channel list of current mode (%d) %s",
827		       iface->conf->channel,
828		       iface->current_mode->mode,
829		       hostapd_hw_mode_txt(iface->current_mode->mode));
830	hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
831		       HOSTAPD_LEVEL_WARNING,
832		       "Hardware does not support configured channel");
833}
834
835
836int hostapd_acs_completed(struct hostapd_iface *iface, int err)
837{
838	int ret = -1;
839
840	if (err)
841		goto out;
842
843	switch (hostapd_check_chans(iface)) {
844	case HOSTAPD_CHAN_VALID:
845		wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO,
846			ACS_EVENT_COMPLETED "freq=%d channel=%d",
847			hostapd_hw_get_freq(iface->bss[0],
848					    iface->conf->channel),
849			iface->conf->channel);
850		break;
851	case HOSTAPD_CHAN_ACS:
852		wpa_printf(MSG_ERROR, "ACS error - reported complete, but no result available");
853		wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, ACS_EVENT_FAILED);
854		hostapd_notify_bad_chans(iface);
855		goto out;
856	case HOSTAPD_CHAN_INVALID:
857	default:
858		wpa_printf(MSG_ERROR, "ACS picked unusable channels");
859		wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, ACS_EVENT_FAILED);
860		hostapd_notify_bad_chans(iface);
861		goto out;
862	}
863
864	ret = hostapd_check_ht_capab(iface);
865	if (ret < 0)
866		goto out;
867	if (ret == 1) {
868		wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback");
869		return 0;
870	}
871
872	ret = 0;
873out:
874	return hostapd_setup_interface_complete(iface, ret);
875}
876
877
878/**
879 * hostapd_select_hw_mode - Select the hardware mode
880 * @iface: Pointer to interface data.
881 * Returns: 0 on success, < 0 on failure
882 *
883 * Sets up the hardware mode, channel, rates, and passive scanning
884 * based on the configuration.
885 */
886int hostapd_select_hw_mode(struct hostapd_iface *iface)
887{
888	int i;
889
890	if (iface->num_hw_features < 1)
891		return -1;
892
893	if ((iface->conf->hw_mode == HOSTAPD_MODE_IEEE80211G ||
894	     iface->conf->ieee80211n || iface->conf->ieee80211ac) &&
895	    iface->conf->channel == 14) {
896		wpa_printf(MSG_INFO, "Disable OFDM/HT/VHT on channel 14");
897		iface->conf->hw_mode = HOSTAPD_MODE_IEEE80211B;
898		iface->conf->ieee80211n = 0;
899		iface->conf->ieee80211ac = 0;
900	}
901
902	iface->current_mode = NULL;
903	for (i = 0; i < iface->num_hw_features; i++) {
904		struct hostapd_hw_modes *mode = &iface->hw_features[i];
905		if (mode->mode == iface->conf->hw_mode) {
906			iface->current_mode = mode;
907			break;
908		}
909	}
910
911	if (iface->current_mode == NULL) {
912		if (!(iface->drv_flags & WPA_DRIVER_FLAGS_ACS_OFFLOAD) ||
913		    !(iface->drv_flags & WPA_DRIVER_FLAGS_SUPPORT_HW_MODE_ANY))
914		{
915			wpa_printf(MSG_ERROR,
916				   "Hardware does not support configured mode");
917			hostapd_logger(iface->bss[0], NULL,
918				       HOSTAPD_MODULE_IEEE80211,
919				       HOSTAPD_LEVEL_WARNING,
920				       "Hardware does not support configured mode (%d) (hw_mode in hostapd.conf)",
921				       (int) iface->conf->hw_mode);
922			return -2;
923		}
924	}
925
926	switch (hostapd_check_chans(iface)) {
927	case HOSTAPD_CHAN_VALID:
928		return 0;
929	case HOSTAPD_CHAN_ACS: /* ACS will run and later complete */
930		return 1;
931	case HOSTAPD_CHAN_INVALID:
932	default:
933		hostapd_notify_bad_chans(iface);
934		return -3;
935	}
936}
937
938
939const char * hostapd_hw_mode_txt(int mode)
940{
941	switch (mode) {
942	case HOSTAPD_MODE_IEEE80211A:
943		return "IEEE 802.11a";
944	case HOSTAPD_MODE_IEEE80211B:
945		return "IEEE 802.11b";
946	case HOSTAPD_MODE_IEEE80211G:
947		return "IEEE 802.11g";
948	case HOSTAPD_MODE_IEEE80211AD:
949		return "IEEE 802.11ad";
950	default:
951		return "UNKNOWN";
952	}
953}
954
955
956int hostapd_hw_get_freq(struct hostapd_data *hapd, int chan)
957{
958	return hw_get_freq(hapd->iface->current_mode, chan);
959}
960
961
962int hostapd_hw_get_channel(struct hostapd_data *hapd, int freq)
963{
964	return hw_get_chan(hapd->iface->current_mode, freq);
965}
966