]>
Commit | Line | Data |
---|---|---|
6fc6879b JM |
1 | /* |
2 | * WPA Supplicant - Scanning | |
9ba9fa07 | 3 | * Copyright (c) 2003-2010, Jouni Malinen <j@w1.fi> |
6fc6879b JM |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License version 2 as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * Alternatively, this software may be distributed under the terms of BSD | |
10 | * license. | |
11 | * | |
12 | * See README and COPYING for more details. | |
13 | */ | |
14 | ||
9ba9fa07 | 15 | #include "utils/includes.h" |
6fc6879b | 16 | |
9ba9fa07 JM |
17 | #include "utils/common.h" |
18 | #include "utils/eloop.h" | |
19 | #include "common/ieee802_11_defs.h" | |
6fc6879b JM |
20 | #include "config.h" |
21 | #include "wpa_supplicant_i.h" | |
2d5b792d | 22 | #include "driver_i.h" |
b01c18a8 | 23 | #include "wps_supplicant.h" |
0e65037c JM |
24 | #include "p2p_supplicant.h" |
25 | #include "p2p/p2p.h" | |
8bac466b | 26 | #include "notify.h" |
9ba9fa07 JM |
27 | #include "bss.h" |
28 | #include "scan.h" | |
6fc6879b JM |
29 | |
30 | ||
31 | static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s) | |
32 | { | |
33 | struct wpa_ssid *ssid; | |
34 | union wpa_event_data data; | |
35 | ||
36 | ssid = wpa_supplicant_get_ssid(wpa_s); | |
37 | if (ssid == NULL) | |
38 | return; | |
39 | ||
8bac466b | 40 | if (wpa_s->current_ssid == NULL) { |
6fc6879b | 41 | wpa_s->current_ssid = ssid; |
8bac466b JM |
42 | if (wpa_s->current_ssid != NULL) |
43 | wpas_notify_network_changed(wpa_s); | |
44 | } | |
6fc6879b | 45 | wpa_supplicant_initiate_eapol(wpa_s); |
f049052b BG |
46 | wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with a configured " |
47 | "network - generating associated event"); | |
6fc6879b JM |
48 | os_memset(&data, 0, sizeof(data)); |
49 | wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data); | |
50 | } | |
51 | ||
52 | ||
ad08c363 | 53 | #ifdef CONFIG_WPS |
5f738a21 | 54 | static int wpas_wps_in_use(struct wpa_supplicant *wpa_s, |
f90c86d4 | 55 | enum wps_request_type *req_type) |
ad08c363 JM |
56 | { |
57 | struct wpa_ssid *ssid; | |
58 | int wps = 0; | |
ad08c363 | 59 | |
5f738a21 | 60 | for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) { |
ad08c363 JM |
61 | if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS)) |
62 | continue; | |
63 | ||
64 | wps = 1; | |
b01c18a8 | 65 | *req_type = wpas_wps_get_req_type(ssid); |
ad08c363 JM |
66 | if (!ssid->eap.phase1) |
67 | continue; | |
68 | ||
ad08c363 JM |
69 | if (os_strstr(ssid->eap.phase1, "pbc=1")) |
70 | return 2; | |
71 | } | |
72 | ||
5f738a21 LC |
73 | #ifdef CONFIG_P2P |
74 | wpa_s->wps->dev.p2p = 1; | |
75 | if (!wps) { | |
76 | wps = 1; | |
77 | *req_type = WPS_REQ_ENROLLEE_INFO; | |
78 | } | |
79 | #endif /* CONFIG_P2P */ | |
80 | ||
ad08c363 JM |
81 | return wps; |
82 | } | |
83 | #endif /* CONFIG_WPS */ | |
84 | ||
e76baaac | 85 | |
4f34d51a | 86 | int wpa_supplicant_enabled_networks(struct wpa_config *conf) |
e76baaac JM |
87 | { |
88 | struct wpa_ssid *ssid = conf->ssid; | |
5471c343 | 89 | int count = 0; |
e76baaac JM |
90 | while (ssid) { |
91 | if (!ssid->disabled) | |
5471c343 | 92 | count++; |
e76baaac JM |
93 | ssid = ssid->next; |
94 | } | |
5471c343 | 95 | return count; |
e76baaac JM |
96 | } |
97 | ||
98 | ||
99 | static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s, | |
100 | struct wpa_ssid *ssid) | |
101 | { | |
102 | while (ssid) { | |
103 | if (!ssid->disabled) | |
104 | break; | |
105 | ssid = ssid->next; | |
106 | } | |
107 | ||
108 | /* ap_scan=2 mode - try to associate with each SSID. */ | |
109 | if (ssid == NULL) { | |
f049052b BG |
110 | wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached " |
111 | "end of scan list - go back to beginning"); | |
ba2a573c | 112 | wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN; |
e76baaac JM |
113 | wpa_supplicant_req_scan(wpa_s, 0, 0); |
114 | return; | |
115 | } | |
116 | if (ssid->next) { | |
117 | /* Continue from the next SSID on the next attempt. */ | |
118 | wpa_s->prev_scan_ssid = ssid; | |
119 | } else { | |
120 | /* Start from the beginning of the SSID list. */ | |
ba2a573c | 121 | wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN; |
e76baaac JM |
122 | } |
123 | wpa_supplicant_associate(wpa_s, NULL, ssid); | |
124 | } | |
125 | ||
126 | ||
d3a98225 JM |
127 | static int int_array_len(const int *a) |
128 | { | |
129 | int i; | |
130 | for (i = 0; a && a[i]; i++) | |
131 | ; | |
132 | return i; | |
133 | } | |
134 | ||
135 | ||
136 | static void int_array_concat(int **res, const int *a) | |
137 | { | |
138 | int reslen, alen, i; | |
139 | int *n; | |
140 | ||
141 | reslen = int_array_len(*res); | |
142 | alen = int_array_len(a); | |
143 | ||
144 | n = os_realloc(*res, (reslen + alen + 1) * sizeof(int)); | |
145 | if (n == NULL) { | |
146 | os_free(*res); | |
147 | *res = NULL; | |
e6c0ebff | 148 | return; |
d3a98225 JM |
149 | } |
150 | for (i = 0; i <= alen; i++) | |
151 | n[reslen + i] = a[i]; | |
152 | *res = n; | |
153 | } | |
154 | ||
155 | ||
156 | static int freq_cmp(const void *a, const void *b) | |
157 | { | |
158 | int _a = *(int *) a; | |
159 | int _b = *(int *) b; | |
160 | ||
161 | if (_a == 0) | |
162 | return 1; | |
163 | if (_b == 0) | |
164 | return -1; | |
165 | return _a - _b; | |
166 | } | |
167 | ||
168 | ||
169 | static void int_array_sort_unique(int *a) | |
170 | { | |
171 | int alen; | |
172 | int i, j; | |
173 | ||
174 | if (a == NULL) | |
175 | return; | |
176 | ||
177 | alen = int_array_len(a); | |
178 | qsort(a, alen, sizeof(int), freq_cmp); | |
179 | ||
180 | i = 0; | |
181 | j = 1; | |
182 | while (a[i] && a[j]) { | |
183 | if (a[i] == a[j]) { | |
184 | j++; | |
185 | continue; | |
186 | } | |
187 | a[++i] = a[j++]; | |
188 | } | |
189 | if (a[i]) | |
190 | i++; | |
191 | a[i] = 0; | |
192 | } | |
193 | ||
194 | ||
60b94c98 JM |
195 | int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s, |
196 | struct wpa_driver_scan_params *params) | |
197 | { | |
198 | int ret; | |
199 | ||
200 | wpa_supplicant_notify_scanning(wpa_s, 1); | |
201 | ||
17fbb751 | 202 | ret = wpa_drv_scan(wpa_s, params); |
60b94c98 JM |
203 | if (ret) { |
204 | wpa_supplicant_notify_scanning(wpa_s, 0); | |
205 | wpas_notify_scan_done(wpa_s, 0); | |
206 | } else | |
207 | wpa_s->scan_runs++; | |
208 | ||
209 | return ret; | |
210 | } | |
211 | ||
212 | ||
6a90053c LC |
213 | static void |
214 | wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx) | |
215 | { | |
216 | struct wpa_supplicant *wpa_s = eloop_ctx; | |
217 | ||
218 | wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan"); | |
219 | ||
220 | if (wpa_supplicant_req_sched_scan(wpa_s)) | |
221 | wpa_supplicant_req_scan(wpa_s, 0, 0); | |
222 | } | |
223 | ||
224 | ||
cbdf3507 LC |
225 | static void |
226 | wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx) | |
227 | { | |
228 | struct wpa_supplicant *wpa_s = eloop_ctx; | |
229 | ||
230 | wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it"); | |
231 | ||
232 | wpa_s->sched_scan_timed_out = 1; | |
233 | wpa_supplicant_cancel_sched_scan(wpa_s); | |
234 | } | |
235 | ||
236 | ||
237 | static int | |
238 | wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s, | |
239 | struct wpa_driver_scan_params *params, | |
240 | int interval) | |
241 | { | |
242 | int ret; | |
243 | ||
cbdf3507 LC |
244 | wpa_supplicant_notify_scanning(wpa_s, 1); |
245 | ret = wpa_drv_sched_scan(wpa_s, params, interval * 1000); | |
246 | if (ret) | |
247 | wpa_supplicant_notify_scanning(wpa_s, 0); | |
248 | else | |
249 | wpa_s->sched_scanning = 1; | |
250 | ||
251 | return ret; | |
252 | } | |
253 | ||
254 | ||
255 | static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s) | |
256 | { | |
257 | int ret; | |
258 | ||
259 | ret = wpa_drv_stop_sched_scan(wpa_s); | |
260 | if (ret) { | |
261 | wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!"); | |
262 | /* TODO: what to do if stopping fails? */ | |
263 | return -1; | |
264 | } | |
265 | ||
266 | return ret; | |
267 | } | |
268 | ||
269 | ||
3812464c JM |
270 | static struct wpa_driver_scan_filter * |
271 | wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids) | |
272 | { | |
273 | struct wpa_driver_scan_filter *ssids; | |
274 | struct wpa_ssid *ssid; | |
275 | size_t count; | |
276 | ||
277 | *num_ssids = 0; | |
278 | if (!conf->filter_ssids) | |
279 | return NULL; | |
280 | ||
281 | for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) { | |
282 | if (ssid->ssid && ssid->ssid_len) | |
283 | count++; | |
284 | } | |
285 | if (count == 0) | |
286 | return NULL; | |
287 | ssids = os_zalloc(count * sizeof(struct wpa_driver_scan_filter)); | |
288 | if (ssids == NULL) | |
289 | return NULL; | |
290 | ||
291 | for (ssid = conf->ssid; ssid; ssid = ssid->next) { | |
292 | if (!ssid->ssid || !ssid->ssid_len) | |
293 | continue; | |
294 | os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len); | |
295 | ssids[*num_ssids].ssid_len = ssid->ssid_len; | |
296 | (*num_ssids)++; | |
297 | } | |
298 | ||
299 | return ssids; | |
300 | } | |
301 | ||
302 | ||
5f738a21 LC |
303 | static void wpa_supplicant_optimize_freqs( |
304 | struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params) | |
305 | { | |
306 | #ifdef CONFIG_P2P | |
307 | if (params->freqs == NULL && wpa_s->p2p_in_provisioning && | |
308 | wpa_s->go_params) { | |
309 | /* Optimize provisioning state scan based on GO information */ | |
310 | if (wpa_s->p2p_in_provisioning < 5 && | |
311 | wpa_s->go_params->freq > 0) { | |
312 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO " | |
313 | "preferred frequency %d MHz", | |
314 | wpa_s->go_params->freq); | |
315 | params->freqs = os_zalloc(2 * sizeof(int)); | |
316 | if (params->freqs) | |
317 | params->freqs[0] = wpa_s->go_params->freq; | |
318 | } else if (wpa_s->p2p_in_provisioning < 8 && | |
319 | wpa_s->go_params->freq_list[0]) { | |
320 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common " | |
321 | "channels"); | |
322 | int_array_concat(¶ms->freqs, | |
323 | wpa_s->go_params->freq_list); | |
324 | if (params->freqs) | |
325 | int_array_sort_unique(params->freqs); | |
326 | } | |
327 | wpa_s->p2p_in_provisioning++; | |
328 | } | |
329 | #endif /* CONFIG_P2P */ | |
330 | ||
331 | #ifdef CONFIG_WPS | |
332 | if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) { | |
333 | /* | |
334 | * Optimize post-provisioning scan based on channel used | |
335 | * during provisioning. | |
336 | */ | |
337 | wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz " | |
338 | "that was used during provisioning", wpa_s->wps_freq); | |
339 | params->freqs = os_zalloc(2 * sizeof(int)); | |
340 | if (params->freqs) | |
341 | params->freqs[0] = wpa_s->wps_freq; | |
342 | wpa_s->after_wps--; | |
343 | } | |
344 | ||
345 | #endif /* CONFIG_WPS */ | |
346 | } | |
347 | ||
348 | ||
46ee0427 JM |
349 | #ifdef CONFIG_INTERWORKING |
350 | static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s, | |
351 | struct wpabuf *buf) | |
352 | { | |
353 | if (wpa_s->conf->interworking == 0) | |
354 | return; | |
355 | ||
356 | wpabuf_put_u8(buf, WLAN_EID_EXT_CAPAB); | |
357 | wpabuf_put_u8(buf, 4); | |
358 | wpabuf_put_u8(buf, 0x00); | |
359 | wpabuf_put_u8(buf, 0x00); | |
360 | wpabuf_put_u8(buf, 0x00); | |
361 | wpabuf_put_u8(buf, 0x80); /* Bit 31 - Interworking */ | |
362 | ||
363 | wpabuf_put_u8(buf, WLAN_EID_INTERWORKING); | |
364 | wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 : | |
365 | 1 + ETH_ALEN); | |
11540c0b | 366 | wpabuf_put_u8(buf, wpa_s->conf->access_network_type); |
46ee0427 JM |
367 | /* No Venue Info */ |
368 | if (!is_zero_ether_addr(wpa_s->conf->hessid)) | |
369 | wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN); | |
370 | } | |
371 | #endif /* CONFIG_INTERWORKING */ | |
372 | ||
373 | ||
5f738a21 LC |
374 | static struct wpabuf * |
375 | wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s, | |
376 | struct wpa_driver_scan_params *params) | |
6fc6879b | 377 | { |
46ee0427 | 378 | struct wpabuf *extra_ie = NULL; |
b01c18a8 | 379 | #ifdef CONFIG_WPS |
509a3972 | 380 | int wps = 0; |
f90c86d4 | 381 | enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO; |
46ee0427 JM |
382 | #endif /* CONFIG_WPS */ |
383 | ||
384 | #ifdef CONFIG_INTERWORKING | |
385 | if (wpa_s->conf->interworking && | |
386 | wpabuf_resize(&extra_ie, 100) == 0) | |
387 | wpas_add_interworking_elements(wpa_s, extra_ie); | |
388 | #endif /* CONFIG_INTERWORKING */ | |
5f738a21 | 389 | |
46ee0427 | 390 | #ifdef CONFIG_WPS |
5f738a21 LC |
391 | wps = wpas_wps_in_use(wpa_s, &req_type); |
392 | ||
393 | if (wps) { | |
46ee0427 | 394 | struct wpabuf *wps_ie; |
5f738a21 LC |
395 | wps_ie = wps_build_probe_req_ie(wps == 2, &wpa_s->wps->dev, |
396 | wpa_s->wps->uuid, req_type, | |
397 | 0, NULL); | |
398 | if (wps_ie) { | |
46ee0427 JM |
399 | if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0) |
400 | wpabuf_put_buf(extra_ie, wps_ie); | |
401 | wpabuf_free(wps_ie); | |
5f738a21 LC |
402 | } |
403 | } | |
404 | ||
405 | #ifdef CONFIG_P2P | |
46ee0427 | 406 | if (wps) { |
5f738a21 | 407 | size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p); |
46ee0427 JM |
408 | if (wpabuf_resize(&extra_ie, ielen) == 0) |
409 | wpas_p2p_scan_ie(wpa_s, extra_ie); | |
5f738a21 LC |
410 | } |
411 | #endif /* CONFIG_P2P */ | |
412 | ||
b01c18a8 | 413 | #endif /* CONFIG_WPS */ |
5f738a21 | 414 | |
46ee0427 | 415 | return extra_ie; |
5f738a21 LC |
416 | } |
417 | ||
418 | ||
419 | static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx) | |
420 | { | |
421 | struct wpa_supplicant *wpa_s = eloop_ctx; | |
422 | struct wpa_ssid *ssid; | |
423 | int scan_req = 0, ret; | |
46ee0427 | 424 | struct wpabuf *extra_ie; |
e76baaac JM |
425 | struct wpa_driver_scan_params params; |
426 | size_t max_ssids; | |
207ef3fb | 427 | enum wpa_states prev_state; |
6fc6879b | 428 | |
8401a6b0 | 429 | if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) { |
f049052b | 430 | wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled"); |
8401a6b0 JM |
431 | return; |
432 | } | |
433 | ||
3180d7a2 SO |
434 | if (wpa_s->disconnected && !wpa_s->scan_req) { |
435 | wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED); | |
6fc6879b | 436 | return; |
3180d7a2 | 437 | } |
6fc6879b | 438 | |
e76baaac JM |
439 | if (!wpa_supplicant_enabled_networks(wpa_s->conf) && |
440 | !wpa_s->scan_req) { | |
f049052b | 441 | wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan"); |
6fc6879b JM |
442 | wpa_supplicant_set_state(wpa_s, WPA_INACTIVE); |
443 | return; | |
444 | } | |
6fc6879b | 445 | |
c2a04078 JM |
446 | if (wpa_s->conf->ap_scan != 0 && |
447 | (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) { | |
f049052b BG |
448 | wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - " |
449 | "overriding ap_scan configuration"); | |
6fc6879b | 450 | wpa_s->conf->ap_scan = 0; |
8bac466b | 451 | wpas_notify_ap_scan_changed(wpa_s); |
6fc6879b JM |
452 | } |
453 | ||
454 | if (wpa_s->conf->ap_scan == 0) { | |
455 | wpa_supplicant_gen_assoc_event(wpa_s); | |
456 | return; | |
457 | } | |
458 | ||
303f60d3 JM |
459 | #ifdef CONFIG_P2P |
460 | if (wpas_p2p_in_progress(wpa_s)) { | |
461 | if (wpa_s->wpa_state == WPA_SCANNING) { | |
462 | wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan " | |
463 | "while P2P operation is in progress"); | |
464 | wpa_supplicant_req_scan(wpa_s, 5, 0); | |
465 | } else { | |
466 | wpa_dbg(wpa_s, MSG_DEBUG, "Do not request scan while " | |
467 | "P2P operation is in progress"); | |
468 | } | |
469 | return; | |
470 | } | |
471 | #endif /* CONFIG_P2P */ | |
472 | ||
17fbb751 | 473 | if (wpa_s->conf->ap_scan == 2) |
e76baaac JM |
474 | max_ssids = 1; |
475 | else { | |
476 | max_ssids = wpa_s->max_scan_ssids; | |
477 | if (max_ssids > WPAS_MAX_SCAN_SSIDS) | |
478 | max_ssids = WPAS_MAX_SCAN_SSIDS; | |
479 | } | |
480 | ||
e76baaac JM |
481 | scan_req = wpa_s->scan_req; |
482 | wpa_s->scan_req = 0; | |
483 | ||
484 | os_memset(¶ms, 0, sizeof(params)); | |
485 | ||
207ef3fb | 486 | prev_state = wpa_s->wpa_state; |
6fc6879b JM |
487 | if (wpa_s->wpa_state == WPA_DISCONNECTED || |
488 | wpa_s->wpa_state == WPA_INACTIVE) | |
489 | wpa_supplicant_set_state(wpa_s, WPA_SCANNING); | |
490 | ||
7dcdcfd6 JM |
491 | if (scan_req != 2 && wpa_s->connect_without_scan) { |
492 | for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) { | |
493 | if (ssid == wpa_s->connect_without_scan) | |
494 | break; | |
495 | } | |
496 | wpa_s->connect_without_scan = NULL; | |
497 | if (ssid) { | |
498 | wpa_printf(MSG_DEBUG, "Start a pre-selected network " | |
499 | "without scan step"); | |
500 | wpa_supplicant_associate(wpa_s, NULL, ssid); | |
501 | return; | |
502 | } | |
503 | } | |
504 | ||
e76baaac | 505 | /* Find the starting point from which to continue scanning */ |
6fc6879b | 506 | ssid = wpa_s->conf->ssid; |
ba2a573c | 507 | if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) { |
6fc6879b JM |
508 | while (ssid) { |
509 | if (ssid == wpa_s->prev_scan_ssid) { | |
510 | ssid = ssid->next; | |
511 | break; | |
512 | } | |
513 | ssid = ssid->next; | |
514 | } | |
515 | } | |
6fc6879b | 516 | |
7dcdcfd6 JM |
517 | if (scan_req != 2 && wpa_s->conf->ap_scan == 2) { |
518 | wpa_s->connect_without_scan = NULL; | |
e76baaac JM |
519 | wpa_supplicant_assoc_try(wpa_s, ssid); |
520 | return; | |
521 | } else if (wpa_s->conf->ap_scan == 2) { | |
6fc6879b | 522 | /* |
ba2a573c JM |
523 | * User-initiated scan request in ap_scan == 2; scan with |
524 | * wildcard SSID. | |
6fc6879b | 525 | */ |
e76baaac JM |
526 | ssid = NULL; |
527 | } else { | |
5be45e2e | 528 | struct wpa_ssid *start = ssid, *tssid; |
d3a98225 | 529 | int freqs_set = 0; |
e76baaac JM |
530 | if (ssid == NULL && max_ssids > 1) |
531 | ssid = wpa_s->conf->ssid; | |
532 | while (ssid) { | |
533 | if (!ssid->disabled && ssid->scan_ssid) { | |
534 | wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID", | |
535 | ssid->ssid, ssid->ssid_len); | |
536 | params.ssids[params.num_ssids].ssid = | |
537 | ssid->ssid; | |
538 | params.ssids[params.num_ssids].ssid_len = | |
539 | ssid->ssid_len; | |
540 | params.num_ssids++; | |
541 | if (params.num_ssids + 1 >= max_ssids) | |
542 | break; | |
543 | } | |
544 | ssid = ssid->next; | |
545 | if (ssid == start) | |
546 | break; | |
547 | if (ssid == NULL && max_ssids > 1 && | |
548 | start != wpa_s->conf->ssid) | |
549 | ssid = wpa_s->conf->ssid; | |
6fc6879b | 550 | } |
d3a98225 | 551 | |
5be45e2e JM |
552 | for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) { |
553 | if (tssid->disabled) | |
d3a98225 | 554 | continue; |
5be45e2e | 555 | if ((params.freqs || !freqs_set) && tssid->scan_freq) { |
d3a98225 | 556 | int_array_concat(¶ms.freqs, |
5be45e2e | 557 | tssid->scan_freq); |
d3a98225 JM |
558 | } else { |
559 | os_free(params.freqs); | |
560 | params.freqs = NULL; | |
561 | } | |
562 | freqs_set = 1; | |
563 | } | |
564 | int_array_sort_unique(params.freqs); | |
6fc6879b JM |
565 | } |
566 | ||
6fc6879b | 567 | if (ssid) { |
6fc6879b | 568 | wpa_s->prev_scan_ssid = ssid; |
e76baaac | 569 | if (max_ssids > 1) { |
f049052b BG |
570 | wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in " |
571 | "the scan request"); | |
ba2a573c | 572 | params.num_ssids++; |
e76baaac | 573 | } |
f049052b BG |
574 | wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for specific " |
575 | "SSID(s)"); | |
e76baaac | 576 | } else { |
ba2a573c | 577 | wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN; |
e76baaac | 578 | params.num_ssids++; |
f049052b BG |
579 | wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard " |
580 | "SSID"); | |
6fc6879b JM |
581 | } |
582 | ||
5f738a21 | 583 | wpa_supplicant_optimize_freqs(wpa_s, ¶ms); |
46ee0427 | 584 | extra_ie = wpa_supplicant_extra_ies(wpa_s, ¶ms); |
0e65037c | 585 | |
f47d639d | 586 | if (params.freqs == NULL && wpa_s->next_scan_freqs) { |
f049052b BG |
587 | wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously " |
588 | "generated frequency list"); | |
f47d639d JM |
589 | params.freqs = wpa_s->next_scan_freqs; |
590 | } else | |
591 | os_free(wpa_s->next_scan_freqs); | |
592 | wpa_s->next_scan_freqs = NULL; | |
593 | ||
3812464c JM |
594 | params.filter_ssids = wpa_supplicant_build_filter_ssids( |
595 | wpa_s->conf, ¶ms.num_filter_ssids); | |
46ee0427 JM |
596 | if (extra_ie) { |
597 | params.extra_ies = wpabuf_head(extra_ie); | |
598 | params.extra_ies_len = wpabuf_len(extra_ie); | |
599 | } | |
3812464c | 600 | |
d1dd48e3 JM |
601 | #ifdef CONFIG_P2P |
602 | if (wpa_s->p2p_in_provisioning) { | |
603 | /* | |
604 | * The interface may not yet be in P2P mode, so we have to | |
605 | * explicitly request P2P probe to disable CCK rates. | |
606 | */ | |
607 | params.p2p_probe = 1; | |
608 | } | |
609 | #endif /* CONFIG_P2P */ | |
610 | ||
60b94c98 | 611 | ret = wpa_supplicant_trigger_scan(wpa_s, ¶ms); |
6fc6879b | 612 | |
46ee0427 | 613 | wpabuf_free(extra_ie); |
d3a98225 | 614 | os_free(params.freqs); |
3812464c | 615 | os_free(params.filter_ssids); |
ad08c363 | 616 | |
6fc6879b | 617 | if (ret) { |
f049052b | 618 | wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan"); |
207ef3fb JM |
619 | if (prev_state != wpa_s->wpa_state) |
620 | wpa_supplicant_set_state(wpa_s, prev_state); | |
1c4c9c50 | 621 | wpa_supplicant_req_scan(wpa_s, 1, 0); |
d902a9c1 | 622 | } |
6fc6879b JM |
623 | } |
624 | ||
625 | ||
626 | /** | |
627 | * wpa_supplicant_req_scan - Schedule a scan for neighboring access points | |
628 | * @wpa_s: Pointer to wpa_supplicant data | |
629 | * @sec: Number of seconds after which to scan | |
630 | * @usec: Number of microseconds after which to scan | |
631 | * | |
632 | * This function is used to schedule a scan for neighboring access points after | |
633 | * the specified time. | |
634 | */ | |
635 | void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec) | |
636 | { | |
7e148849 DW |
637 | /* If there's at least one network that should be specifically scanned |
638 | * then don't cancel the scan and reschedule. Some drivers do | |
639 | * background scanning which generates frequent scan results, and that | |
640 | * causes the specific SSID scan to get continually pushed back and | |
641 | * never happen, which causes hidden APs to never get probe-scanned. | |
642 | */ | |
643 | if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) && | |
644 | wpa_s->conf->ap_scan == 1) { | |
645 | struct wpa_ssid *ssid = wpa_s->conf->ssid; | |
646 | ||
647 | while (ssid) { | |
648 | if (!ssid->disabled && ssid->scan_ssid) | |
649 | break; | |
650 | ssid = ssid->next; | |
651 | } | |
652 | if (ssid) { | |
f049052b | 653 | wpa_dbg(wpa_s, MSG_DEBUG, "Not rescheduling scan to " |
7e148849 DW |
654 | "ensure that specific SSID scans occur"); |
655 | return; | |
656 | } | |
657 | } | |
658 | ||
f049052b | 659 | wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec", |
6fc6879b JM |
660 | sec, usec); |
661 | eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL); | |
662 | eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL); | |
663 | } | |
664 | ||
665 | ||
6a90053c LC |
666 | /** |
667 | * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan | |
668 | * @wpa_s: Pointer to wpa_supplicant data | |
669 | * @sec: Number of seconds after which to scan | |
670 | * @usec: Number of microseconds after which to scan | |
671 | * | |
672 | * This function is used to schedule periodic scans for neighboring | |
673 | * access points after the specified time. | |
674 | */ | |
675 | int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s, | |
676 | int sec, int usec) | |
677 | { | |
678 | if (!wpa_s->sched_scan_supported) | |
679 | return -1; | |
680 | ||
681 | eloop_register_timeout(sec, usec, | |
682 | wpa_supplicant_delayed_sched_scan_timeout, | |
683 | wpa_s, NULL); | |
684 | ||
685 | return 0; | |
686 | } | |
687 | ||
688 | ||
cbdf3507 LC |
689 | /** |
690 | * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan | |
691 | * @wpa_s: Pointer to wpa_supplicant data | |
692 | * | |
693 | * This function is used to schedule periodic scans for neighboring | |
694 | * access points repeating the scan continuously. | |
695 | */ | |
696 | int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s) | |
697 | { | |
698 | struct wpa_driver_scan_params params; | |
699 | enum wpa_states prev_state; | |
700 | struct wpa_ssid *ssid; | |
701 | struct wpabuf *wps_ie = NULL; | |
702 | int ret; | |
cbdf3507 LC |
703 | unsigned int max_sched_scan_ssids; |
704 | ||
705 | if (!wpa_s->sched_scan_supported) | |
706 | return -1; | |
707 | ||
708 | if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS) | |
709 | max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS; | |
710 | else | |
711 | max_sched_scan_ssids = wpa_s->max_sched_scan_ssids; | |
712 | ||
1966e3d1 ES |
713 | if (wpa_s->sched_scanning) { |
714 | wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning"); | |
cbdf3507 | 715 | return 0; |
1966e3d1 | 716 | } |
cbdf3507 LC |
717 | |
718 | os_memset(¶ms, 0, sizeof(params)); | |
719 | ||
b59e6f26 LC |
720 | /* If we can't allocate space for the filters, we just don't filter */ |
721 | params.filter_ssids = os_zalloc(wpa_s->max_match_sets * | |
722 | sizeof(struct wpa_driver_scan_filter)); | |
723 | ||
cbdf3507 LC |
724 | prev_state = wpa_s->wpa_state; |
725 | if (wpa_s->wpa_state == WPA_DISCONNECTED || | |
726 | wpa_s->wpa_state == WPA_INACTIVE) | |
727 | wpa_supplicant_set_state(wpa_s, WPA_SCANNING); | |
728 | ||
729 | /* Find the starting point from which to continue scanning */ | |
730 | ssid = wpa_s->conf->ssid; | |
731 | if (wpa_s->prev_sched_ssid) { | |
732 | while (ssid) { | |
733 | if (ssid == wpa_s->prev_sched_ssid) { | |
734 | ssid = ssid->next; | |
735 | break; | |
736 | } | |
737 | ssid = ssid->next; | |
738 | } | |
739 | } | |
740 | ||
741 | if (!ssid || !wpa_s->prev_sched_ssid) { | |
742 | wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list"); | |
743 | ||
744 | wpa_s->sched_scan_interval = 2; | |
745 | wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2; | |
746 | wpa_s->first_sched_scan = 1; | |
747 | ssid = wpa_s->conf->ssid; | |
748 | wpa_s->prev_sched_ssid = ssid; | |
749 | } | |
750 | ||
751 | while (ssid) { | |
752 | if (ssid->disabled) { | |
753 | wpa_s->prev_sched_ssid = ssid; | |
754 | ssid = ssid->next; | |
755 | continue; | |
756 | } | |
757 | ||
fcd16847 JM |
758 | if (params.num_filter_ssids < wpa_s->max_match_sets && |
759 | params.filter_ssids && ssid->ssid && ssid->ssid_len) { | |
1966e3d1 ES |
760 | wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s", |
761 | wpa_ssid_txt(ssid->ssid, ssid->ssid_len)); | |
b59e6f26 LC |
762 | os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid, |
763 | ssid->ssid, ssid->ssid_len); | |
764 | params.filter_ssids[params.num_filter_ssids].ssid_len = | |
765 | ssid->ssid_len; | |
766 | params.num_filter_ssids++; | |
767 | } | |
768 | ||
3f56f3a4 JM |
769 | if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) { |
770 | wpa_dbg(wpa_s, MSG_DEBUG, | |
771 | "add to active scan ssid: %s", | |
1966e3d1 | 772 | wpa_ssid_txt(ssid->ssid, ssid->ssid_len)); |
cbdf3507 LC |
773 | params.ssids[params.num_ssids].ssid = |
774 | ssid->ssid; | |
775 | params.ssids[params.num_ssids].ssid_len = | |
776 | ssid->ssid_len; | |
777 | params.num_ssids++; | |
b59e6f26 | 778 | if (params.num_ssids >= max_sched_scan_ssids) { |
cbdf3507 LC |
779 | wpa_s->prev_sched_ssid = ssid; |
780 | break; | |
781 | } | |
782 | } | |
b59e6f26 LC |
783 | |
784 | if (params.num_filter_ssids >= wpa_s->max_match_sets) | |
785 | break; | |
cbdf3507 LC |
786 | wpa_s->prev_sched_ssid = ssid; |
787 | ssid = ssid->next; | |
788 | } | |
789 | ||
7c6a266c JM |
790 | if (params.num_filter_ssids == 0) { |
791 | os_free(params.filter_ssids); | |
792 | params.filter_ssids = NULL; | |
793 | } | |
794 | ||
cbdf3507 LC |
795 | if (wpa_s->wps) |
796 | wps_ie = wpa_supplicant_extra_ies(wpa_s, ¶ms); | |
797 | ||
798 | wpa_dbg(wpa_s, MSG_DEBUG, | |
799 | "Starting sched scan: interval %d timeout %d", | |
800 | wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout); | |
801 | ||
802 | ret = wpa_supplicant_start_sched_scan(wpa_s, ¶ms, | |
803 | wpa_s->sched_scan_interval); | |
804 | wpabuf_free(wps_ie); | |
b59e6f26 | 805 | os_free(params.filter_ssids); |
cbdf3507 LC |
806 | if (ret) { |
807 | wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan"); | |
808 | if (prev_state != wpa_s->wpa_state) | |
809 | wpa_supplicant_set_state(wpa_s, prev_state); | |
810 | return ret; | |
811 | } | |
812 | ||
813 | /* If we have more SSIDs to scan, add a timeout so we scan them too */ | |
814 | if (ssid || !wpa_s->first_sched_scan) { | |
815 | wpa_s->sched_scan_timed_out = 0; | |
816 | eloop_register_timeout(wpa_s->sched_scan_timeout, 0, | |
817 | wpa_supplicant_sched_scan_timeout, | |
818 | wpa_s, NULL); | |
819 | wpa_s->first_sched_scan = 0; | |
820 | wpa_s->sched_scan_timeout /= 2; | |
821 | wpa_s->sched_scan_interval *= 2; | |
822 | } | |
823 | ||
824 | return 0; | |
825 | } | |
826 | ||
827 | ||
6fc6879b JM |
828 | /** |
829 | * wpa_supplicant_cancel_scan - Cancel a scheduled scan request | |
830 | * @wpa_s: Pointer to wpa_supplicant data | |
831 | * | |
832 | * This function is used to cancel a scan request scheduled with | |
833 | * wpa_supplicant_req_scan(). | |
834 | */ | |
835 | void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s) | |
836 | { | |
f049052b | 837 | wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request"); |
6fc6879b JM |
838 | eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL); |
839 | } | |
cb8564b1 DW |
840 | |
841 | ||
cbdf3507 LC |
842 | /** |
843 | * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans | |
844 | * @wpa_s: Pointer to wpa_supplicant data | |
845 | * | |
846 | * This function is used to stop a periodic scheduled scan. | |
847 | */ | |
848 | void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s) | |
849 | { | |
850 | if (!wpa_s->sched_scanning) | |
851 | return; | |
852 | ||
853 | wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan"); | |
854 | eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL); | |
855 | wpa_supplicant_stop_sched_scan(wpa_s); | |
856 | } | |
857 | ||
858 | ||
cb8564b1 DW |
859 | void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s, |
860 | int scanning) | |
861 | { | |
862 | if (wpa_s->scanning != scanning) { | |
863 | wpa_s->scanning = scanning; | |
8bac466b | 864 | wpas_notify_scanning(wpa_s); |
cb8564b1 DW |
865 | } |
866 | } | |
867 | ||
9ba9fa07 JM |
868 | |
869 | static int wpa_scan_get_max_rate(const struct wpa_scan_res *res) | |
870 | { | |
871 | int rate = 0; | |
872 | const u8 *ie; | |
873 | int i; | |
874 | ||
875 | ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES); | |
876 | for (i = 0; ie && i < ie[1]; i++) { | |
877 | if ((ie[i + 2] & 0x7f) > rate) | |
878 | rate = ie[i + 2] & 0x7f; | |
879 | } | |
880 | ||
881 | ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES); | |
882 | for (i = 0; ie && i < ie[1]; i++) { | |
883 | if ((ie[i + 2] & 0x7f) > rate) | |
884 | rate = ie[i + 2] & 0x7f; | |
885 | } | |
886 | ||
887 | return rate; | |
888 | } | |
889 | ||
890 | ||
d1f9c410 JM |
891 | const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie) |
892 | { | |
893 | const u8 *end, *pos; | |
894 | ||
895 | pos = (const u8 *) (res + 1); | |
896 | end = pos + res->ie_len; | |
897 | ||
898 | while (pos + 1 < end) { | |
899 | if (pos + 2 + pos[1] > end) | |
900 | break; | |
901 | if (pos[0] == ie) | |
902 | return pos; | |
903 | pos += 2 + pos[1]; | |
904 | } | |
905 | ||
906 | return NULL; | |
907 | } | |
908 | ||
909 | ||
9ba9fa07 JM |
910 | const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res, |
911 | u32 vendor_type) | |
912 | { | |
913 | const u8 *end, *pos; | |
914 | ||
915 | pos = (const u8 *) (res + 1); | |
916 | end = pos + res->ie_len; | |
917 | ||
918 | while (pos + 1 < end) { | |
919 | if (pos + 2 + pos[1] > end) | |
920 | break; | |
921 | if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 && | |
922 | vendor_type == WPA_GET_BE32(&pos[2])) | |
923 | return pos; | |
924 | pos += 2 + pos[1]; | |
925 | } | |
926 | ||
927 | return NULL; | |
928 | } | |
929 | ||
930 | ||
931 | struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res, | |
932 | u32 vendor_type) | |
933 | { | |
934 | struct wpabuf *buf; | |
935 | const u8 *end, *pos; | |
936 | ||
937 | buf = wpabuf_alloc(res->ie_len); | |
938 | if (buf == NULL) | |
939 | return NULL; | |
940 | ||
941 | pos = (const u8 *) (res + 1); | |
942 | end = pos + res->ie_len; | |
943 | ||
54f489be JM |
944 | while (pos + 1 < end) { |
945 | if (pos + 2 + pos[1] > end) | |
946 | break; | |
947 | if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 && | |
948 | vendor_type == WPA_GET_BE32(&pos[2])) | |
949 | wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4); | |
950 | pos += 2 + pos[1]; | |
951 | } | |
952 | ||
953 | if (wpabuf_len(buf) == 0) { | |
954 | wpabuf_free(buf); | |
955 | buf = NULL; | |
956 | } | |
957 | ||
958 | return buf; | |
959 | } | |
960 | ||
961 | ||
962 | struct wpabuf * wpa_scan_get_vendor_ie_multi_beacon( | |
963 | const struct wpa_scan_res *res, u32 vendor_type) | |
964 | { | |
965 | struct wpabuf *buf; | |
966 | const u8 *end, *pos; | |
967 | ||
968 | if (res->beacon_ie_len == 0) | |
969 | return NULL; | |
970 | buf = wpabuf_alloc(res->beacon_ie_len); | |
971 | if (buf == NULL) | |
972 | return NULL; | |
973 | ||
974 | pos = (const u8 *) (res + 1); | |
975 | pos += res->ie_len; | |
976 | end = pos + res->beacon_ie_len; | |
977 | ||
9ba9fa07 JM |
978 | while (pos + 1 < end) { |
979 | if (pos + 2 + pos[1] > end) | |
980 | break; | |
981 | if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 && | |
982 | vendor_type == WPA_GET_BE32(&pos[2])) | |
983 | wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4); | |
984 | pos += 2 + pos[1]; | |
985 | } | |
986 | ||
987 | if (wpabuf_len(buf) == 0) { | |
988 | wpabuf_free(buf); | |
989 | buf = NULL; | |
990 | } | |
991 | ||
992 | return buf; | |
993 | } | |
994 | ||
995 | ||
577db0ae GM |
996 | /* |
997 | * Channels with a great SNR can operate at full rate. What is a great SNR? | |
998 | * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general | |
999 | * rule of thumb is that any SNR above 20 is good." This one | |
1000 | * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23 | |
1001 | * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a | |
1002 | * conservative value. | |
1003 | */ | |
1004 | #define GREAT_SNR 30 | |
1005 | ||
9ba9fa07 JM |
1006 | /* Compare function for sorting scan results. Return >0 if @b is considered |
1007 | * better. */ | |
1008 | static int wpa_scan_result_compar(const void *a, const void *b) | |
1009 | { | |
577db0ae GM |
1010 | #define IS_5GHZ(n) (n > 4000) |
1011 | #define MIN(a,b) a < b ? a : b | |
9ba9fa07 JM |
1012 | struct wpa_scan_res **_wa = (void *) a; |
1013 | struct wpa_scan_res **_wb = (void *) b; | |
1014 | struct wpa_scan_res *wa = *_wa; | |
1015 | struct wpa_scan_res *wb = *_wb; | |
1016 | int wpa_a, wpa_b, maxrate_a, maxrate_b; | |
577db0ae | 1017 | int snr_a, snr_b; |
9ba9fa07 JM |
1018 | |
1019 | /* WPA/WPA2 support preferred */ | |
1020 | wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL || | |
1021 | wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL; | |
1022 | wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL || | |
1023 | wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL; | |
1024 | ||
1025 | if (wpa_b && !wpa_a) | |
1026 | return 1; | |
1027 | if (!wpa_b && wpa_a) | |
1028 | return -1; | |
1029 | ||
1030 | /* privacy support preferred */ | |
1031 | if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 && | |
1032 | (wb->caps & IEEE80211_CAP_PRIVACY)) | |
1033 | return 1; | |
1034 | if ((wa->caps & IEEE80211_CAP_PRIVACY) && | |
1035 | (wb->caps & IEEE80211_CAP_PRIVACY) == 0) | |
1036 | return -1; | |
1037 | ||
577db0ae GM |
1038 | if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) && |
1039 | !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) { | |
1040 | snr_a = MIN(wa->level - wa->noise, GREAT_SNR); | |
1041 | snr_b = MIN(wb->level - wb->noise, GREAT_SNR); | |
1042 | } else { | |
1043 | /* Not suitable information to calculate SNR, so use level */ | |
1044 | snr_a = wa->level; | |
1045 | snr_b = wb->level; | |
1046 | } | |
1047 | ||
577db0ae GM |
1048 | /* best/max rate preferred if SNR close enough */ |
1049 | if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) || | |
9ba9fa07 JM |
1050 | (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) { |
1051 | maxrate_a = wpa_scan_get_max_rate(wa); | |
1052 | maxrate_b = wpa_scan_get_max_rate(wb); | |
1053 | if (maxrate_a != maxrate_b) | |
1054 | return maxrate_b - maxrate_a; | |
577db0ae GM |
1055 | if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq)) |
1056 | return IS_5GHZ(wa->freq) ? -1 : 1; | |
9ba9fa07 JM |
1057 | } |
1058 | ||
1059 | /* use freq for channel preference */ | |
1060 | ||
577db0ae | 1061 | /* all things being equal, use SNR; if SNRs are |
9ba9fa07 JM |
1062 | * identical, use quality values since some drivers may only report |
1063 | * that value and leave the signal level zero */ | |
577db0ae | 1064 | if (snr_b == snr_a) |
9ba9fa07 | 1065 | return wb->qual - wa->qual; |
577db0ae GM |
1066 | return snr_b - snr_a; |
1067 | #undef MIN | |
1068 | #undef IS_5GHZ | |
9ba9fa07 JM |
1069 | } |
1070 | ||
1071 | ||
41e650ae JM |
1072 | #ifdef CONFIG_WPS |
1073 | /* Compare function for sorting scan results when searching a WPS AP for | |
1074 | * provisioning. Return >0 if @b is considered better. */ | |
1075 | static int wpa_scan_result_wps_compar(const void *a, const void *b) | |
1076 | { | |
1077 | struct wpa_scan_res **_wa = (void *) a; | |
1078 | struct wpa_scan_res **_wb = (void *) b; | |
1079 | struct wpa_scan_res *wa = *_wa; | |
1080 | struct wpa_scan_res *wb = *_wb; | |
1081 | int uses_wps_a, uses_wps_b; | |
1082 | struct wpabuf *wps_a, *wps_b; | |
1083 | int res; | |
1084 | ||
1085 | /* Optimization - check WPS IE existence before allocated memory and | |
1086 | * doing full reassembly. */ | |
1087 | uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL; | |
1088 | uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL; | |
1089 | if (uses_wps_a && !uses_wps_b) | |
1090 | return -1; | |
1091 | if (!uses_wps_a && uses_wps_b) | |
1092 | return 1; | |
1093 | ||
1094 | if (uses_wps_a && uses_wps_b) { | |
1095 | wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE); | |
1096 | wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE); | |
1097 | res = wps_ap_priority_compar(wps_a, wps_b); | |
1098 | wpabuf_free(wps_a); | |
1099 | wpabuf_free(wps_b); | |
1100 | if (res) | |
1101 | return res; | |
1102 | } | |
1103 | ||
1104 | /* | |
1105 | * Do not use current AP security policy as a sorting criteria during | |
1106 | * WPS provisioning step since the AP may get reconfigured at the | |
1107 | * completion of provisioning. | |
1108 | */ | |
1109 | ||
1110 | /* all things being equal, use signal level; if signal levels are | |
1111 | * identical, use quality values since some drivers may only report | |
1112 | * that value and leave the signal level zero */ | |
1113 | if (wb->level == wa->level) | |
1114 | return wb->qual - wa->qual; | |
1115 | return wb->level - wa->level; | |
1116 | } | |
1117 | #endif /* CONFIG_WPS */ | |
1118 | ||
1119 | ||
aa820e02 JM |
1120 | static void dump_scan_res(struct wpa_scan_results *scan_res) |
1121 | { | |
1122 | size_t i; | |
1123 | ||
1124 | if (scan_res->res == NULL || scan_res->num == 0) | |
1125 | return; | |
1126 | ||
1127 | wpa_printf(MSG_EXCESSIVE, "Sorted scan results"); | |
1128 | ||
1129 | for (i = 0; i < scan_res->num; i++) { | |
1130 | struct wpa_scan_res *r = scan_res->res[i]; | |
1131 | if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID)) | |
1132 | == WPA_SCAN_LEVEL_DBM) { | |
1133 | int snr = r->level - r->noise; | |
1134 | wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d " | |
1135 | "noise=%d level=%d snr=%d%s flags=0x%x", | |
1136 | MAC2STR(r->bssid), r->freq, r->qual, | |
1137 | r->noise, r->level, snr, | |
1138 | snr >= GREAT_SNR ? "*" : "", r->flags); | |
1139 | } else { | |
1140 | wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d " | |
1141 | "noise=%d level=%d flags=0x%x", | |
1142 | MAC2STR(r->bssid), r->freq, r->qual, | |
1143 | r->noise, r->level, r->flags); | |
1144 | } | |
1145 | } | |
1146 | } | |
1147 | ||
1148 | ||
9ba9fa07 JM |
1149 | /** |
1150 | * wpa_supplicant_get_scan_results - Get scan results | |
1151 | * @wpa_s: Pointer to wpa_supplicant data | |
1152 | * @info: Information about what was scanned or %NULL if not available | |
1153 | * @new_scan: Whether a new scan was performed | |
1154 | * Returns: Scan results, %NULL on failure | |
1155 | * | |
1156 | * This function request the current scan results from the driver and updates | |
1157 | * the local BSS list wpa_s->bss. The caller is responsible for freeing the | |
1158 | * results with wpa_scan_results_free(). | |
1159 | */ | |
1160 | struct wpa_scan_results * | |
1161 | wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s, | |
1162 | struct scan_info *info, int new_scan) | |
1163 | { | |
1164 | struct wpa_scan_results *scan_res; | |
1165 | size_t i; | |
41e650ae | 1166 | int (*compar)(const void *, const void *) = wpa_scan_result_compar; |
9ba9fa07 | 1167 | |
17fbb751 | 1168 | scan_res = wpa_drv_get_scan_results2(wpa_s); |
9ba9fa07 | 1169 | if (scan_res == NULL) { |
f049052b | 1170 | wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results"); |
9ba9fa07 JM |
1171 | return NULL; |
1172 | } | |
1173 | ||
41e650ae JM |
1174 | #ifdef CONFIG_WPS |
1175 | if (wpas_wps_in_progress(wpa_s)) { | |
f049052b BG |
1176 | wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS " |
1177 | "provisioning rules"); | |
41e650ae JM |
1178 | compar = wpa_scan_result_wps_compar; |
1179 | } | |
1180 | #endif /* CONFIG_WPS */ | |
1181 | ||
9ba9fa07 | 1182 | qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *), |
41e650ae | 1183 | compar); |
aa820e02 | 1184 | dump_scan_res(scan_res); |
9ba9fa07 JM |
1185 | |
1186 | wpa_bss_update_start(wpa_s); | |
1187 | for (i = 0; i < scan_res->num; i++) | |
1188 | wpa_bss_update_scan_res(wpa_s, scan_res->res[i]); | |
1189 | wpa_bss_update_end(wpa_s, info, new_scan); | |
1190 | ||
1191 | return scan_res; | |
1192 | } | |
1193 | ||
1194 | ||
1195 | int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s) | |
1196 | { | |
1197 | struct wpa_scan_results *scan_res; | |
1198 | scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0); | |
1199 | if (scan_res == NULL) | |
1200 | return -1; | |
1201 | wpa_scan_results_free(scan_res); | |
1202 | ||
1203 | return 0; | |
1204 | } |