]>
Commit | Line | Data |
---|---|---|
6fc6879b JM |
1 | /* |
2 | * hostapd / Hardware feature query and different modes | |
3 | * Copyright 2002-2003, Instant802 Networks, Inc. | |
4 | * Copyright 2005-2006, Devicescape Software, Inc. | |
7fa56233 | 5 | * Copyright (c) 2008-2012, Jouni Malinen <j@w1.fi> |
6fc6879b JM |
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 | ||
1057d78e | 17 | #include "utils/includes.h" |
6fc6879b | 18 | |
1057d78e JM |
19 | #include "utils/common.h" |
20 | #include "utils/eloop.h" | |
90973fb2 JM |
21 | #include "common/ieee802_11_defs.h" |
22 | #include "common/ieee802_11_common.h" | |
6e6e8c31 | 23 | #include "drivers/driver.h" |
6226e38d JM |
24 | #include "hostapd.h" |
25 | #include "ap_config.h" | |
6e6e8c31 | 26 | #include "ap_drv_ops.h" |
50f4f2a0 | 27 | #include "acs.h" |
6226e38d | 28 | #include "hw_features.h" |
6fc6879b JM |
29 | |
30 | ||
31 | void 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 | ||
e76da505 JD |
48 | #ifndef CONFIG_NO_STDOUT_DEBUG |
49 | static 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 | ||
6fc6879b JM |
78 | int 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 | ||
85141289 JM |
85 | if (hostapd_drv_none(hapd)) |
86 | return -1; | |
6fc6879b JM |
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]; | |
e76da505 JD |
104 | int dfs_enabled = hapd->iconf->ieee80211h && |
105 | (iface->drv_flags & WPA_DRIVER_FLAGS_RADAR); | |
106 | ||
6fc6879b JM |
107 | /* set flag for channels we can use in current regulatory |
108 | * domain */ | |
109 | for (j = 0; j < feature->num_channels; j++) { | |
e76da505 JD |
110 | int dfs = 0; |
111 | ||
10b83bd7 JM |
112 | /* |
113 | * Disable all channels that are marked not to allow | |
e76da505 JD |
114 | * IBSS operation or active scanning. |
115 | * Use radar channels only if the driver supports DFS. | |
10b83bd7 | 116 | */ |
e76da505 JD |
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)) { | |
6fc6879b | 124 | feature->channels[j].flag |= |
10b83bd7 | 125 | HOSTAPD_CHAN_DISABLED; |
e76da505 JD |
126 | } |
127 | ||
10b83bd7 JM |
128 | if (feature->channels[j].flag & HOSTAPD_CHAN_DISABLED) |
129 | continue; | |
e76da505 | 130 | |
10b83bd7 | 131 | wpa_printf(MSG_MSGDUMP, "Allowed channel: mode=%d " |
e76da505 | 132 | "chan=%d freq=%d MHz max_tx_power=%d dBm%s", |
10b83bd7 JM |
133 | feature->mode, |
134 | feature->channels[j].chan, | |
bf01d8bc | 135 | feature->channels[j].freq, |
e76da505 JD |
136 | feature->channels[j].max_tx_power, |
137 | dfs ? dfs_info(&feature->channels[j]) : ""); | |
6fc6879b JM |
138 | } |
139 | } | |
140 | ||
141 | return ret; | |
142 | } | |
143 | ||
144 | ||
34445d12 | 145 | int hostapd_prepare_rates(struct hostapd_iface *iface, |
5a5009dc | 146 | struct hostapd_hw_modes *mode) |
6fc6879b JM |
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 | ||
34445d12 JM |
154 | if (iface->conf->basic_rates) |
155 | basic_rates = iface->conf->basic_rates; | |
6fc6879b JM |
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; | |
7829894c VK |
166 | case HOSTAPD_MODE_IEEE80211AD: |
167 | return 0; /* No basic rates for 11ad */ | |
6fc6879b JM |
168 | default: |
169 | return -1; | |
170 | } | |
171 | ||
e5693c47 JM |
172 | i = 0; |
173 | while (basic_rates[i] >= 0) | |
174 | i++; | |
f0898e95 JM |
175 | if (i) |
176 | i++; /* -1 termination */ | |
34445d12 | 177 | os_free(iface->basic_rates); |
78018ae9 | 178 | iface->basic_rates = os_malloc(i * sizeof(int)); |
34445d12 | 179 | if (iface->basic_rates) |
78018ae9 | 180 | os_memcpy(iface->basic_rates, basic_rates, i * sizeof(int)); |
6fc6879b | 181 | |
34445d12 JM |
182 | os_free(iface->current_rates); |
183 | iface->num_rates = 0; | |
6fc6879b | 184 | |
34445d12 | 185 | iface->current_rates = |
f9884c09 | 186 | os_calloc(mode->num_rates, sizeof(struct hostapd_rate_data)); |
34445d12 | 187 | if (!iface->current_rates) { |
6fc6879b JM |
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 | ||
34445d12 JM |
196 | if (iface->conf->supported_rates && |
197 | !hostapd_rate_found(iface->conf->supported_rates, | |
fb7842aa | 198 | mode->rates[i])) |
6fc6879b JM |
199 | continue; |
200 | ||
34445d12 | 201 | rate = &iface->current_rates[iface->num_rates]; |
fb7842aa | 202 | rate->rate = mode->rates[i]; |
6fc6879b JM |
203 | if (hostapd_rate_found(basic_rates, rate->rate)) { |
204 | rate->flags |= HOSTAPD_RATE_BASIC; | |
205 | num_basic_rates++; | |
fb7842aa | 206 | } |
6fc6879b | 207 | wpa_printf(MSG_DEBUG, "RATE[%d] rate=%d flags=0x%x", |
34445d12 JM |
208 | iface->num_rates, rate->rate, rate->flags); |
209 | iface->num_rates++; | |
6fc6879b JM |
210 | } |
211 | ||
34445d12 JM |
212 | if ((iface->num_rates == 0 || num_basic_rates == 0) && |
213 | (!iface->conf->ieee80211n || !iface->conf->require_ht)) { | |
6fc6879b JM |
214 | wpa_printf(MSG_ERROR, "No rates remaining in supported/basic " |
215 | "rate sets (%d,%d).", | |
34445d12 | 216 | iface->num_rates, num_basic_rates); |
6fc6879b JM |
217 | return -1; |
218 | } | |
219 | ||
220 | return 0; | |
221 | } | |
222 | ||
223 | ||
3e0cb2c5 JM |
224 | #ifdef CONFIG_IEEE80211N |
225 | static 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 | } | |
30fd5f14 JM |
288 | |
289 | ||
5eb4e3d0 JM |
290 | static 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 | ||
ad1e68e6 JM |
302 | static 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) { | |
41619262 JM |
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) | |
ad1e68e6 | 319 | *sec_chan = *pri_chan + 4; |
41619262 | 320 | else if (sec == HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW) |
ad1e68e6 JM |
321 | *sec_chan = *pri_chan - 4; |
322 | } | |
323 | } | |
324 | } | |
325 | ||
326 | ||
327 | static int ieee80211n_check_40mhz_5g(struct hostapd_iface *iface, | |
328 | struct wpa_scan_results *scan_res) | |
5eb4e3d0 JM |
329 | { |
330 | int pri_chan, sec_chan, pri_freq, sec_freq, pri_bss, sec_bss; | |
ad1e68e6 JM |
331 | int bss_pri_chan, bss_sec_chan; |
332 | size_t i; | |
5eb4e3d0 JM |
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 | ||
5eb4e3d0 JM |
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; | |
ad1e68e6 JM |
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) | |
5eb4e3d0 | 351 | pri_bss++; |
ad1e68e6 | 352 | else if (bss->freq == sec_freq) |
5eb4e3d0 JM |
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; | |
ad1e68e6 JM |
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) { | |
5eb4e3d0 JM |
373 | match = 1; |
374 | break; | |
375 | } | |
376 | } | |
377 | if (!match) { | |
ad1e68e6 JM |
378 | for (i = 0; i < scan_res->num; i++) { |
379 | struct wpa_scan_res *bss = scan_res->res[i]; | |
c6313c75 FF |
380 | ieee80211n_get_pri_sec_chan(bss, &bss_pri_chan, |
381 | &bss_sec_chan); | |
ad1e68e6 JM |
382 | if (pri_chan == bss_sec_chan && |
383 | sec_chan == bss_pri_chan) { | |
5eb4e3d0 JM |
384 | wpa_printf(MSG_INFO, "Switch own primary and " |
385 | "secondary channel due to BSS " | |
386 | "overlap with " MACSTR, | |
ad1e68e6 | 387 | MAC2STR(bss->bssid)); |
5eb4e3d0 JM |
388 | ieee80211n_switch_pri_sec(iface); |
389 | break; | |
390 | } | |
391 | } | |
392 | } | |
393 | ||
394 | return 1; | |
395 | } | |
396 | ||
397 | ||
ad1e68e6 JM |
398 | static int ieee80211n_check_40mhz_2g4(struct hostapd_iface *iface, |
399 | struct wpa_scan_results *scan_res) | |
5eb4e3d0 JM |
400 | { |
401 | int pri_freq, sec_freq; | |
402 | int affected_start, affected_end; | |
ad1e68e6 | 403 | size_t i; |
5eb4e3d0 JM |
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); | |
ad1e68e6 JM |
414 | for (i = 0; i < scan_res->num; i++) { |
415 | struct wpa_scan_res *bss = scan_res->res[i]; | |
416 | int pri = bss->freq; | |
5eb4e3d0 | 417 | int sec = pri; |
ad1e68e6 JM |
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) | |
5eb4e3d0 JM |
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 | ||
ad1e68e6 JM |
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) { | |
5eb4e3d0 JM |
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>", | |
ad1e68e6 JM |
442 | MAC2STR(bss->bssid), |
443 | pri, sec, pri_chan, | |
5eb4e3d0 JM |
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 | ||
ad1e68e6 | 457 | static void ieee80211n_check_scan(struct hostapd_iface *iface) |
5eb4e3d0 | 458 | { |
ad1e68e6 | 459 | struct wpa_scan_results *scan_res; |
5eb4e3d0 | 460 | int oper40; |
7615078c | 461 | int res; |
5eb4e3d0 | 462 | |
5eb4e3d0 | 463 | /* Check list of neighboring BSSes (from scan) to see whether 40 MHz is |
7fa56233 | 464 | * allowed per IEEE Std 802.11-2012, 10.15.3.2 */ |
5eb4e3d0 | 465 | |
ad1e68e6 JM |
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 | ||
5eb4e3d0 | 474 | if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A) |
ad1e68e6 | 475 | oper40 = ieee80211n_check_40mhz_5g(iface, scan_res); |
5eb4e3d0 | 476 | else |
ad1e68e6 JM |
477 | oper40 = ieee80211n_check_40mhz_2g4(iface, scan_res); |
478 | wpa_scan_results_free(scan_res); | |
5eb4e3d0 JM |
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; | |
5eb4e3d0 | 487 | } |
ad1e68e6 | 488 | |
7615078c JM |
489 | res = ieee80211n_allowed_ht40_channel_pair(iface); |
490 | hostapd_setup_interface_complete(iface, !res); | |
ad1e68e6 JM |
491 | } |
492 | ||
493 | ||
7fa56233 JM |
494 | static 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; | |
f9884c09 | 517 | params->freqs = os_calloc(mode->num_channels + 1, sizeof(int)); |
7fa56233 JM |
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 | ||
ad1e68e6 JM |
534 | static 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(¶ms, 0, sizeof(params)); | |
7fa56233 JM |
544 | if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G) |
545 | ieee80211n_scan_channels_2g4(iface, ¶ms); | |
ad1e68e6 JM |
546 | if (hostapd_driver_scan(iface->bss[0], ¶ms) < 0) { |
547 | wpa_printf(MSG_ERROR, "Failed to request a scan of " | |
548 | "neighboring BSSes"); | |
7fa56233 | 549 | os_free(params.freqs); |
ad1e68e6 JM |
550 | return -1; |
551 | } | |
7fa56233 | 552 | os_free(params.freqs); |
ad1e68e6 JM |
553 | |
554 | iface->scan_cb = ieee80211n_check_scan; | |
555 | return 1; | |
5eb4e3d0 JM |
556 | } |
557 | ||
558 | ||
30fd5f14 JM |
559 | static 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 | ||
30fd5f14 JM |
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 | } | |
ad1e68e6 JM |
655 | |
656 | #endif /* CONFIG_IEEE80211N */ | |
657 | ||
658 | ||
659 | int hostapd_check_ht_capab(struct hostapd_iface *iface) | |
660 | { | |
661 | #ifdef CONFIG_IEEE80211N | |
662 | int ret; | |
ec2b8909 SM |
663 | if (!iface->conf->ieee80211n) |
664 | return 0; | |
1b4d3793 NS |
665 | if (!ieee80211n_supported_ht_capab(iface)) |
666 | return -1; | |
ad1e68e6 JM |
667 | ret = ieee80211n_check_40mhz(iface); |
668 | if (ret) | |
669 | return ret; | |
670 | if (!ieee80211n_allowed_ht40_channel_pair(iface)) | |
671 | return -1; | |
3e0cb2c5 JM |
672 | #endif /* CONFIG_IEEE80211N */ |
673 | ||
ad1e68e6 JM |
674 | return 0; |
675 | } | |
676 | ||
3e0cb2c5 | 677 | |
245e026e MK |
678 | static 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 | ||
706 | static 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 | ||
719 | static enum hostapd_chan_status | |
720 | hostapd_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 | /* | |
50f4f2a0 MK |
730 | * The user set channel=0 or channel=acs_survey |
731 | * which is used to trigger ACS. | |
245e026e | 732 | */ |
50f4f2a0 MK |
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 | } | |
245e026e MK |
742 | } |
743 | ||
744 | ||
745 | static 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 | ||
50f4f2a0 MK |
761 | int hostapd_acs_completed(struct hostapd_iface *iface) |
762 | { | |
0e1d0b37 | 763 | int ret = -1; |
50f4f2a0 MK |
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); | |
0e1d0b37 | 771 | goto out; |
50f4f2a0 MK |
772 | case HOSTAPD_CHAN_INVALID: |
773 | default: | |
774 | wpa_printf(MSG_ERROR, "ACS picked unusable channels"); | |
775 | hostapd_notify_bad_chans(iface); | |
0e1d0b37 | 776 | goto out; |
50f4f2a0 MK |
777 | } |
778 | ||
779 | ret = hostapd_check_ht_capab(iface); | |
780 | if (ret < 0) | |
0e1d0b37 | 781 | goto out; |
50f4f2a0 MK |
782 | if (ret == 1) { |
783 | wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback"); | |
784 | return 0; | |
785 | } | |
786 | ||
0e1d0b37 HS |
787 | ret = 0; |
788 | out: | |
789 | return hostapd_setup_interface_complete(iface, ret); | |
50f4f2a0 MK |
790 | } |
791 | ||
792 | ||
6fc6879b | 793 | /** |
ddaa83eb | 794 | * hostapd_select_hw_mode - Select the hardware mode |
6fc6879b | 795 | * @iface: Pointer to interface data. |
60eda5e4 | 796 | * Returns: 0 on success, < 0 on failure |
6fc6879b | 797 | * |
ddaa83eb JM |
798 | * Sets up the hardware mode, channel, rates, and passive scanning |
799 | * based on the configuration. | |
6fc6879b | 800 | */ |
ddaa83eb | 801 | int hostapd_select_hw_mode(struct hostapd_iface *iface) |
6fc6879b | 802 | { |
245e026e | 803 | int i; |
6fc6879b JM |
804 | |
805 | if (iface->num_hw_features < 1) | |
806 | return -1; | |
807 | ||
808 | iface->current_mode = NULL; | |
809 | for (i = 0; i < iface->num_hw_features; i++) { | |
810 | struct hostapd_hw_modes *mode = &iface->hw_features[i]; | |
6caf9ca6 | 811 | if (mode->mode == iface->conf->hw_mode) { |
6fc6879b JM |
812 | iface->current_mode = mode; |
813 | break; | |
814 | } | |
815 | } | |
816 | ||
817 | if (iface->current_mode == NULL) { | |
818 | wpa_printf(MSG_ERROR, "Hardware does not support configured " | |
819 | "mode"); | |
820 | hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211, | |
821 | HOSTAPD_LEVEL_WARNING, | |
822 | "Hardware does not support configured mode " | |
93ac2404 JM |
823 | "(%d) (hw_mode in hostapd.conf)", |
824 | (int) iface->conf->hw_mode); | |
60eda5e4 | 825 | return -2; |
6fc6879b JM |
826 | } |
827 | ||
245e026e MK |
828 | switch (hostapd_check_chans(iface)) { |
829 | case HOSTAPD_CHAN_VALID: | |
830 | return 0; | |
50f4f2a0 MK |
831 | case HOSTAPD_CHAN_ACS: /* ACS will run and later complete */ |
832 | return 1; | |
245e026e MK |
833 | case HOSTAPD_CHAN_INVALID: |
834 | default: | |
835 | hostapd_notify_bad_chans(iface); | |
60eda5e4 | 836 | return -3; |
24c9fceb | 837 | } |
6fc6879b | 838 | |
61693eaa | 839 | return 0; |
6fc6879b JM |
840 | } |
841 | ||
842 | ||
843 | const char * hostapd_hw_mode_txt(int mode) | |
844 | { | |
845 | switch (mode) { | |
846 | case HOSTAPD_MODE_IEEE80211A: | |
847 | return "IEEE 802.11a"; | |
848 | case HOSTAPD_MODE_IEEE80211B: | |
849 | return "IEEE 802.11b"; | |
850 | case HOSTAPD_MODE_IEEE80211G: | |
851 | return "IEEE 802.11g"; | |
7829894c VK |
852 | case HOSTAPD_MODE_IEEE80211AD: |
853 | return "IEEE 802.11ad"; | |
6fc6879b JM |
854 | default: |
855 | return "UNKNOWN"; | |
856 | } | |
857 | } | |
858 | ||
859 | ||
860 | int hostapd_hw_get_freq(struct hostapd_data *hapd, int chan) | |
861 | { | |
862 | int i; | |
863 | ||
864 | if (!hapd->iface->current_mode) | |
865 | return 0; | |
866 | ||
867 | for (i = 0; i < hapd->iface->current_mode->num_channels; i++) { | |
868 | struct hostapd_channel_data *ch = | |
869 | &hapd->iface->current_mode->channels[i]; | |
870 | if (ch->chan == chan) | |
871 | return ch->freq; | |
872 | } | |
873 | ||
874 | return 0; | |
875 | } | |
876 | ||
877 | ||
878 | int hostapd_hw_get_channel(struct hostapd_data *hapd, int freq) | |
879 | { | |
880 | int i; | |
881 | ||
882 | if (!hapd->iface->current_mode) | |
883 | return 0; | |
884 | ||
885 | for (i = 0; i < hapd->iface->current_mode->num_channels; i++) { | |
886 | struct hostapd_channel_data *ch = | |
887 | &hapd->iface->current_mode->channels[i]; | |
888 | if (ch->freq == freq) | |
889 | return ch->chan; | |
890 | } | |
891 | ||
892 | return 0; | |
893 | } |