hw_features.c revision 051af73b8f8014eff33330aead0f36944b3403e6
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 program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * Alternatively, this software may be distributed under the terms of BSD
12 * license.
13 *
14 * See README and COPYING for more details.
15 */
16
17#include "utils/includes.h"
18
19#include "utils/common.h"
20#include "utils/eloop.h"
21#include "common/ieee802_11_defs.h"
22#include "common/ieee802_11_common.h"
23#include "drivers/driver.h"
24#include "hostapd.h"
25#include "ap_config.h"
26#include "ap_drv_ops.h"
27#include "acs.h"
28#include "hw_features.h"
29
30
31void hostapd_free_hw_features(struct hostapd_hw_modes *hw_features,
32			      size_t num_hw_features)
33{
34	size_t i;
35
36	if (hw_features == NULL)
37		return;
38
39	for (i = 0; i < num_hw_features; i++) {
40		os_free(hw_features[i].channels);
41		os_free(hw_features[i].rates);
42	}
43
44	os_free(hw_features);
45}
46
47
48#ifndef CONFIG_NO_STDOUT_DEBUG
49static char * dfs_info(struct hostapd_channel_data *chan)
50{
51	static char info[256];
52	char *state;
53
54	switch (chan->flag & HOSTAPD_CHAN_DFS_MASK) {
55	case HOSTAPD_CHAN_DFS_UNKNOWN:
56		state = "unknown";
57		break;
58	case HOSTAPD_CHAN_DFS_USABLE:
59		state = "usable";
60		break;
61	case HOSTAPD_CHAN_DFS_UNAVAILABLE:
62		state = "unavailable";
63		break;
64	case HOSTAPD_CHAN_DFS_AVAILABLE:
65		state = "available";
66		break;
67	default:
68		return "";
69	}
70	os_snprintf(info, sizeof(info), " (DFS state = %s)", state);
71	info[sizeof(info) - 1] = '\0';
72
73	return info;
74}
75#endif /* CONFIG_NO_STDOUT_DEBUG */
76
77
78int hostapd_get_hw_features(struct hostapd_iface *iface)
79{
80	struct hostapd_data *hapd = iface->bss[0];
81	int ret = 0, i, j;
82	u16 num_modes, flags;
83	struct hostapd_hw_modes *modes;
84
85	if (hostapd_drv_none(hapd))
86		return -1;
87	modes = hostapd_get_hw_feature_data(hapd, &num_modes, &flags);
88	if (modes == NULL) {
89		hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
90			       HOSTAPD_LEVEL_DEBUG,
91			       "Fetching hardware channel/rate support not "
92			       "supported.");
93		return -1;
94	}
95
96	iface->hw_flags = flags;
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			 * IBSS operation or active scanning.
115			 * Use radar channels only if the driver supports DFS.
116			 */
117			if ((feature->channels[j].flag &
118			     HOSTAPD_CHAN_RADAR) && dfs_enabled) {
119				dfs = 1;
120			} else if (feature->channels[j].flag &
121				   (HOSTAPD_CHAN_NO_IBSS |
122				    HOSTAPD_CHAN_PASSIVE_SCAN |
123				    HOSTAPD_CHAN_RADAR)) {
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 ret;
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 sec_chan, ok, j, first;
228	int allowed[] = { 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157,
229			  184, 192 };
230	size_t k;
231
232	if (!iface->conf->secondary_channel)
233		return 1; /* HT40 not used */
234
235	sec_chan = iface->conf->channel + iface->conf->secondary_channel * 4;
236	wpa_printf(MSG_DEBUG, "HT40: control channel: %d  "
237		   "secondary channel: %d",
238		   iface->conf->channel, sec_chan);
239
240	/* Verify that HT40 secondary channel is an allowed 20 MHz
241	 * channel */
242	ok = 0;
243	for (j = 0; j < iface->current_mode->num_channels; j++) {
244		struct hostapd_channel_data *chan =
245			&iface->current_mode->channels[j];
246		if (!(chan->flag & HOSTAPD_CHAN_DISABLED) &&
247		    chan->chan == sec_chan) {
248			ok = 1;
249			break;
250		}
251	}
252	if (!ok) {
253		wpa_printf(MSG_ERROR, "HT40 secondary channel %d not allowed",
254			   sec_chan);
255		return 0;
256	}
257
258	/*
259	 * Verify that HT40 primary,secondary channel pair is allowed per
260	 * IEEE 802.11n Annex J. This is only needed for 5 GHz band since
261	 * 2.4 GHz rules allow all cases where the secondary channel fits into
262	 * the list of allowed channels (already checked above).
263	 */
264	if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A)
265		return 1;
266
267	if (iface->conf->secondary_channel > 0)
268		first = iface->conf->channel;
269	else
270		first = sec_chan;
271
272	ok = 0;
273	for (k = 0; k < sizeof(allowed) / sizeof(allowed[0]); k++) {
274		if (first == allowed[k]) {
275			ok = 1;
276			break;
277		}
278	}
279	if (!ok) {
280		wpa_printf(MSG_ERROR, "HT40 channel pair (%d, %d) not allowed",
281			   iface->conf->channel,
282			   iface->conf->secondary_channel);
283		return 0;
284	}
285
286	return 1;
287}
288
289
290static void ieee80211n_switch_pri_sec(struct hostapd_iface *iface)
291{
292	if (iface->conf->secondary_channel > 0) {
293		iface->conf->channel += 4;
294		iface->conf->secondary_channel = -1;
295	} else {
296		iface->conf->channel -= 4;
297		iface->conf->secondary_channel = 1;
298	}
299}
300
301
302static void ieee80211n_get_pri_sec_chan(struct wpa_scan_res *bss,
303					int *pri_chan, int *sec_chan)
304{
305	struct ieee80211_ht_operation *oper;
306	struct ieee802_11_elems elems;
307
308	*pri_chan = *sec_chan = 0;
309
310	ieee802_11_parse_elems((u8 *) (bss + 1), bss->ie_len, &elems, 0);
311	if (elems.ht_operation &&
312	    elems.ht_operation_len >= sizeof(*oper)) {
313		oper = (struct ieee80211_ht_operation *) elems.ht_operation;
314		*pri_chan = oper->control_chan;
315		if (oper->ht_param & HT_INFO_HT_PARAM_REC_TRANS_CHNL_WIDTH) {
316			int sec = oper->ht_param &
317				HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK;
318			if (sec == HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
319				*sec_chan = *pri_chan + 4;
320			else if (sec == HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
321				*sec_chan = *pri_chan - 4;
322		}
323	}
324}
325
326
327static int ieee80211n_check_40mhz_5g(struct hostapd_iface *iface,
328				     struct wpa_scan_results *scan_res)
329{
330	int pri_chan, sec_chan, pri_freq, sec_freq, pri_bss, sec_bss;
331	int bss_pri_chan, bss_sec_chan;
332	size_t i;
333	int match;
334
335	pri_chan = iface->conf->channel;
336	sec_chan = iface->conf->secondary_channel * 4;
337	pri_freq = hostapd_hw_get_freq(iface->bss[0], pri_chan);
338	if (iface->conf->secondary_channel > 0)
339		sec_freq = pri_freq + 20;
340	else
341		sec_freq = pri_freq - 20;
342
343	/*
344	 * Switch PRI/SEC channels if Beacons were detected on selected SEC
345	 * channel, but not on selected PRI channel.
346	 */
347	pri_bss = sec_bss = 0;
348	for (i = 0; i < scan_res->num; i++) {
349		struct wpa_scan_res *bss = scan_res->res[i];
350		if (bss->freq == pri_freq)
351			pri_bss++;
352		else if (bss->freq == sec_freq)
353			sec_bss++;
354	}
355	if (sec_bss && !pri_bss) {
356		wpa_printf(MSG_INFO, "Switch own primary and secondary "
357			   "channel to get secondary channel with no Beacons "
358			   "from other BSSes");
359		ieee80211n_switch_pri_sec(iface);
360	}
361
362	/*
363	 * Match PRI/SEC channel with any existing HT40 BSS on the same
364	 * channels that we are about to use (if already mixed order in
365	 * existing BSSes, use own preference).
366	 */
367	match = 0;
368	for (i = 0; i < scan_res->num; i++) {
369		struct wpa_scan_res *bss = scan_res->res[i];
370		ieee80211n_get_pri_sec_chan(bss, &bss_pri_chan, &bss_sec_chan);
371		if (pri_chan == bss_pri_chan &&
372		    sec_chan == bss_sec_chan) {
373			match = 1;
374			break;
375		}
376	}
377	if (!match) {
378		for (i = 0; i < scan_res->num; i++) {
379			struct wpa_scan_res *bss = scan_res->res[i];
380			ieee80211n_get_pri_sec_chan(bss, &bss_pri_chan,
381						    &bss_sec_chan);
382			if (pri_chan == bss_sec_chan &&
383			    sec_chan == bss_pri_chan) {
384				wpa_printf(MSG_INFO, "Switch own primary and "
385					   "secondary channel due to BSS "
386					   "overlap with " MACSTR,
387					   MAC2STR(bss->bssid));
388				ieee80211n_switch_pri_sec(iface);
389				break;
390			}
391		}
392	}
393
394	return 1;
395}
396
397
398static int ieee80211n_check_40mhz_2g4(struct hostapd_iface *iface,
399				      struct wpa_scan_results *scan_res)
400{
401	int pri_freq, sec_freq;
402	int affected_start, affected_end;
403	size_t i;
404
405	pri_freq = hostapd_hw_get_freq(iface->bss[0], iface->conf->channel);
406	if (iface->conf->secondary_channel > 0)
407		sec_freq = pri_freq + 20;
408	else
409		sec_freq = pri_freq - 20;
410	affected_start = (pri_freq + sec_freq) / 2 - 25;
411	affected_end = (pri_freq + sec_freq) / 2 + 25;
412	wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
413		   affected_start, affected_end);
414	for (i = 0; i < scan_res->num; i++) {
415		struct wpa_scan_res *bss = scan_res->res[i];
416		int pri = bss->freq;
417		int sec = pri;
418		int sec_chan, pri_chan;
419
420		ieee80211n_get_pri_sec_chan(bss, &pri_chan, &sec_chan);
421
422		if (sec_chan) {
423			if (sec_chan < pri_chan)
424				sec = pri - 20;
425			else
426				sec = pri + 20;
427		}
428
429		if ((pri < affected_start || pri > affected_end) &&
430		    (sec < affected_start || sec > affected_end))
431			continue; /* not within affected channel range */
432
433		wpa_printf(MSG_DEBUG, "Neighboring BSS: " MACSTR
434			   " freq=%d pri=%d sec=%d",
435			   MAC2STR(bss->bssid), bss->freq, pri_chan, sec_chan);
436
437		if (sec_chan) {
438			if (pri_freq != pri || sec_freq != sec) {
439				wpa_printf(MSG_DEBUG, "40 MHz pri/sec "
440					   "mismatch with BSS " MACSTR
441					   " <%d,%d> (chan=%d%c) vs. <%d,%d>",
442					   MAC2STR(bss->bssid),
443					   pri, sec, pri_chan,
444					   sec > pri ? '+' : '-',
445					   pri_freq, sec_freq);
446				return 0;
447			}
448		}
449
450		/* TODO: 40 MHz intolerant */
451	}
452
453	return 1;
454}
455
456
457static void ieee80211n_check_scan(struct hostapd_iface *iface)
458{
459	struct wpa_scan_results *scan_res;
460	int oper40;
461	int res;
462
463	/* Check list of neighboring BSSes (from scan) to see whether 40 MHz is
464	 * allowed per IEEE Std 802.11-2012, 10.15.3.2 */
465
466	iface->scan_cb = NULL;
467
468	scan_res = hostapd_driver_get_scan_results(iface->bss[0]);
469	if (scan_res == NULL) {
470		hostapd_setup_interface_complete(iface, 1);
471		return;
472	}
473
474	if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A)
475		oper40 = ieee80211n_check_40mhz_5g(iface, scan_res);
476	else
477		oper40 = ieee80211n_check_40mhz_2g4(iface, scan_res);
478	wpa_scan_results_free(scan_res);
479
480	if (!oper40) {
481		wpa_printf(MSG_INFO, "20/40 MHz operation not permitted on "
482			   "channel pri=%d sec=%d based on overlapping BSSes",
483			   iface->conf->channel,
484			   iface->conf->channel +
485			   iface->conf->secondary_channel * 4);
486		iface->conf->secondary_channel = 0;
487	}
488
489	res = ieee80211n_allowed_ht40_channel_pair(iface);
490	hostapd_setup_interface_complete(iface, !res);
491}
492
493
494static void ieee80211n_scan_channels_2g4(struct hostapd_iface *iface,
495					 struct wpa_driver_scan_params *params)
496{
497	/* Scan only the affected frequency range */
498	int pri_freq, sec_freq;
499	int affected_start, affected_end;
500	int i, pos;
501	struct hostapd_hw_modes *mode;
502
503	if (iface->current_mode == NULL)
504		return;
505
506	pri_freq = hostapd_hw_get_freq(iface->bss[0], iface->conf->channel);
507	if (iface->conf->secondary_channel > 0)
508		sec_freq = pri_freq + 20;
509	else
510		sec_freq = pri_freq - 20;
511	affected_start = (pri_freq + sec_freq) / 2 - 25;
512	affected_end = (pri_freq + sec_freq) / 2 + 25;
513	wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
514		   affected_start, affected_end);
515
516	mode = iface->current_mode;
517	params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
518	if (params->freqs == NULL)
519		return;
520	pos = 0;
521
522	for (i = 0; i < mode->num_channels; i++) {
523		struct hostapd_channel_data *chan = &mode->channels[i];
524		if (chan->flag & HOSTAPD_CHAN_DISABLED)
525			continue;
526		if (chan->freq < affected_start ||
527		    chan->freq > affected_end)
528			continue;
529		params->freqs[pos++] = chan->freq;
530	}
531}
532
533
534static int ieee80211n_check_40mhz(struct hostapd_iface *iface)
535{
536	struct wpa_driver_scan_params params;
537
538	if (!iface->conf->secondary_channel)
539		return 0; /* HT40 not used */
540
541	wpa_printf(MSG_DEBUG, "Scan for neighboring BSSes prior to enabling "
542		   "40 MHz channel");
543	os_memset(&params, 0, sizeof(params));
544	if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
545		ieee80211n_scan_channels_2g4(iface, &params);
546	if (hostapd_driver_scan(iface->bss[0], &params) < 0) {
547		wpa_printf(MSG_ERROR, "Failed to request a scan of "
548			   "neighboring BSSes");
549		os_free(params.freqs);
550		return -1;
551	}
552	os_free(params.freqs);
553
554	iface->scan_cb = ieee80211n_check_scan;
555	return 1;
556}
557
558
559static int ieee80211n_supported_ht_capab(struct hostapd_iface *iface)
560{
561	u16 hw = iface->current_mode->ht_capab;
562	u16 conf = iface->conf->ht_capab;
563
564	if ((conf & HT_CAP_INFO_LDPC_CODING_CAP) &&
565	    !(hw & HT_CAP_INFO_LDPC_CODING_CAP)) {
566		wpa_printf(MSG_ERROR, "Driver does not support configured "
567			   "HT capability [LDPC]");
568		return 0;
569	}
570
571	if ((conf & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) &&
572	    !(hw & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
573		wpa_printf(MSG_ERROR, "Driver does not support configured "
574			   "HT capability [HT40*]");
575		return 0;
576	}
577
578	if ((conf & HT_CAP_INFO_SMPS_MASK) != (hw & HT_CAP_INFO_SMPS_MASK) &&
579	    (conf & HT_CAP_INFO_SMPS_MASK) != HT_CAP_INFO_SMPS_DISABLED) {
580		wpa_printf(MSG_ERROR, "Driver does not support configured "
581			   "HT capability [SMPS-*]");
582		return 0;
583	}
584
585	if ((conf & HT_CAP_INFO_GREEN_FIELD) &&
586	    !(hw & HT_CAP_INFO_GREEN_FIELD)) {
587		wpa_printf(MSG_ERROR, "Driver does not support configured "
588			   "HT capability [GF]");
589		return 0;
590	}
591
592	if ((conf & HT_CAP_INFO_SHORT_GI20MHZ) &&
593	    !(hw & HT_CAP_INFO_SHORT_GI20MHZ)) {
594		wpa_printf(MSG_ERROR, "Driver does not support configured "
595			   "HT capability [SHORT-GI-20]");
596		return 0;
597	}
598
599	if ((conf & HT_CAP_INFO_SHORT_GI40MHZ) &&
600	    !(hw & HT_CAP_INFO_SHORT_GI40MHZ)) {
601		wpa_printf(MSG_ERROR, "Driver does not support configured "
602			   "HT capability [SHORT-GI-40]");
603		return 0;
604	}
605
606	if ((conf & HT_CAP_INFO_TX_STBC) && !(hw & HT_CAP_INFO_TX_STBC)) {
607		wpa_printf(MSG_ERROR, "Driver does not support configured "
608			   "HT capability [TX-STBC]");
609		return 0;
610	}
611
612	if ((conf & HT_CAP_INFO_RX_STBC_MASK) >
613	    (hw & HT_CAP_INFO_RX_STBC_MASK)) {
614		wpa_printf(MSG_ERROR, "Driver does not support configured "
615			   "HT capability [RX-STBC*]");
616		return 0;
617	}
618
619	if ((conf & HT_CAP_INFO_DELAYED_BA) &&
620	    !(hw & HT_CAP_INFO_DELAYED_BA)) {
621		wpa_printf(MSG_ERROR, "Driver does not support configured "
622			   "HT capability [DELAYED-BA]");
623		return 0;
624	}
625
626	if ((conf & HT_CAP_INFO_MAX_AMSDU_SIZE) &&
627	    !(hw & HT_CAP_INFO_MAX_AMSDU_SIZE)) {
628		wpa_printf(MSG_ERROR, "Driver does not support configured "
629			   "HT capability [MAX-AMSDU-7935]");
630		return 0;
631	}
632
633	if ((conf & HT_CAP_INFO_DSSS_CCK40MHZ) &&
634	    !(hw & HT_CAP_INFO_DSSS_CCK40MHZ)) {
635		wpa_printf(MSG_ERROR, "Driver does not support configured "
636			   "HT capability [DSSS_CCK-40]");
637		return 0;
638	}
639
640	if ((conf & HT_CAP_INFO_PSMP_SUPP) && !(hw & HT_CAP_INFO_PSMP_SUPP)) {
641		wpa_printf(MSG_ERROR, "Driver does not support configured "
642			   "HT capability [PSMP]");
643		return 0;
644	}
645
646	if ((conf & HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT) &&
647	    !(hw & HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT)) {
648		wpa_printf(MSG_ERROR, "Driver does not support configured "
649			   "HT capability [LSIG-TXOP-PROT]");
650		return 0;
651	}
652
653	return 1;
654}
655
656#endif /* CONFIG_IEEE80211N */
657
658
659int hostapd_check_ht_capab(struct hostapd_iface *iface)
660{
661#ifdef CONFIG_IEEE80211N
662	int ret;
663	if (!iface->conf->ieee80211n)
664		return 0;
665	if (!ieee80211n_supported_ht_capab(iface))
666		return -1;
667	ret = ieee80211n_check_40mhz(iface);
668	if (ret)
669		return ret;
670	if (!ieee80211n_allowed_ht40_channel_pair(iface))
671		return -1;
672#endif /* CONFIG_IEEE80211N */
673
674	return 0;
675}
676
677
678static int hostapd_is_usable_chan(struct hostapd_iface *iface,
679				  int channel, int primary)
680{
681	int i;
682	struct hostapd_channel_data *chan;
683
684	for (i = 0; i < iface->current_mode->num_channels; i++) {
685		chan = &iface->current_mode->channels[i];
686		if (chan->chan != channel)
687			continue;
688
689		if (!(chan->flag & HOSTAPD_CHAN_DISABLED))
690			return 1;
691
692		wpa_printf(MSG_DEBUG,
693			   "%schannel [%i] (%i) is disabled for use in AP mode, flags: 0x%x%s%s%s",
694			   primary ? "" : "Configured HT40 secondary ",
695			   i, chan->chan, chan->flag,
696			   chan->flag & HOSTAPD_CHAN_NO_IBSS ? " NO-IBSS" : "",
697			   chan->flag & HOSTAPD_CHAN_PASSIVE_SCAN ?
698			   " PASSIVE-SCAN" : "",
699			   chan->flag & HOSTAPD_CHAN_RADAR ? " RADAR" : "");
700	}
701
702	return 0;
703}
704
705
706static int hostapd_is_usable_chans(struct hostapd_iface *iface)
707{
708	if (!hostapd_is_usable_chan(iface, iface->conf->channel, 1))
709		return 0;
710
711	if (!iface->conf->secondary_channel)
712		return 1;
713
714	return hostapd_is_usable_chan(iface, iface->conf->channel +
715				      iface->conf->secondary_channel * 4, 0);
716}
717
718
719static enum hostapd_chan_status
720hostapd_check_chans(struct hostapd_iface *iface)
721{
722	if (iface->conf->channel) {
723		if (hostapd_is_usable_chans(iface))
724			return HOSTAPD_CHAN_VALID;
725		else
726			return HOSTAPD_CHAN_INVALID;
727	}
728
729	/*
730	 * The user set channel=0 or channel=acs_survey
731	 * which is used to trigger ACS.
732	 */
733
734	switch (acs_init(iface)) {
735	case HOSTAPD_CHAN_ACS:
736		return HOSTAPD_CHAN_ACS;
737	case HOSTAPD_CHAN_VALID:
738	case HOSTAPD_CHAN_INVALID:
739	default:
740		return HOSTAPD_CHAN_INVALID;
741	}
742}
743
744
745static void hostapd_notify_bad_chans(struct hostapd_iface *iface)
746{
747	hostapd_logger(iface->bss[0], NULL,
748		       HOSTAPD_MODULE_IEEE80211,
749		       HOSTAPD_LEVEL_WARNING,
750		       "Configured channel (%d) not found from the "
751		       "channel list of current mode (%d) %s",
752		       iface->conf->channel,
753		       iface->current_mode->mode,
754		       hostapd_hw_mode_txt(iface->current_mode->mode));
755	hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
756		       HOSTAPD_LEVEL_WARNING,
757		       "Hardware does not support configured channel");
758}
759
760
761int hostapd_acs_completed(struct hostapd_iface *iface)
762{
763	int ret;
764
765	switch (hostapd_check_chans(iface)) {
766	case HOSTAPD_CHAN_VALID:
767		break;
768	case HOSTAPD_CHAN_ACS:
769		wpa_printf(MSG_ERROR, "ACS error - reported complete, but no result available");
770		hostapd_notify_bad_chans(iface);
771		return -1;
772	case HOSTAPD_CHAN_INVALID:
773	default:
774		wpa_printf(MSG_ERROR, "ACS picked unusable channels");
775		hostapd_notify_bad_chans(iface);
776		return -1;
777	}
778
779	ret = hostapd_check_ht_capab(iface);
780	if (ret < 0)
781		return -1;
782	if (ret == 1) {
783		wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback");
784		return 0;
785	}
786
787	return hostapd_setup_interface_complete(iface, 0);
788}
789
790
791/**
792 * hostapd_select_hw_mode - Select the hardware mode
793 * @iface: Pointer to interface data.
794 * Returns: 0 on success, < 0 on failure
795 *
796 * Sets up the hardware mode, channel, rates, and passive scanning
797 * based on the configuration.
798 */
799int hostapd_select_hw_mode(struct hostapd_iface *iface)
800{
801	int i;
802
803	if (iface->num_hw_features < 1)
804		return -1;
805
806	iface->current_mode = NULL;
807	for (i = 0; i < iface->num_hw_features; i++) {
808		struct hostapd_hw_modes *mode = &iface->hw_features[i];
809		if (mode->mode == iface->conf->hw_mode) {
810			iface->current_mode = mode;
811			break;
812		}
813	}
814
815	if (iface->current_mode == NULL) {
816		wpa_printf(MSG_ERROR, "Hardware does not support configured "
817			   "mode");
818		hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
819			       HOSTAPD_LEVEL_WARNING,
820			       "Hardware does not support configured mode "
821			       "(%d) (hw_mode in hostapd.conf)",
822			       (int) iface->conf->hw_mode);
823		return -2;
824	}
825
826	switch (hostapd_check_chans(iface)) {
827	case HOSTAPD_CHAN_VALID:
828		return 0;
829	case HOSTAPD_CHAN_ACS: /* ACS will run and later complete */
830		return 1;
831	case HOSTAPD_CHAN_INVALID:
832	default:
833		hostapd_notify_bad_chans(iface);
834		return -3;
835	}
836
837	return 0;
838}
839
840
841const char * hostapd_hw_mode_txt(int mode)
842{
843	switch (mode) {
844	case HOSTAPD_MODE_IEEE80211A:
845		return "IEEE 802.11a";
846	case HOSTAPD_MODE_IEEE80211B:
847		return "IEEE 802.11b";
848	case HOSTAPD_MODE_IEEE80211G:
849		return "IEEE 802.11g";
850	case HOSTAPD_MODE_IEEE80211AD:
851		return "IEEE 802.11ad";
852	default:
853		return "UNKNOWN";
854	}
855}
856
857
858int hostapd_hw_get_freq(struct hostapd_data *hapd, int chan)
859{
860	int i;
861
862	if (!hapd->iface->current_mode)
863		return 0;
864
865	for (i = 0; i < hapd->iface->current_mode->num_channels; i++) {
866		struct hostapd_channel_data *ch =
867			&hapd->iface->current_mode->channels[i];
868		if (ch->chan == chan)
869			return ch->freq;
870	}
871
872	return 0;
873}
874
875
876int hostapd_hw_get_channel(struct hostapd_data *hapd, int freq)
877{
878	int i;
879
880	if (!hapd->iface->current_mode)
881		return 0;
882
883	for (i = 0; i < hapd->iface->current_mode->num_channels; i++) {
884		struct hostapd_channel_data *ch =
885			&hapd->iface->current_mode->channels[i];
886		if (ch->freq == freq)
887			return ch->chan;
888	}
889
890	return 0;
891}
892