]>
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 | 6 | * |
5f9c134a JM |
7 | * This software may be distributed under the terms of the BSD license. |
8 | * See README for more details. | |
6fc6879b JM |
9 | */ |
10 | ||
1057d78e | 11 | #include "utils/includes.h" |
6fc6879b | 12 | |
1057d78e JM |
13 | #include "utils/common.h" |
14 | #include "utils/eloop.h" | |
90973fb2 JM |
15 | #include "common/ieee802_11_defs.h" |
16 | #include "common/ieee802_11_common.h" | |
ae134e1d | 17 | #include "common/wpa_ctrl.h" |
5f10b7f4 | 18 | #include "common/hw_features_common.h" |
6226e38d JM |
19 | #include "hostapd.h" |
20 | #include "ap_config.h" | |
6e6e8c31 | 21 | #include "ap_drv_ops.h" |
50f4f2a0 | 22 | #include "acs.h" |
9c47f6a2 PX |
23 | #include "ieee802_11.h" |
24 | #include "beacon.h" | |
6226e38d | 25 | #include "hw_features.h" |
6fc6879b JM |
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 | ||
e76da505 JD |
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 | ||
6fc6879b JM |
75 | int hostapd_get_hw_features(struct hostapd_iface *iface) |
76 | { | |
77 | struct hostapd_data *hapd = iface->bss[0]; | |
948d3a87 | 78 | int i, j; |
6fc6879b JM |
79 | u16 num_modes, flags; |
80 | struct hostapd_hw_modes *modes; | |
aa56e36d | 81 | u8 dfs_domain; |
6fc6879b | 82 | |
85141289 JM |
83 | if (hostapd_drv_none(hapd)) |
84 | return -1; | |
aa56e36d VT |
85 | modes = hostapd_get_hw_feature_data(hapd, &num_modes, &flags, |
86 | &dfs_domain); | |
6fc6879b JM |
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; | |
04f667fc | 96 | iface->dfs_domain = dfs_domain; |
6fc6879b JM |
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 | |
0a443580 IP |
114 | * to initiate radiation (a.k.a. passive scan and no |
115 | * IBSS). | |
e76da505 | 116 | * Use radar channels only if the driver supports DFS. |
10b83bd7 | 117 | */ |
e76da505 JD |
118 | if ((feature->channels[j].flag & |
119 | HOSTAPD_CHAN_RADAR) && dfs_enabled) { | |
120 | dfs = 1; | |
70634eec AS |
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 & | |
0a443580 | 126 | HOSTAPD_CHAN_NO_IR)) { |
6fc6879b | 127 | feature->channels[j].flag |= |
10b83bd7 | 128 | HOSTAPD_CHAN_DISABLED; |
e76da505 JD |
129 | } |
130 | ||
10b83bd7 JM |
131 | if (feature->channels[j].flag & HOSTAPD_CHAN_DISABLED) |
132 | continue; | |
e76da505 | 133 | |
10b83bd7 | 134 | wpa_printf(MSG_MSGDUMP, "Allowed channel: mode=%d " |
e76da505 | 135 | "chan=%d freq=%d MHz max_tx_power=%d dBm%s", |
10b83bd7 JM |
136 | feature->mode, |
137 | feature->channels[j].chan, | |
bf01d8bc | 138 | feature->channels[j].freq, |
e76da505 JD |
139 | feature->channels[j].max_tx_power, |
140 | dfs ? dfs_info(&feature->channels[j]) : ""); | |
6fc6879b JM |
141 | } |
142 | } | |
143 | ||
948d3a87 | 144 | return 0; |
6fc6879b JM |
145 | } |
146 | ||
147 | ||
34445d12 | 148 | int hostapd_prepare_rates(struct hostapd_iface *iface, |
5a5009dc | 149 | struct hostapd_hw_modes *mode) |
6fc6879b JM |
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 | ||
34445d12 JM |
157 | if (iface->conf->basic_rates) |
158 | basic_rates = iface->conf->basic_rates; | |
6fc6879b JM |
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; | |
7829894c VK |
169 | case HOSTAPD_MODE_IEEE80211AD: |
170 | return 0; /* No basic rates for 11ad */ | |
6fc6879b JM |
171 | default: |
172 | return -1; | |
173 | } | |
174 | ||
e5693c47 JM |
175 | i = 0; |
176 | while (basic_rates[i] >= 0) | |
177 | i++; | |
f0898e95 JM |
178 | if (i) |
179 | i++; /* -1 termination */ | |
34445d12 | 180 | os_free(iface->basic_rates); |
78018ae9 | 181 | iface->basic_rates = os_malloc(i * sizeof(int)); |
34445d12 | 182 | if (iface->basic_rates) |
78018ae9 | 183 | os_memcpy(iface->basic_rates, basic_rates, i * sizeof(int)); |
6fc6879b | 184 | |
34445d12 JM |
185 | os_free(iface->current_rates); |
186 | iface->num_rates = 0; | |
6fc6879b | 187 | |
34445d12 | 188 | iface->current_rates = |
f9884c09 | 189 | os_calloc(mode->num_rates, sizeof(struct hostapd_rate_data)); |
34445d12 | 190 | if (!iface->current_rates) { |
6fc6879b JM |
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 | ||
34445d12 JM |
199 | if (iface->conf->supported_rates && |
200 | !hostapd_rate_found(iface->conf->supported_rates, | |
fb7842aa | 201 | mode->rates[i])) |
6fc6879b JM |
202 | continue; |
203 | ||
34445d12 | 204 | rate = &iface->current_rates[iface->num_rates]; |
fb7842aa | 205 | rate->rate = mode->rates[i]; |
6fc6879b JM |
206 | if (hostapd_rate_found(basic_rates, rate->rate)) { |
207 | rate->flags |= HOSTAPD_RATE_BASIC; | |
208 | num_basic_rates++; | |
fb7842aa | 209 | } |
6fc6879b | 210 | wpa_printf(MSG_DEBUG, "RATE[%d] rate=%d flags=0x%x", |
34445d12 JM |
211 | iface->num_rates, rate->rate, rate->flags); |
212 | iface->num_rates++; | |
6fc6879b JM |
213 | } |
214 | ||
34445d12 JM |
215 | if ((iface->num_rates == 0 || num_basic_rates == 0) && |
216 | (!iface->conf->ieee80211n || !iface->conf->require_ht)) { | |
6fc6879b JM |
217 | wpa_printf(MSG_ERROR, "No rates remaining in supported/basic " |
218 | "rate sets (%d,%d).", | |
34445d12 | 219 | iface->num_rates, num_basic_rates); |
6fc6879b JM |
220 | return -1; |
221 | } | |
222 | ||
223 | return 0; | |
224 | } | |
225 | ||
226 | ||
3e0cb2c5 JM |
227 | #ifdef CONFIG_IEEE80211N |
228 | static int ieee80211n_allowed_ht40_channel_pair(struct hostapd_iface *iface) | |
229 | { | |
51442743 | 230 | int pri_chan, sec_chan; |
3e0cb2c5 | 231 | |
51442743 JD |
232 | pri_chan = iface->conf->channel; |
233 | sec_chan = pri_chan + iface->conf->secondary_channel * 4; | |
3e0cb2c5 | 234 | |
51442743 JD |
235 | return allowed_ht40_channel_pair(iface->current_mode, pri_chan, |
236 | sec_chan); | |
3e0cb2c5 | 237 | } |
30fd5f14 JM |
238 | |
239 | ||
5eb4e3d0 JM |
240 | static 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 | ||
ad1e68e6 JM |
252 | static int ieee80211n_check_40mhz_5g(struct hostapd_iface *iface, |
253 | struct wpa_scan_results *scan_res) | |
5eb4e3d0 | 254 | { |
0e550fe4 JD |
255 | int pri_chan, sec_chan; |
256 | int res; | |
5eb4e3d0 JM |
257 | |
258 | pri_chan = iface->conf->channel; | |
49b74430 | 259 | sec_chan = pri_chan + iface->conf->secondary_channel * 4; |
5eb4e3d0 | 260 | |
0e550fe4 JD |
261 | res = check_40mhz_5g(iface->current_mode, scan_res, pri_chan, sec_chan); |
262 | ||
55413ce0 JM |
263 | if (res == 2) { |
264 | if (iface->conf->no_pri_sec_switch) { | |
265 | wpa_printf(MSG_DEBUG, | |
266 | "Cannot switch PRI/SEC channels due to local constraint"); | |
267 | } else { | |
268 | ieee80211n_switch_pri_sec(iface); | |
269 | } | |
270 | } | |
5eb4e3d0 | 271 | |
0e550fe4 | 272 | return !!res; |
5eb4e3d0 JM |
273 | } |
274 | ||
275 | ||
ad1e68e6 JM |
276 | static int ieee80211n_check_40mhz_2g4(struct hostapd_iface *iface, |
277 | struct wpa_scan_results *scan_res) | |
5eb4e3d0 | 278 | { |
a828f626 | 279 | int pri_chan, sec_chan; |
5eb4e3d0 | 280 | |
a828f626 JD |
281 | pri_chan = iface->conf->channel; |
282 | sec_chan = pri_chan + iface->conf->secondary_channel * 4; | |
5eb4e3d0 | 283 | |
a828f626 JD |
284 | return check_40mhz_2g4(iface->current_mode, scan_res, pri_chan, |
285 | sec_chan); | |
5eb4e3d0 JM |
286 | } |
287 | ||
288 | ||
ad1e68e6 | 289 | static void ieee80211n_check_scan(struct hostapd_iface *iface) |
5eb4e3d0 | 290 | { |
ad1e68e6 | 291 | struct wpa_scan_results *scan_res; |
5eb4e3d0 | 292 | int oper40; |
7615078c | 293 | int res; |
5eb4e3d0 | 294 | |
5eb4e3d0 | 295 | /* Check list of neighboring BSSes (from scan) to see whether 40 MHz is |
7fa56233 | 296 | * allowed per IEEE Std 802.11-2012, 10.15.3.2 */ |
5eb4e3d0 | 297 | |
ad1e68e6 JM |
298 | iface->scan_cb = NULL; |
299 | ||
300 | scan_res = hostapd_driver_get_scan_results(iface->bss[0]); | |
301 | if (scan_res == NULL) { | |
302 | hostapd_setup_interface_complete(iface, 1); | |
303 | return; | |
304 | } | |
305 | ||
5eb4e3d0 | 306 | if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A) |
ad1e68e6 | 307 | oper40 = ieee80211n_check_40mhz_5g(iface, scan_res); |
5eb4e3d0 | 308 | else |
ad1e68e6 JM |
309 | oper40 = ieee80211n_check_40mhz_2g4(iface, scan_res); |
310 | wpa_scan_results_free(scan_res); | |
5eb4e3d0 | 311 | |
9c47f6a2 | 312 | iface->secondary_ch = iface->conf->secondary_channel; |
5eb4e3d0 JM |
313 | if (!oper40) { |
314 | wpa_printf(MSG_INFO, "20/40 MHz operation not permitted on " | |
315 | "channel pri=%d sec=%d based on overlapping BSSes", | |
316 | iface->conf->channel, | |
317 | iface->conf->channel + | |
318 | iface->conf->secondary_channel * 4); | |
319 | iface->conf->secondary_channel = 0; | |
9c47f6a2 PX |
320 | if (iface->drv_flags & WPA_DRIVER_FLAGS_HT_2040_COEX) { |
321 | /* | |
322 | * TODO: Could consider scheduling another scan to check | |
323 | * if channel width can be changed if no coex reports | |
324 | * are received from associating stations. | |
325 | */ | |
326 | } | |
5eb4e3d0 | 327 | } |
ad1e68e6 | 328 | |
7615078c | 329 | res = ieee80211n_allowed_ht40_channel_pair(iface); |
9c47f6a2 PX |
330 | if (!res) { |
331 | iface->conf->secondary_channel = 0; | |
c6b7ac07 JC |
332 | hostapd_set_oper_centr_freq_seg0_idx(iface->conf, 0); |
333 | hostapd_set_oper_centr_freq_seg1_idx(iface->conf, 0); | |
334 | hostapd_set_oper_chwidth(iface->conf, CHANWIDTH_USE_HT); | |
236053e5 | 335 | res = 1; |
9c47f6a2 PX |
336 | wpa_printf(MSG_INFO, "Fallback to 20 MHz"); |
337 | } | |
338 | ||
7615078c | 339 | hostapd_setup_interface_complete(iface, !res); |
ad1e68e6 JM |
340 | } |
341 | ||
342 | ||
7fa56233 JM |
343 | static void ieee80211n_scan_channels_2g4(struct hostapd_iface *iface, |
344 | struct wpa_driver_scan_params *params) | |
345 | { | |
346 | /* Scan only the affected frequency range */ | |
347 | int pri_freq, sec_freq; | |
348 | int affected_start, affected_end; | |
349 | int i, pos; | |
350 | struct hostapd_hw_modes *mode; | |
351 | ||
352 | if (iface->current_mode == NULL) | |
353 | return; | |
354 | ||
355 | pri_freq = hostapd_hw_get_freq(iface->bss[0], iface->conf->channel); | |
356 | if (iface->conf->secondary_channel > 0) | |
357 | sec_freq = pri_freq + 20; | |
358 | else | |
359 | sec_freq = pri_freq - 20; | |
2eb5967d JM |
360 | /* |
361 | * Note: Need to find the PRI channel also in cases where the affected | |
362 | * channel is the SEC channel of a 40 MHz BSS, so need to include the | |
363 | * scanning coverage here to be 40 MHz from the center frequency. | |
364 | */ | |
365 | affected_start = (pri_freq + sec_freq) / 2 - 40; | |
366 | affected_end = (pri_freq + sec_freq) / 2 + 40; | |
7fa56233 JM |
367 | wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz", |
368 | affected_start, affected_end); | |
369 | ||
370 | mode = iface->current_mode; | |
f9884c09 | 371 | params->freqs = os_calloc(mode->num_channels + 1, sizeof(int)); |
7fa56233 JM |
372 | if (params->freqs == NULL) |
373 | return; | |
374 | pos = 0; | |
375 | ||
376 | for (i = 0; i < mode->num_channels; i++) { | |
377 | struct hostapd_channel_data *chan = &mode->channels[i]; | |
378 | if (chan->flag & HOSTAPD_CHAN_DISABLED) | |
379 | continue; | |
380 | if (chan->freq < affected_start || | |
381 | chan->freq > affected_end) | |
382 | continue; | |
383 | params->freqs[pos++] = chan->freq; | |
384 | } | |
385 | } | |
386 | ||
387 | ||
afadaff9 JM |
388 | static void ieee80211n_scan_channels_5g(struct hostapd_iface *iface, |
389 | struct wpa_driver_scan_params *params) | |
390 | { | |
391 | /* Scan only the affected frequency range */ | |
392 | int pri_freq; | |
393 | int affected_start, affected_end; | |
394 | int i, pos; | |
395 | struct hostapd_hw_modes *mode; | |
396 | ||
397 | if (iface->current_mode == NULL) | |
398 | return; | |
399 | ||
400 | pri_freq = hostapd_hw_get_freq(iface->bss[0], iface->conf->channel); | |
401 | if (iface->conf->secondary_channel > 0) { | |
402 | affected_start = pri_freq - 10; | |
403 | affected_end = pri_freq + 30; | |
404 | } else { | |
405 | affected_start = pri_freq - 30; | |
406 | affected_end = pri_freq + 10; | |
407 | } | |
408 | wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz", | |
409 | affected_start, affected_end); | |
410 | ||
411 | mode = iface->current_mode; | |
412 | params->freqs = os_calloc(mode->num_channels + 1, sizeof(int)); | |
413 | if (params->freqs == NULL) | |
414 | return; | |
415 | pos = 0; | |
416 | ||
417 | for (i = 0; i < mode->num_channels; i++) { | |
418 | struct hostapd_channel_data *chan = &mode->channels[i]; | |
419 | if (chan->flag & HOSTAPD_CHAN_DISABLED) | |
420 | continue; | |
421 | if (chan->freq < affected_start || | |
422 | chan->freq > affected_end) | |
423 | continue; | |
424 | params->freqs[pos++] = chan->freq; | |
425 | } | |
426 | } | |
427 | ||
428 | ||
5f0bca77 PX |
429 | static void ap_ht40_scan_retry(void *eloop_data, void *user_data) |
430 | { | |
431 | #define HT2040_COEX_SCAN_RETRY 15 | |
432 | struct hostapd_iface *iface = eloop_data; | |
433 | struct wpa_driver_scan_params params; | |
434 | int ret; | |
435 | ||
436 | os_memset(¶ms, 0, sizeof(params)); | |
437 | if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G) | |
438 | ieee80211n_scan_channels_2g4(iface, ¶ms); | |
439 | else | |
440 | ieee80211n_scan_channels_5g(iface, ¶ms); | |
441 | ||
442 | ret = hostapd_driver_scan(iface->bss[0], ¶ms); | |
443 | iface->num_ht40_scan_tries++; | |
444 | os_free(params.freqs); | |
445 | ||
446 | if (ret == -EBUSY && | |
447 | iface->num_ht40_scan_tries < HT2040_COEX_SCAN_RETRY) { | |
448 | wpa_printf(MSG_ERROR, | |
449 | "Failed to request a scan of neighboring BSSes ret=%d (%s) - try to scan again (attempt %d)", | |
450 | ret, strerror(-ret), iface->num_ht40_scan_tries); | |
451 | eloop_register_timeout(1, 0, ap_ht40_scan_retry, iface, NULL); | |
452 | return; | |
453 | } | |
454 | ||
455 | if (ret == 0) { | |
456 | iface->scan_cb = ieee80211n_check_scan; | |
457 | return; | |
458 | } | |
459 | ||
460 | wpa_printf(MSG_DEBUG, | |
461 | "Failed to request a scan in device, bringing up in HT20 mode"); | |
462 | iface->conf->secondary_channel = 0; | |
463 | iface->conf->ht_capab &= ~HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET; | |
464 | hostapd_setup_interface_complete(iface, 0); | |
465 | } | |
466 | ||
467 | ||
468 | void hostapd_stop_setup_timers(struct hostapd_iface *iface) | |
469 | { | |
470 | eloop_cancel_timeout(ap_ht40_scan_retry, iface, NULL); | |
471 | } | |
472 | ||
473 | ||
ad1e68e6 JM |
474 | static int ieee80211n_check_40mhz(struct hostapd_iface *iface) |
475 | { | |
476 | struct wpa_driver_scan_params params; | |
5f0bca77 | 477 | int ret; |
ad1e68e6 | 478 | |
6bdc43c4 AB |
479 | /* Check that HT40 is used and PRI / SEC switch is allowed */ |
480 | if (!iface->conf->secondary_channel || iface->conf->no_pri_sec_switch) | |
481 | return 0; | |
ad1e68e6 | 482 | |
e1c5faf0 | 483 | hostapd_set_state(iface, HAPD_IFACE_HT_SCAN); |
ad1e68e6 JM |
484 | wpa_printf(MSG_DEBUG, "Scan for neighboring BSSes prior to enabling " |
485 | "40 MHz channel"); | |
486 | os_memset(¶ms, 0, sizeof(params)); | |
7fa56233 JM |
487 | if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G) |
488 | ieee80211n_scan_channels_2g4(iface, ¶ms); | |
afadaff9 JM |
489 | else |
490 | ieee80211n_scan_channels_5g(iface, ¶ms); | |
5f0bca77 PX |
491 | |
492 | ret = hostapd_driver_scan(iface->bss[0], ¶ms); | |
493 | os_free(params.freqs); | |
494 | ||
495 | if (ret == -EBUSY) { | |
496 | wpa_printf(MSG_ERROR, | |
497 | "Failed to request a scan of neighboring BSSes ret=%d (%s) - try to scan again", | |
498 | ret, strerror(-ret)); | |
499 | iface->num_ht40_scan_tries = 1; | |
500 | eloop_cancel_timeout(ap_ht40_scan_retry, iface, NULL); | |
501 | eloop_register_timeout(1, 0, ap_ht40_scan_retry, iface, NULL); | |
502 | return 1; | |
503 | } | |
504 | ||
505 | if (ret < 0) { | |
506 | wpa_printf(MSG_ERROR, | |
507 | "Failed to request a scan of neighboring BSSes ret=%d (%s)", | |
508 | ret, strerror(-ret)); | |
ad1e68e6 JM |
509 | return -1; |
510 | } | |
511 | ||
512 | iface->scan_cb = ieee80211n_check_scan; | |
513 | return 1; | |
5eb4e3d0 JM |
514 | } |
515 | ||
516 | ||
30fd5f14 JM |
517 | static int ieee80211n_supported_ht_capab(struct hostapd_iface *iface) |
518 | { | |
519 | u16 hw = iface->current_mode->ht_capab; | |
520 | u16 conf = iface->conf->ht_capab; | |
521 | ||
30fd5f14 JM |
522 | if ((conf & HT_CAP_INFO_LDPC_CODING_CAP) && |
523 | !(hw & HT_CAP_INFO_LDPC_CODING_CAP)) { | |
524 | wpa_printf(MSG_ERROR, "Driver does not support configured " | |
525 | "HT capability [LDPC]"); | |
526 | return 0; | |
527 | } | |
528 | ||
857d9422 MM |
529 | /* |
530 | * Driver ACS chosen channel may not be HT40 due to internal driver | |
531 | * restrictions. | |
532 | */ | |
533 | if (!iface->conf->acs && (conf & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) && | |
30fd5f14 JM |
534 | !(hw & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) { |
535 | wpa_printf(MSG_ERROR, "Driver does not support configured " | |
536 | "HT capability [HT40*]"); | |
537 | return 0; | |
538 | } | |
539 | ||
04ee647d EP |
540 | switch (conf & HT_CAP_INFO_SMPS_MASK) { |
541 | case HT_CAP_INFO_SMPS_STATIC: | |
542 | if (!(iface->smps_modes & WPA_DRIVER_SMPS_MODE_STATIC)) { | |
543 | wpa_printf(MSG_ERROR, | |
544 | "Driver does not support configured HT capability [SMPS-STATIC]"); | |
545 | return 0; | |
546 | } | |
547 | break; | |
548 | case HT_CAP_INFO_SMPS_DYNAMIC: | |
549 | if (!(iface->smps_modes & WPA_DRIVER_SMPS_MODE_DYNAMIC)) { | |
550 | wpa_printf(MSG_ERROR, | |
551 | "Driver does not support configured HT capability [SMPS-DYNAMIC]"); | |
552 | return 0; | |
553 | } | |
554 | break; | |
555 | case HT_CAP_INFO_SMPS_DISABLED: | |
556 | default: | |
557 | break; | |
30fd5f14 JM |
558 | } |
559 | ||
560 | if ((conf & HT_CAP_INFO_GREEN_FIELD) && | |
561 | !(hw & HT_CAP_INFO_GREEN_FIELD)) { | |
562 | wpa_printf(MSG_ERROR, "Driver does not support configured " | |
563 | "HT capability [GF]"); | |
564 | return 0; | |
565 | } | |
566 | ||
567 | if ((conf & HT_CAP_INFO_SHORT_GI20MHZ) && | |
568 | !(hw & HT_CAP_INFO_SHORT_GI20MHZ)) { | |
569 | wpa_printf(MSG_ERROR, "Driver does not support configured " | |
570 | "HT capability [SHORT-GI-20]"); | |
571 | return 0; | |
572 | } | |
573 | ||
574 | if ((conf & HT_CAP_INFO_SHORT_GI40MHZ) && | |
575 | !(hw & HT_CAP_INFO_SHORT_GI40MHZ)) { | |
576 | wpa_printf(MSG_ERROR, "Driver does not support configured " | |
577 | "HT capability [SHORT-GI-40]"); | |
578 | return 0; | |
579 | } | |
580 | ||
581 | if ((conf & HT_CAP_INFO_TX_STBC) && !(hw & HT_CAP_INFO_TX_STBC)) { | |
582 | wpa_printf(MSG_ERROR, "Driver does not support configured " | |
583 | "HT capability [TX-STBC]"); | |
584 | return 0; | |
585 | } | |
586 | ||
587 | if ((conf & HT_CAP_INFO_RX_STBC_MASK) > | |
588 | (hw & HT_CAP_INFO_RX_STBC_MASK)) { | |
589 | wpa_printf(MSG_ERROR, "Driver does not support configured " | |
590 | "HT capability [RX-STBC*]"); | |
591 | return 0; | |
592 | } | |
593 | ||
594 | if ((conf & HT_CAP_INFO_DELAYED_BA) && | |
595 | !(hw & HT_CAP_INFO_DELAYED_BA)) { | |
596 | wpa_printf(MSG_ERROR, "Driver does not support configured " | |
597 | "HT capability [DELAYED-BA]"); | |
598 | return 0; | |
599 | } | |
600 | ||
601 | if ((conf & HT_CAP_INFO_MAX_AMSDU_SIZE) && | |
602 | !(hw & HT_CAP_INFO_MAX_AMSDU_SIZE)) { | |
603 | wpa_printf(MSG_ERROR, "Driver does not support configured " | |
604 | "HT capability [MAX-AMSDU-7935]"); | |
605 | return 0; | |
606 | } | |
607 | ||
608 | if ((conf & HT_CAP_INFO_DSSS_CCK40MHZ) && | |
609 | !(hw & HT_CAP_INFO_DSSS_CCK40MHZ)) { | |
610 | wpa_printf(MSG_ERROR, "Driver does not support configured " | |
611 | "HT capability [DSSS_CCK-40]"); | |
612 | return 0; | |
613 | } | |
614 | ||
30fd5f14 JM |
615 | if ((conf & HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT) && |
616 | !(hw & HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT)) { | |
617 | wpa_printf(MSG_ERROR, "Driver does not support configured " | |
618 | "HT capability [LSIG-TXOP-PROT]"); | |
619 | return 0; | |
620 | } | |
621 | ||
622 | return 1; | |
623 | } | |
ad1e68e6 | 624 | |
c781eb84 EP |
625 | |
626 | #ifdef CONFIG_IEEE80211AC | |
c781eb84 EP |
627 | static int ieee80211ac_supported_vht_capab(struct hostapd_iface *iface) |
628 | { | |
e7d0e97b YL |
629 | struct hostapd_hw_modes *mode = iface->current_mode; |
630 | u32 hw = mode->vht_capab; | |
c781eb84 EP |
631 | u32 conf = iface->conf->vht_capab; |
632 | ||
633 | wpa_printf(MSG_DEBUG, "hw vht capab: 0x%x, conf vht capab: 0x%x", | |
634 | hw, conf); | |
635 | ||
e7d0e97b YL |
636 | if (mode->mode == HOSTAPD_MODE_IEEE80211G && |
637 | iface->conf->bss[0]->vendor_vht && | |
638 | mode->vht_capab == 0 && iface->hw_features) { | |
639 | int i; | |
640 | ||
641 | for (i = 0; i < iface->num_hw_features; i++) { | |
642 | if (iface->hw_features[i].mode == | |
643 | HOSTAPD_MODE_IEEE80211A) { | |
644 | mode = &iface->hw_features[i]; | |
645 | hw = mode->vht_capab; | |
646 | wpa_printf(MSG_DEBUG, | |
647 | "update hw vht capab based on 5 GHz band: 0x%x", | |
648 | hw); | |
649 | break; | |
650 | } | |
651 | } | |
652 | } | |
653 | ||
a7a638c2 | 654 | return ieee80211ac_cap_check(hw, conf); |
c781eb84 EP |
655 | } |
656 | #endif /* CONFIG_IEEE80211AC */ | |
657 | ||
ad1e68e6 JM |
658 | #endif /* CONFIG_IEEE80211N */ |
659 | ||
660 | ||
661 | int hostapd_check_ht_capab(struct hostapd_iface *iface) | |
662 | { | |
663 | #ifdef CONFIG_IEEE80211N | |
664 | int ret; | |
ec2b8909 SM |
665 | if (!iface->conf->ieee80211n) |
666 | return 0; | |
80653771 VT |
667 | |
668 | if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211B && | |
669 | iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G && | |
670 | (iface->conf->ht_capab & HT_CAP_INFO_DSSS_CCK40MHZ)) { | |
671 | wpa_printf(MSG_DEBUG, | |
672 | "Disable HT capability [DSSS_CCK-40] on 5 GHz band"); | |
673 | iface->conf->ht_capab &= ~HT_CAP_INFO_DSSS_CCK40MHZ; | |
674 | } | |
675 | ||
1b4d3793 NS |
676 | if (!ieee80211n_supported_ht_capab(iface)) |
677 | return -1; | |
c781eb84 | 678 | #ifdef CONFIG_IEEE80211AC |
72a09d43 SM |
679 | if (iface->conf->ieee80211ac && |
680 | !ieee80211ac_supported_vht_capab(iface)) | |
c781eb84 EP |
681 | return -1; |
682 | #endif /* CONFIG_IEEE80211AC */ | |
ad1e68e6 JM |
683 | ret = ieee80211n_check_40mhz(iface); |
684 | if (ret) | |
685 | return ret; | |
686 | if (!ieee80211n_allowed_ht40_channel_pair(iface)) | |
687 | return -1; | |
3e0cb2c5 JM |
688 | #endif /* CONFIG_IEEE80211N */ |
689 | ||
ad1e68e6 JM |
690 | return 0; |
691 | } | |
692 | ||
3e0cb2c5 | 693 | |
245e026e MK |
694 | static int hostapd_is_usable_chan(struct hostapd_iface *iface, |
695 | int channel, int primary) | |
696 | { | |
245e026e MK |
697 | struct hostapd_channel_data *chan; |
698 | ||
0980c7fa JM |
699 | if (!iface->current_mode) |
700 | return 0; | |
701 | ||
ce6d9ce1 DL |
702 | chan = hw_get_channel_chan(iface->current_mode, channel, NULL); |
703 | if (!chan) | |
704 | return 0; | |
245e026e | 705 | |
ce6d9ce1 DL |
706 | if ((primary && chan_pri_allowed(chan)) || |
707 | (!primary && !(chan->flag & HOSTAPD_CHAN_DISABLED))) | |
708 | return 1; | |
245e026e | 709 | |
ce6d9ce1 DL |
710 | wpa_printf(MSG_INFO, |
711 | "Channel %d (%s) not allowed for AP mode, flags: 0x%x%s%s", | |
712 | channel, primary ? "primary" : "secondary", | |
713 | chan->flag, | |
714 | chan->flag & HOSTAPD_CHAN_NO_IR ? " NO-IR" : "", | |
715 | chan->flag & HOSTAPD_CHAN_RADAR ? " RADAR" : ""); | |
245e026e MK |
716 | return 0; |
717 | } | |
718 | ||
719 | ||
720 | static int hostapd_is_usable_chans(struct hostapd_iface *iface) | |
721 | { | |
ec27b04e | 722 | int secondary_chan; |
ce6d9ce1 DL |
723 | struct hostapd_channel_data *pri_chan; |
724 | ||
725 | pri_chan = hw_get_channel_chan(iface->current_mode, | |
726 | iface->conf->channel, NULL); | |
727 | if (!pri_chan) | |
728 | return 0; | |
ec27b04e | 729 | |
245e026e MK |
730 | if (!hostapd_is_usable_chan(iface, iface->conf->channel, 1)) |
731 | return 0; | |
732 | ||
733 | if (!iface->conf->secondary_channel) | |
734 | return 1; | |
735 | ||
ec27b04e PX |
736 | if (!iface->conf->ht40_plus_minus_allowed) |
737 | return hostapd_is_usable_chan( | |
738 | iface, iface->conf->channel + | |
739 | iface->conf->secondary_channel * 4, 0); | |
740 | ||
741 | /* Both HT40+ and HT40- are set, pick a valid secondary channel */ | |
742 | secondary_chan = iface->conf->channel + 4; | |
ce6d9ce1 DL |
743 | if (hostapd_is_usable_chan(iface, secondary_chan, 0) && |
744 | (pri_chan->allowed_bw & HOSTAPD_CHAN_WIDTH_40P)) { | |
ec27b04e PX |
745 | iface->conf->secondary_channel = 1; |
746 | return 1; | |
747 | } | |
748 | ||
749 | secondary_chan = iface->conf->channel - 4; | |
ce6d9ce1 DL |
750 | if (hostapd_is_usable_chan(iface, secondary_chan, 0) && |
751 | (pri_chan->allowed_bw & HOSTAPD_CHAN_WIDTH_40M)) { | |
ec27b04e PX |
752 | iface->conf->secondary_channel = -1; |
753 | return 1; | |
754 | } | |
755 | ||
756 | return 0; | |
245e026e MK |
757 | } |
758 | ||
759 | ||
760 | static enum hostapd_chan_status | |
761 | hostapd_check_chans(struct hostapd_iface *iface) | |
762 | { | |
763 | if (iface->conf->channel) { | |
764 | if (hostapd_is_usable_chans(iface)) | |
765 | return HOSTAPD_CHAN_VALID; | |
766 | else | |
767 | return HOSTAPD_CHAN_INVALID; | |
768 | } | |
769 | ||
770 | /* | |
50f4f2a0 MK |
771 | * The user set channel=0 or channel=acs_survey |
772 | * which is used to trigger ACS. | |
245e026e | 773 | */ |
50f4f2a0 MK |
774 | |
775 | switch (acs_init(iface)) { | |
776 | case HOSTAPD_CHAN_ACS: | |
777 | return HOSTAPD_CHAN_ACS; | |
778 | case HOSTAPD_CHAN_VALID: | |
779 | case HOSTAPD_CHAN_INVALID: | |
780 | default: | |
781 | return HOSTAPD_CHAN_INVALID; | |
782 | } | |
245e026e MK |
783 | } |
784 | ||
785 | ||
786 | static void hostapd_notify_bad_chans(struct hostapd_iface *iface) | |
787 | { | |
0980c7fa JM |
788 | if (!iface->current_mode) { |
789 | hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211, | |
790 | HOSTAPD_LEVEL_WARNING, | |
791 | "Hardware does not support configured mode"); | |
792 | return; | |
793 | } | |
245e026e MK |
794 | hostapd_logger(iface->bss[0], NULL, |
795 | HOSTAPD_MODULE_IEEE80211, | |
796 | HOSTAPD_LEVEL_WARNING, | |
797 | "Configured channel (%d) not found from the " | |
798 | "channel list of current mode (%d) %s", | |
799 | iface->conf->channel, | |
800 | iface->current_mode->mode, | |
801 | hostapd_hw_mode_txt(iface->current_mode->mode)); | |
802 | hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211, | |
803 | HOSTAPD_LEVEL_WARNING, | |
804 | "Hardware does not support configured channel"); | |
805 | } | |
806 | ||
807 | ||
3645fd5a | 808 | int hostapd_acs_completed(struct hostapd_iface *iface, int err) |
50f4f2a0 | 809 | { |
0e1d0b37 | 810 | int ret = -1; |
50f4f2a0 | 811 | |
3645fd5a HS |
812 | if (err) |
813 | goto out; | |
814 | ||
50f4f2a0 MK |
815 | switch (hostapd_check_chans(iface)) { |
816 | case HOSTAPD_CHAN_VALID: | |
ae134e1d JM |
817 | wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, |
818 | ACS_EVENT_COMPLETED "freq=%d channel=%d", | |
819 | hostapd_hw_get_freq(iface->bss[0], | |
820 | iface->conf->channel), | |
821 | iface->conf->channel); | |
50f4f2a0 MK |
822 | break; |
823 | case HOSTAPD_CHAN_ACS: | |
824 | wpa_printf(MSG_ERROR, "ACS error - reported complete, but no result available"); | |
ae134e1d | 825 | wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, ACS_EVENT_FAILED); |
50f4f2a0 | 826 | hostapd_notify_bad_chans(iface); |
0e1d0b37 | 827 | goto out; |
50f4f2a0 MK |
828 | case HOSTAPD_CHAN_INVALID: |
829 | default: | |
830 | wpa_printf(MSG_ERROR, "ACS picked unusable channels"); | |
ae134e1d | 831 | wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, ACS_EVENT_FAILED); |
50f4f2a0 | 832 | hostapd_notify_bad_chans(iface); |
0e1d0b37 | 833 | goto out; |
50f4f2a0 MK |
834 | } |
835 | ||
836 | ret = hostapd_check_ht_capab(iface); | |
837 | if (ret < 0) | |
0e1d0b37 | 838 | goto out; |
50f4f2a0 MK |
839 | if (ret == 1) { |
840 | wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback"); | |
841 | return 0; | |
842 | } | |
843 | ||
0e1d0b37 HS |
844 | ret = 0; |
845 | out: | |
846 | return hostapd_setup_interface_complete(iface, ret); | |
50f4f2a0 MK |
847 | } |
848 | ||
849 | ||
6fc6879b | 850 | /** |
ddaa83eb | 851 | * hostapd_select_hw_mode - Select the hardware mode |
6fc6879b | 852 | * @iface: Pointer to interface data. |
60eda5e4 | 853 | * Returns: 0 on success, < 0 on failure |
6fc6879b | 854 | * |
ddaa83eb JM |
855 | * Sets up the hardware mode, channel, rates, and passive scanning |
856 | * based on the configuration. | |
6fc6879b | 857 | */ |
ddaa83eb | 858 | int hostapd_select_hw_mode(struct hostapd_iface *iface) |
6fc6879b | 859 | { |
245e026e | 860 | int i; |
6fc6879b JM |
861 | |
862 | if (iface->num_hw_features < 1) | |
863 | return -1; | |
864 | ||
c1151e47 JM |
865 | if ((iface->conf->hw_mode == HOSTAPD_MODE_IEEE80211G || |
866 | iface->conf->ieee80211n || iface->conf->ieee80211ac) && | |
867 | iface->conf->channel == 14) { | |
868 | wpa_printf(MSG_INFO, "Disable OFDM/HT/VHT on channel 14"); | |
869 | iface->conf->hw_mode = HOSTAPD_MODE_IEEE80211B; | |
870 | iface->conf->ieee80211n = 0; | |
871 | iface->conf->ieee80211ac = 0; | |
872 | } | |
873 | ||
6fc6879b JM |
874 | iface->current_mode = NULL; |
875 | for (i = 0; i < iface->num_hw_features; i++) { | |
876 | struct hostapd_hw_modes *mode = &iface->hw_features[i]; | |
6caf9ca6 | 877 | if (mode->mode == iface->conf->hw_mode) { |
6fc6879b JM |
878 | iface->current_mode = mode; |
879 | break; | |
880 | } | |
881 | } | |
882 | ||
883 | if (iface->current_mode == NULL) { | |
3784c058 PX |
884 | if (!(iface->drv_flags & WPA_DRIVER_FLAGS_ACS_OFFLOAD) || |
885 | !(iface->drv_flags & WPA_DRIVER_FLAGS_SUPPORT_HW_MODE_ANY)) | |
886 | { | |
887 | wpa_printf(MSG_ERROR, | |
888 | "Hardware does not support configured mode"); | |
889 | hostapd_logger(iface->bss[0], NULL, | |
890 | HOSTAPD_MODULE_IEEE80211, | |
891 | HOSTAPD_LEVEL_WARNING, | |
892 | "Hardware does not support configured mode (%d) (hw_mode in hostapd.conf)", | |
893 | (int) iface->conf->hw_mode); | |
894 | return -2; | |
895 | } | |
6fc6879b JM |
896 | } |
897 | ||
245e026e MK |
898 | switch (hostapd_check_chans(iface)) { |
899 | case HOSTAPD_CHAN_VALID: | |
900 | return 0; | |
50f4f2a0 MK |
901 | case HOSTAPD_CHAN_ACS: /* ACS will run and later complete */ |
902 | return 1; | |
245e026e MK |
903 | case HOSTAPD_CHAN_INVALID: |
904 | default: | |
905 | hostapd_notify_bad_chans(iface); | |
60eda5e4 | 906 | return -3; |
24c9fceb | 907 | } |
6fc6879b JM |
908 | } |
909 | ||
910 | ||
911 | const char * hostapd_hw_mode_txt(int mode) | |
912 | { | |
913 | switch (mode) { | |
914 | case HOSTAPD_MODE_IEEE80211A: | |
915 | return "IEEE 802.11a"; | |
916 | case HOSTAPD_MODE_IEEE80211B: | |
917 | return "IEEE 802.11b"; | |
918 | case HOSTAPD_MODE_IEEE80211G: | |
919 | return "IEEE 802.11g"; | |
7829894c VK |
920 | case HOSTAPD_MODE_IEEE80211AD: |
921 | return "IEEE 802.11ad"; | |
6fc6879b JM |
922 | default: |
923 | return "UNKNOWN"; | |
924 | } | |
925 | } | |
926 | ||
927 | ||
928 | int hostapd_hw_get_freq(struct hostapd_data *hapd, int chan) | |
929 | { | |
5f10b7f4 | 930 | return hw_get_freq(hapd->iface->current_mode, chan); |
6fc6879b JM |
931 | } |
932 | ||
933 | ||
934 | int hostapd_hw_get_channel(struct hostapd_data *hapd, int freq) | |
935 | { | |
5b9f46df PX |
936 | int i, channel; |
937 | struct hostapd_hw_modes *mode; | |
938 | ||
eb314e8a JM |
939 | if (hapd->iface->current_mode) { |
940 | channel = hw_get_chan(hapd->iface->current_mode, freq); | |
941 | if (channel) | |
942 | return channel; | |
943 | } | |
944 | ||
5b9f46df PX |
945 | /* Check other available modes since the channel list for the current |
946 | * mode did not include the specified frequency. */ | |
eb314e8a JM |
947 | if (!hapd->iface->hw_features) |
948 | return 0; | |
5b9f46df PX |
949 | for (i = 0; i < hapd->iface->num_hw_features; i++) { |
950 | mode = &hapd->iface->hw_features[i]; | |
951 | channel = hw_get_chan(mode, freq); | |
952 | if (channel) | |
953 | return channel; | |
954 | } | |
955 | return 0; | |
6fc6879b | 956 | } |