]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/scan.c
Use special scan result processing steps only on requesting interface
[thirdparty/hostap.git] / wpa_supplicant / scan.c
CommitLineData
6fc6879b
JM
1/*
2 * WPA Supplicant - Scanning
2c09af30 3 * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
6fc6879b 4 *
0f3d578e
JM
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
6fc6879b
JM
7 */
8
9ba9fa07 9#include "utils/includes.h"
6fc6879b 10
9ba9fa07
JM
11#include "utils/common.h"
12#include "utils/eloop.h"
13#include "common/ieee802_11_defs.h"
66fe0f70 14#include "common/wpa_ctrl.h"
6fc6879b
JM
15#include "config.h"
16#include "wpa_supplicant_i.h"
2d5b792d 17#include "driver_i.h"
b01c18a8 18#include "wps_supplicant.h"
0e65037c
JM
19#include "p2p_supplicant.h"
20#include "p2p/p2p.h"
c923b8a5 21#include "hs20_supplicant.h"
8bac466b 22#include "notify.h"
9ba9fa07
JM
23#include "bss.h"
24#include "scan.h"
6fc6879b
JM
25
26
27static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
28{
29 struct wpa_ssid *ssid;
30 union wpa_event_data data;
31
32 ssid = wpa_supplicant_get_ssid(wpa_s);
33 if (ssid == NULL)
34 return;
35
8bac466b 36 if (wpa_s->current_ssid == NULL) {
6fc6879b 37 wpa_s->current_ssid = ssid;
8bac466b
JM
38 if (wpa_s->current_ssid != NULL)
39 wpas_notify_network_changed(wpa_s);
40 }
6fc6879b 41 wpa_supplicant_initiate_eapol(wpa_s);
f049052b
BG
42 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with a configured "
43 "network - generating associated event");
6fc6879b
JM
44 os_memset(&data, 0, sizeof(data));
45 wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
46}
47
48
ad08c363 49#ifdef CONFIG_WPS
5f738a21 50static int wpas_wps_in_use(struct wpa_supplicant *wpa_s,
f90c86d4 51 enum wps_request_type *req_type)
ad08c363
JM
52{
53 struct wpa_ssid *ssid;
54 int wps = 0;
ad08c363 55
5f738a21 56 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
ad08c363
JM
57 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
58 continue;
59
60 wps = 1;
b01c18a8 61 *req_type = wpas_wps_get_req_type(ssid);
ad08c363
JM
62 if (!ssid->eap.phase1)
63 continue;
64
ad08c363
JM
65 if (os_strstr(ssid->eap.phase1, "pbc=1"))
66 return 2;
67 }
68
5f738a21 69#ifdef CONFIG_P2P
7a808c7e
JM
70 if (!wpa_s->global->p2p_disabled && wpa_s->global->p2p &&
71 !wpa_s->conf->p2p_disabled) {
b21ff9cb
JM
72 wpa_s->wps->dev.p2p = 1;
73 if (!wps) {
74 wps = 1;
75 *req_type = WPS_REQ_ENROLLEE_INFO;
76 }
5f738a21
LC
77 }
78#endif /* CONFIG_P2P */
79
ad08c363
JM
80 return wps;
81}
82#endif /* CONFIG_WPS */
83
e76baaac 84
2c09af30
JM
85/**
86 * wpa_supplicant_enabled_networks - Check whether there are enabled networks
87 * @wpa_s: Pointer to wpa_supplicant data
88 * Returns: 0 if no networks are enabled, >0 if networks are enabled
89 *
90 * This function is used to figure out whether any networks (or Interworking
91 * with enabled credentials and auto_interworking) are present in the current
92 * configuration.
93 */
349493bd 94int wpa_supplicant_enabled_networks(struct wpa_supplicant *wpa_s)
e76baaac 95{
349493bd 96 struct wpa_ssid *ssid = wpa_s->conf->ssid;
d4534bde 97 int count = 0, disabled = 0;
e76baaac 98 while (ssid) {
349493bd 99 if (!wpas_network_disabled(wpa_s, ssid))
5471c343 100 count++;
d4534bde
JM
101 else
102 disabled++;
e76baaac
JM
103 ssid = ssid->next;
104 }
4d5bda5f
JM
105 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
106 wpa_s->conf->auto_interworking)
107 count++;
d4534bde
JM
108 if (count == 0 && disabled > 0) {
109 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks (%d disabled "
110 "networks)", disabled);
111 }
5471c343 112 return count;
e76baaac
JM
113}
114
115
116static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
117 struct wpa_ssid *ssid)
118{
119 while (ssid) {
349493bd 120 if (!wpas_network_disabled(wpa_s, ssid))
e76baaac
JM
121 break;
122 ssid = ssid->next;
123 }
124
125 /* ap_scan=2 mode - try to associate with each SSID. */
126 if (ssid == NULL) {
f049052b
BG
127 wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
128 "end of scan list - go back to beginning");
ba2a573c 129 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
e76baaac
JM
130 wpa_supplicant_req_scan(wpa_s, 0, 0);
131 return;
132 }
133 if (ssid->next) {
134 /* Continue from the next SSID on the next attempt. */
135 wpa_s->prev_scan_ssid = ssid;
136 } else {
137 /* Start from the beginning of the SSID list. */
ba2a573c 138 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
e76baaac
JM
139 }
140 wpa_supplicant_associate(wpa_s, NULL, ssid);
141}
142
143
d3a98225
JM
144static int int_array_len(const int *a)
145{
146 int i;
147 for (i = 0; a && a[i]; i++)
148 ;
149 return i;
150}
151
152
153static void int_array_concat(int **res, const int *a)
154{
155 int reslen, alen, i;
156 int *n;
157
158 reslen = int_array_len(*res);
159 alen = int_array_len(a);
160
067ffa26 161 n = os_realloc_array(*res, reslen + alen + 1, sizeof(int));
d3a98225
JM
162 if (n == NULL) {
163 os_free(*res);
164 *res = NULL;
e6c0ebff 165 return;
d3a98225
JM
166 }
167 for (i = 0; i <= alen; i++)
168 n[reslen + i] = a[i];
169 *res = n;
170}
171
172
173static int freq_cmp(const void *a, const void *b)
174{
175 int _a = *(int *) a;
176 int _b = *(int *) b;
177
178 if (_a == 0)
179 return 1;
180 if (_b == 0)
181 return -1;
182 return _a - _b;
183}
184
185
186static void int_array_sort_unique(int *a)
187{
188 int alen;
189 int i, j;
190
191 if (a == NULL)
192 return;
193
194 alen = int_array_len(a);
195 qsort(a, alen, sizeof(int), freq_cmp);
196
197 i = 0;
198 j = 1;
199 while (a[i] && a[j]) {
200 if (a[i] == a[j]) {
201 j++;
202 continue;
203 }
204 a[++i] = a[j++];
205 }
206 if (a[i])
207 i++;
208 a[i] = 0;
209}
210
211
2c09af30
JM
212/**
213 * wpa_supplicant_trigger_scan - Request driver to start a scan
214 * @wpa_s: Pointer to wpa_supplicant data
215 * @params: Scan parameters
216 * Returns: 0 on success, -1 on failure
217 */
60b94c98
JM
218int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
219 struct wpa_driver_scan_params *params)
220{
221 int ret;
222
223 wpa_supplicant_notify_scanning(wpa_s, 1);
224
17fbb751 225 ret = wpa_drv_scan(wpa_s, params);
60b94c98
JM
226 if (ret) {
227 wpa_supplicant_notify_scanning(wpa_s, 0);
228 wpas_notify_scan_done(wpa_s, 0);
0b7a25c0 229 } else {
60b94c98 230 wpa_s->scan_runs++;
0b7a25c0
JM
231 wpa_s->normal_scans++;
232 }
60b94c98
JM
233
234 return ret;
235}
236
237
6a90053c
LC
238static void
239wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
240{
241 struct wpa_supplicant *wpa_s = eloop_ctx;
242
243 wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
244
245 if (wpa_supplicant_req_sched_scan(wpa_s))
246 wpa_supplicant_req_scan(wpa_s, 0, 0);
247}
248
249
cbdf3507
LC
250static void
251wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
252{
253 struct wpa_supplicant *wpa_s = eloop_ctx;
254
255 wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
256
257 wpa_s->sched_scan_timed_out = 1;
258 wpa_supplicant_cancel_sched_scan(wpa_s);
259}
260
261
262static int
263wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
264 struct wpa_driver_scan_params *params,
265 int interval)
266{
267 int ret;
268
cbdf3507
LC
269 wpa_supplicant_notify_scanning(wpa_s, 1);
270 ret = wpa_drv_sched_scan(wpa_s, params, interval * 1000);
271 if (ret)
272 wpa_supplicant_notify_scanning(wpa_s, 0);
273 else
274 wpa_s->sched_scanning = 1;
275
276 return ret;
277}
278
279
280static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
281{
282 int ret;
283
284 ret = wpa_drv_stop_sched_scan(wpa_s);
285 if (ret) {
286 wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
287 /* TODO: what to do if stopping fails? */
288 return -1;
289 }
290
291 return ret;
292}
293
294
3812464c
JM
295static struct wpa_driver_scan_filter *
296wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
297{
298 struct wpa_driver_scan_filter *ssids;
299 struct wpa_ssid *ssid;
300 size_t count;
301
302 *num_ssids = 0;
303 if (!conf->filter_ssids)
304 return NULL;
305
306 for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
307 if (ssid->ssid && ssid->ssid_len)
308 count++;
309 }
310 if (count == 0)
311 return NULL;
312 ssids = os_zalloc(count * sizeof(struct wpa_driver_scan_filter));
313 if (ssids == NULL)
314 return NULL;
315
316 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
317 if (!ssid->ssid || !ssid->ssid_len)
318 continue;
319 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
320 ssids[*num_ssids].ssid_len = ssid->ssid_len;
321 (*num_ssids)++;
322 }
323
324 return ssids;
325}
326
327
5f738a21
LC
328static void wpa_supplicant_optimize_freqs(
329 struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
330{
331#ifdef CONFIG_P2P
332 if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
333 wpa_s->go_params) {
334 /* Optimize provisioning state scan based on GO information */
335 if (wpa_s->p2p_in_provisioning < 5 &&
336 wpa_s->go_params->freq > 0) {
337 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
338 "preferred frequency %d MHz",
339 wpa_s->go_params->freq);
340 params->freqs = os_zalloc(2 * sizeof(int));
341 if (params->freqs)
342 params->freqs[0] = wpa_s->go_params->freq;
343 } else if (wpa_s->p2p_in_provisioning < 8 &&
344 wpa_s->go_params->freq_list[0]) {
345 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
346 "channels");
347 int_array_concat(&params->freqs,
348 wpa_s->go_params->freq_list);
349 if (params->freqs)
350 int_array_sort_unique(params->freqs);
351 }
352 wpa_s->p2p_in_provisioning++;
353 }
354#endif /* CONFIG_P2P */
355
356#ifdef CONFIG_WPS
357 if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
358 /*
359 * Optimize post-provisioning scan based on channel used
360 * during provisioning.
361 */
362 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
363 "that was used during provisioning", wpa_s->wps_freq);
364 params->freqs = os_zalloc(2 * sizeof(int));
365 if (params->freqs)
366 params->freqs[0] = wpa_s->wps_freq;
367 wpa_s->after_wps--;
368 }
369
1e7fb4f1
JM
370 if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
371 {
372 /* Optimize provisioning scan based on already known channel */
373 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
374 wpa_s->wps_freq);
375 params->freqs = os_zalloc(2 * sizeof(int));
376 if (params->freqs)
377 params->freqs[0] = wpa_s->wps_freq;
378 wpa_s->known_wps_freq = 0; /* only do this once */
379 }
5f738a21
LC
380#endif /* CONFIG_WPS */
381}
382
383
46ee0427
JM
384#ifdef CONFIG_INTERWORKING
385static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
386 struct wpabuf *buf)
387{
388 if (wpa_s->conf->interworking == 0)
389 return;
390
391 wpabuf_put_u8(buf, WLAN_EID_EXT_CAPAB);
392 wpabuf_put_u8(buf, 4);
393 wpabuf_put_u8(buf, 0x00);
394 wpabuf_put_u8(buf, 0x00);
395 wpabuf_put_u8(buf, 0x00);
396 wpabuf_put_u8(buf, 0x80); /* Bit 31 - Interworking */
397
398 wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
399 wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
400 1 + ETH_ALEN);
11540c0b 401 wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
46ee0427
JM
402 /* No Venue Info */
403 if (!is_zero_ether_addr(wpa_s->conf->hessid))
404 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
405}
406#endif /* CONFIG_INTERWORKING */
407
408
a13e07ec 409static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
6fc6879b 410{
46ee0427 411 struct wpabuf *extra_ie = NULL;
b01c18a8 412#ifdef CONFIG_WPS
509a3972 413 int wps = 0;
f90c86d4 414 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
46ee0427
JM
415#endif /* CONFIG_WPS */
416
417#ifdef CONFIG_INTERWORKING
418 if (wpa_s->conf->interworking &&
419 wpabuf_resize(&extra_ie, 100) == 0)
420 wpas_add_interworking_elements(wpa_s, extra_ie);
421#endif /* CONFIG_INTERWORKING */
5f738a21 422
46ee0427 423#ifdef CONFIG_WPS
5f738a21
LC
424 wps = wpas_wps_in_use(wpa_s, &req_type);
425
426 if (wps) {
46ee0427 427 struct wpabuf *wps_ie;
360182ed
JM
428 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
429 DEV_PW_DEFAULT,
430 &wpa_s->wps->dev,
5f738a21
LC
431 wpa_s->wps->uuid, req_type,
432 0, NULL);
433 if (wps_ie) {
46ee0427
JM
434 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
435 wpabuf_put_buf(extra_ie, wps_ie);
436 wpabuf_free(wps_ie);
5f738a21
LC
437 }
438 }
439
440#ifdef CONFIG_P2P
46ee0427 441 if (wps) {
5f738a21 442 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
46ee0427
JM
443 if (wpabuf_resize(&extra_ie, ielen) == 0)
444 wpas_p2p_scan_ie(wpa_s, extra_ie);
5f738a21
LC
445 }
446#endif /* CONFIG_P2P */
447
b01c18a8 448#endif /* CONFIG_WPS */
5f738a21 449
46ee0427 450 return extra_ie;
5f738a21
LC
451}
452
453
5cc70322
JM
454#ifdef CONFIG_P2P
455
456/*
457 * Check whether there are any enabled networks or credentials that could be
458 * used for a non-P2P connection.
459 */
460static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
461{
462 struct wpa_ssid *ssid;
463
464 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
465 if (wpas_network_disabled(wpa_s, ssid))
466 continue;
467 if (!ssid->p2p_group)
468 return 1;
469 }
470
471 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
472 wpa_s->conf->auto_interworking)
473 return 1;
474
475 return 0;
476}
477
478
479/*
480 * Find the operating frequency of any other virtual interface that is using
481 * the same radio concurrently.
482 */
483static int shared_vif_oper_freq(struct wpa_supplicant *wpa_s)
484{
485 const char *rn, *rn2;
486 struct wpa_supplicant *ifs;
487 u8 bssid[ETH_ALEN];
488
489 if (!wpa_s->driver->get_radio_name)
490 return -1;
491
492 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
493 if (rn == NULL || rn[0] == '\0')
494 return -1;
495
496 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
497 if (ifs == wpa_s || !ifs->driver->get_radio_name)
498 continue;
499
500 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
501 if (!rn2 || os_strcmp(rn, rn2) != 0)
502 continue;
503
504 if (ifs->current_ssid == NULL || ifs->assoc_freq == 0)
505 continue;
506
507 if (ifs->current_ssid->mode == WPAS_MODE_AP ||
508 ifs->current_ssid->mode == WPAS_MODE_P2P_GO)
509 return ifs->current_ssid->frequency;
510 if (wpa_drv_get_bssid(ifs, bssid) == 0)
511 return ifs->assoc_freq;
512 }
513
514 return 0;
515}
516
517#endif /* CONFIG_P2P */
518
519
5f738a21
LC
520static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
521{
522 struct wpa_supplicant *wpa_s = eloop_ctx;
523 struct wpa_ssid *ssid;
4115303b
JM
524 enum scan_req_type scan_req = NORMAL_SCAN_REQ;
525 int ret;
b9b12d02 526 struct wpabuf *extra_ie = NULL;
e76baaac 527 struct wpa_driver_scan_params params;
7c865c68 528 struct wpa_driver_scan_params *scan_params;
e76baaac 529 size_t max_ssids;
207ef3fb 530 enum wpa_states prev_state;
6fc6879b 531
8401a6b0 532 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
f049052b 533 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
b1cd021e 534 wpas_p2p_continue_after_scan(wpa_s);
8401a6b0
JM
535 return;
536 }
537
4115303b 538 if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
ac06fb12 539 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
3180d7a2 540 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
b1cd021e 541 wpas_p2p_continue_after_scan(wpa_s);
6fc6879b 542 return;
3180d7a2 543 }
6fc6879b 544
349493bd 545 if (!wpa_supplicant_enabled_networks(wpa_s) &&
4115303b 546 wpa_s->scan_req == NORMAL_SCAN_REQ) {
f049052b 547 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
6fc6879b 548 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
b1cd021e 549 wpas_p2p_continue_after_scan(wpa_s);
6fc6879b
JM
550 return;
551 }
6fc6879b 552
c2a04078
JM
553 if (wpa_s->conf->ap_scan != 0 &&
554 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
f049052b
BG
555 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
556 "overriding ap_scan configuration");
6fc6879b 557 wpa_s->conf->ap_scan = 0;
8bac466b 558 wpas_notify_ap_scan_changed(wpa_s);
6fc6879b
JM
559 }
560
561 if (wpa_s->conf->ap_scan == 0) {
562 wpa_supplicant_gen_assoc_event(wpa_s);
563 return;
564 }
565
303f60d3
JM
566#ifdef CONFIG_P2P
567 if (wpas_p2p_in_progress(wpa_s)) {
99fcd404
JM
568 if (wpa_s->sta_scan_pending &&
569 wpas_p2p_in_progress(wpa_s) == 2 &&
e665ca9a 570 wpa_s->global->p2p_cb_on_scan_complete) {
99fcd404
JM
571 wpa_dbg(wpa_s, MSG_DEBUG, "Process pending station "
572 "mode scan during P2P search");
573 } else {
303f60d3
JM
574 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan "
575 "while P2P operation is in progress");
99fcd404 576 wpa_s->sta_scan_pending = 1;
303f60d3 577 wpa_supplicant_req_scan(wpa_s, 5, 0);
99fcd404 578 return;
303f60d3 579 }
303f60d3
JM
580 }
581#endif /* CONFIG_P2P */
582
17fbb751 583 if (wpa_s->conf->ap_scan == 2)
e76baaac
JM
584 max_ssids = 1;
585 else {
586 max_ssids = wpa_s->max_scan_ssids;
587 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
588 max_ssids = WPAS_MAX_SCAN_SSIDS;
589 }
590
e76baaac 591 scan_req = wpa_s->scan_req;
4115303b 592 wpa_s->scan_req = NORMAL_SCAN_REQ;
e76baaac
JM
593
594 os_memset(&params, 0, sizeof(params));
595
207ef3fb 596 prev_state = wpa_s->wpa_state;
6fc6879b
JM
597 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
598 wpa_s->wpa_state == WPA_INACTIVE)
599 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
600
7c865c68
TB
601 /*
602 * If autoscan has set its own scanning parameters
603 */
604 if (wpa_s->autoscan_params != NULL) {
605 scan_params = wpa_s->autoscan_params;
606 goto scan;
607 }
608
4115303b 609 if (scan_req != MANUAL_SCAN_REQ && wpa_s->connect_without_scan) {
7dcdcfd6
JM
610 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
611 if (ssid == wpa_s->connect_without_scan)
612 break;
613 }
614 wpa_s->connect_without_scan = NULL;
615 if (ssid) {
616 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
617 "without scan step");
618 wpa_supplicant_associate(wpa_s, NULL, ssid);
619 return;
620 }
621 }
622
a21c05db 623#ifdef CONFIG_P2P
85ea132a
JM
624 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
625 wpa_s->go_params) {
a21c05db 626 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during "
85ea132a 627 "P2P group formation");
a21c05db
JM
628 params.ssids[0].ssid = wpa_s->go_params->ssid;
629 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
630 params.num_ssids = 1;
631 goto ssid_list_set;
632 }
633#endif /* CONFIG_P2P */
634
e76baaac 635 /* Find the starting point from which to continue scanning */
6fc6879b 636 ssid = wpa_s->conf->ssid;
ba2a573c 637 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
6fc6879b
JM
638 while (ssid) {
639 if (ssid == wpa_s->prev_scan_ssid) {
640 ssid = ssid->next;
641 break;
642 }
643 ssid = ssid->next;
644 }
645 }
6fc6879b 646
4115303b 647 if (scan_req != MANUAL_SCAN_REQ && wpa_s->conf->ap_scan == 2) {
7dcdcfd6 648 wpa_s->connect_without_scan = NULL;
b3aa456b 649 wpa_s->prev_scan_wildcard = 0;
e76baaac
JM
650 wpa_supplicant_assoc_try(wpa_s, ssid);
651 return;
652 } else if (wpa_s->conf->ap_scan == 2) {
6fc6879b 653 /*
ba2a573c
JM
654 * User-initiated scan request in ap_scan == 2; scan with
655 * wildcard SSID.
6fc6879b 656 */
e76baaac
JM
657 ssid = NULL;
658 } else {
5be45e2e 659 struct wpa_ssid *start = ssid, *tssid;
d3a98225 660 int freqs_set = 0;
e76baaac
JM
661 if (ssid == NULL && max_ssids > 1)
662 ssid = wpa_s->conf->ssid;
663 while (ssid) {
349493bd
JM
664 if (!wpas_network_disabled(wpa_s, ssid) &&
665 ssid->scan_ssid) {
e76baaac
JM
666 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
667 ssid->ssid, ssid->ssid_len);
668 params.ssids[params.num_ssids].ssid =
669 ssid->ssid;
670 params.ssids[params.num_ssids].ssid_len =
671 ssid->ssid_len;
672 params.num_ssids++;
673 if (params.num_ssids + 1 >= max_ssids)
674 break;
675 }
676 ssid = ssid->next;
677 if (ssid == start)
678 break;
679 if (ssid == NULL && max_ssids > 1 &&
680 start != wpa_s->conf->ssid)
681 ssid = wpa_s->conf->ssid;
6fc6879b 682 }
d3a98225 683
5be45e2e 684 for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
349493bd 685 if (wpas_network_disabled(wpa_s, tssid))
d3a98225 686 continue;
5be45e2e 687 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
d3a98225 688 int_array_concat(&params.freqs,
5be45e2e 689 tssid->scan_freq);
d3a98225
JM
690 } else {
691 os_free(params.freqs);
692 params.freqs = NULL;
693 }
694 freqs_set = 1;
695 }
696 int_array_sort_unique(params.freqs);
6fc6879b
JM
697 }
698
b3aa456b
ES
699 if (ssid && max_ssids == 1) {
700 /*
701 * If the driver is limited to 1 SSID at a time interleave
702 * wildcard SSID scans with specific SSID scans to avoid
703 * waiting a long time for a wildcard scan.
704 */
705 if (!wpa_s->prev_scan_wildcard) {
706 params.ssids[0].ssid = NULL;
707 params.ssids[0].ssid_len = 0;
708 wpa_s->prev_scan_wildcard = 1;
709 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
710 "wildcard SSID (Interleave with specific)");
711 } else {
712 wpa_s->prev_scan_ssid = ssid;
713 wpa_s->prev_scan_wildcard = 0;
714 wpa_dbg(wpa_s, MSG_DEBUG,
715 "Starting AP scan for specific SSID: %s",
716 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
e76baaac 717 }
b3aa456b
ES
718 } else if (ssid) {
719 /* max_ssids > 1 */
720
721 wpa_s->prev_scan_ssid = ssid;
722 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
723 "the scan request");
724 params.num_ssids++;
e76baaac 725 } else {
ba2a573c 726 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
e76baaac 727 params.num_ssids++;
f049052b
BG
728 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
729 "SSID");
6fc6879b 730 }
a21c05db
JM
731#ifdef CONFIG_P2P
732ssid_list_set:
733#endif /* CONFIG_P2P */
6fc6879b 734
5f738a21 735 wpa_supplicant_optimize_freqs(wpa_s, &params);
a13e07ec 736 extra_ie = wpa_supplicant_extra_ies(wpa_s);
0e65037c 737
c923b8a5 738#ifdef CONFIG_HS20
5ebe8c81 739 if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 7) == 0)
c923b8a5
JK
740 wpas_hs20_add_indication(extra_ie);
741#endif /* CONFIG_HS20 */
742
f47d639d 743 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
f049052b
BG
744 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
745 "generated frequency list");
f47d639d
JM
746 params.freqs = wpa_s->next_scan_freqs;
747 } else
748 os_free(wpa_s->next_scan_freqs);
749 wpa_s->next_scan_freqs = NULL;
750
3812464c
JM
751 params.filter_ssids = wpa_supplicant_build_filter_ssids(
752 wpa_s->conf, &params.num_filter_ssids);
46ee0427
JM
753 if (extra_ie) {
754 params.extra_ies = wpabuf_head(extra_ie);
755 params.extra_ies_len = wpabuf_len(extra_ie);
756 }
3812464c 757
d1dd48e3 758#ifdef CONFIG_P2P
85ea132a
JM
759 if (wpa_s->p2p_in_provisioning ||
760 (wpa_s->show_group_started && wpa_s->go_params)) {
d1dd48e3
JM
761 /*
762 * The interface may not yet be in P2P mode, so we have to
763 * explicitly request P2P probe to disable CCK rates.
764 */
765 params.p2p_probe = 1;
766 }
767#endif /* CONFIG_P2P */
768
7c865c68
TB
769 scan_params = &params;
770
771scan:
5cc70322
JM
772#ifdef CONFIG_P2P
773 /*
774 * If the driver does not support multi-channel concurrency and a
775 * virtual interface that shares the same radio with the wpa_s interface
776 * is operating there may not be need to scan other channels apart from
777 * the current operating channel on the other virtual interface. Filter
778 * out other channels in case we are trying to find a connection for a
779 * station interface when we are not configured to prefer station
780 * connection and a concurrent operation is already in process.
781 */
4115303b 782 if (wpa_s->scan_for_connection && scan_req == NORMAL_SCAN_REQ &&
5cc70322
JM
783 !scan_params->freqs && !params.freqs &&
784 wpas_is_p2p_prioritized(wpa_s) &&
785 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) &&
786 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
787 non_p2p_network_enabled(wpa_s)) {
788 int freq = shared_vif_oper_freq(wpa_s);
789 if (freq > 0) {
790 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only the current "
791 "operating channel (%d MHz) since driver does "
792 "not support multi-channel concurrency", freq);
793 params.freqs = os_zalloc(sizeof(int) * 2);
794 if (params.freqs)
795 params.freqs[0] = freq;
796 scan_params->freqs = params.freqs;
797 }
798 }
799#endif /* CONFIG_P2P */
800
7c865c68 801 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
6fc6879b 802
46ee0427 803 wpabuf_free(extra_ie);
d3a98225 804 os_free(params.freqs);
3812464c 805 os_free(params.filter_ssids);
ad08c363 806
6fc6879b 807 if (ret) {
f049052b 808 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
207ef3fb
JM
809 if (prev_state != wpa_s->wpa_state)
810 wpa_supplicant_set_state(wpa_s, prev_state);
23270cd8
JM
811 /* Restore scan_req since we will try to scan again */
812 wpa_s->scan_req = scan_req;
1c4c9c50 813 wpa_supplicant_req_scan(wpa_s, 1, 0);
5cc70322
JM
814 } else {
815 wpa_s->scan_for_connection = 0;
d902a9c1 816 }
6fc6879b
JM
817}
818
819
9e737f08
PF
820void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
821{
822 struct os_time remaining, new_int;
823 int cancelled;
824
825 cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
826 &remaining);
827
828 new_int.sec = sec;
829 new_int.usec = 0;
830 if (cancelled && os_time_before(&remaining, &new_int)) {
831 new_int.sec = remaining.sec;
832 new_int.usec = remaining.usec;
833 }
834
835 eloop_register_timeout(new_int.sec, new_int.usec, wpa_supplicant_scan,
836 wpa_s, NULL);
837 wpa_s->scan_interval = sec;
838}
839
840
6fc6879b
JM
841/**
842 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
843 * @wpa_s: Pointer to wpa_supplicant data
844 * @sec: Number of seconds after which to scan
845 * @usec: Number of microseconds after which to scan
846 *
847 * This function is used to schedule a scan for neighboring access points after
848 * the specified time.
849 */
850void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
851{
7e148849
DW
852 /* If there's at least one network that should be specifically scanned
853 * then don't cancel the scan and reschedule. Some drivers do
854 * background scanning which generates frequent scan results, and that
855 * causes the specific SSID scan to get continually pushed back and
856 * never happen, which causes hidden APs to never get probe-scanned.
857 */
858 if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
859 wpa_s->conf->ap_scan == 1) {
860 struct wpa_ssid *ssid = wpa_s->conf->ssid;
861
862 while (ssid) {
349493bd
JM
863 if (!wpas_network_disabled(wpa_s, ssid) &&
864 ssid->scan_ssid)
7e148849
DW
865 break;
866 ssid = ssid->next;
867 }
868 if (ssid) {
f049052b 869 wpa_dbg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
7e148849
DW
870 "ensure that specific SSID scans occur");
871 return;
872 }
873 }
874
f049052b 875 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
6fc6879b
JM
876 sec, usec);
877 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
878 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
879}
880
881
6a90053c
LC
882/**
883 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
884 * @wpa_s: Pointer to wpa_supplicant data
885 * @sec: Number of seconds after which to scan
886 * @usec: Number of microseconds after which to scan
2c09af30 887 * Returns: 0 on success or -1 otherwise
6a90053c
LC
888 *
889 * This function is used to schedule periodic scans for neighboring
890 * access points after the specified time.
891 */
892int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
893 int sec, int usec)
894{
895 if (!wpa_s->sched_scan_supported)
896 return -1;
897
898 eloop_register_timeout(sec, usec,
899 wpa_supplicant_delayed_sched_scan_timeout,
900 wpa_s, NULL);
901
902 return 0;
903}
904
905
cbdf3507
LC
906/**
907 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
908 * @wpa_s: Pointer to wpa_supplicant data
2c09af30 909 * Returns: 0 is sched_scan was started or -1 otherwise
cbdf3507
LC
910 *
911 * This function is used to schedule periodic scans for neighboring
912 * access points repeating the scan continuously.
913 */
914int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
915{
916 struct wpa_driver_scan_params params;
7c865c68 917 struct wpa_driver_scan_params *scan_params;
cbdf3507 918 enum wpa_states prev_state;
7c865c68 919 struct wpa_ssid *ssid = NULL;
a13e07ec 920 struct wpabuf *extra_ie = NULL;
cbdf3507 921 int ret;
cbdf3507 922 unsigned int max_sched_scan_ssids;
76a5249e 923 int wildcard = 0;
0b7a25c0 924 int need_ssids;
cbdf3507
LC
925
926 if (!wpa_s->sched_scan_supported)
927 return -1;
928
929 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
930 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
931 else
932 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
54ddd743 933 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
76a5249e 934 return -1;
cbdf3507 935
1966e3d1
ES
936 if (wpa_s->sched_scanning) {
937 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
cbdf3507 938 return 0;
1966e3d1 939 }
cbdf3507 940
0b7a25c0
JM
941 need_ssids = 0;
942 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
349493bd 943 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
0b7a25c0
JM
944 /* Use wildcard SSID to find this network */
945 wildcard = 1;
349493bd
JM
946 } else if (!wpas_network_disabled(wpa_s, ssid) &&
947 ssid->ssid_len)
0b7a25c0 948 need_ssids++;
aa283ddd
JM
949
950#ifdef CONFIG_WPS
349493bd 951 if (!wpas_network_disabled(wpa_s, ssid) &&
fea7c3a0 952 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
aa283ddd
JM
953 /*
954 * Normal scan is more reliable and faster for WPS
955 * operations and since these are for short periods of
956 * time, the benefit of trying to use sched_scan would
957 * be limited.
958 */
959 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
960 "sched_scan for WPS");
961 return -1;
962 }
963#endif /* CONFIG_WPS */
0b7a25c0
JM
964 }
965 if (wildcard)
966 need_ssids++;
967
968 if (wpa_s->normal_scans < 3 &&
969 (need_ssids <= wpa_s->max_scan_ssids ||
970 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
971 /*
972 * When normal scan can speed up operations, use that for the
973 * first operations before starting the sched_scan to allow
974 * user space sleep more. We do this only if the normal scan
975 * has functionality that is suitable for this or if the
976 * sched_scan does not have better support for multiple SSIDs.
977 */
978 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
979 "sched_scan for initial scans (normal_scans=%d)",
980 wpa_s->normal_scans);
981 return -1;
982 }
983
cbdf3507
LC
984 os_memset(&params, 0, sizeof(params));
985
b59e6f26
LC
986 /* If we can't allocate space for the filters, we just don't filter */
987 params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
988 sizeof(struct wpa_driver_scan_filter));
989
cbdf3507
LC
990 prev_state = wpa_s->wpa_state;
991 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
992 wpa_s->wpa_state == WPA_INACTIVE)
993 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
994
7c865c68
TB
995 if (wpa_s->autoscan_params != NULL) {
996 scan_params = wpa_s->autoscan_params;
997 goto scan;
998 }
999
cbdf3507
LC
1000 /* Find the starting point from which to continue scanning */
1001 ssid = wpa_s->conf->ssid;
1002 if (wpa_s->prev_sched_ssid) {
1003 while (ssid) {
1004 if (ssid == wpa_s->prev_sched_ssid) {
1005 ssid = ssid->next;
1006 break;
1007 }
1008 ssid = ssid->next;
1009 }
1010 }
1011
1012 if (!ssid || !wpa_s->prev_sched_ssid) {
1013 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
1014
7c865c68
TB
1015 if (wpa_s->sched_scan_interval == 0)
1016 wpa_s->sched_scan_interval = 10;
cbdf3507
LC
1017 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1018 wpa_s->first_sched_scan = 1;
1019 ssid = wpa_s->conf->ssid;
1020 wpa_s->prev_sched_ssid = ssid;
1021 }
1022
76a5249e
JM
1023 if (wildcard) {
1024 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1025 params.num_ssids++;
1026 }
1027
cbdf3507 1028 while (ssid) {
349493bd 1029 if (wpas_network_disabled(wpa_s, ssid))
5edddf41 1030 goto next;
cbdf3507 1031
fcd16847
JM
1032 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1033 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1966e3d1
ES
1034 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1035 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
b59e6f26
LC
1036 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1037 ssid->ssid, ssid->ssid_len);
1038 params.filter_ssids[params.num_filter_ssids].ssid_len =
1039 ssid->ssid_len;
1040 params.num_filter_ssids++;
86b47aaf
JM
1041 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1042 {
1043 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1044 "filter for sched_scan - drop filter");
1045 os_free(params.filter_ssids);
1046 params.filter_ssids = NULL;
1047 params.num_filter_ssids = 0;
b59e6f26
LC
1048 }
1049
3f56f3a4 1050 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
76a5249e
JM
1051 if (params.num_ssids == max_sched_scan_ssids)
1052 break; /* only room for broadcast SSID */
3f56f3a4
JM
1053 wpa_dbg(wpa_s, MSG_DEBUG,
1054 "add to active scan ssid: %s",
1966e3d1 1055 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
cbdf3507
LC
1056 params.ssids[params.num_ssids].ssid =
1057 ssid->ssid;
1058 params.ssids[params.num_ssids].ssid_len =
1059 ssid->ssid_len;
1060 params.num_ssids++;
b59e6f26 1061 if (params.num_ssids >= max_sched_scan_ssids) {
cbdf3507 1062 wpa_s->prev_sched_ssid = ssid;
b55aca46
JM
1063 do {
1064 ssid = ssid->next;
1065 } while (ssid &&
349493bd 1066 (wpas_network_disabled(wpa_s, ssid) ||
fea7c3a0 1067 !ssid->scan_ssid));
cbdf3507
LC
1068 break;
1069 }
1070 }
b59e6f26 1071
5edddf41 1072 next:
cbdf3507
LC
1073 wpa_s->prev_sched_ssid = ssid;
1074 ssid = ssid->next;
1075 }
1076
7c6a266c
JM
1077 if (params.num_filter_ssids == 0) {
1078 os_free(params.filter_ssids);
1079 params.filter_ssids = NULL;
1080 }
1081
a13e07ec
ES
1082 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1083 if (extra_ie) {
1084 params.extra_ies = wpabuf_head(extra_ie);
1085 params.extra_ies_len = wpabuf_len(extra_ie);
1086 }
cbdf3507 1087
7c865c68
TB
1088 scan_params = &params;
1089
1090scan:
a8cb5a88 1091 if (ssid || !wpa_s->first_sched_scan) {
a8cb5a88
JM
1092 wpa_dbg(wpa_s, MSG_DEBUG,
1093 "Starting sched scan: interval %d timeout %d",
1094 wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
7d21a223
JM
1095 } else {
1096 wpa_dbg(wpa_s, MSG_DEBUG,
1097 "Starting sched scan: interval %d (no timeout)",
1098 wpa_s->sched_scan_interval);
a8cb5a88 1099 }
cbdf3507 1100
7c865c68 1101 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params,
cbdf3507 1102 wpa_s->sched_scan_interval);
a13e07ec 1103 wpabuf_free(extra_ie);
b59e6f26 1104 os_free(params.filter_ssids);
cbdf3507
LC
1105 if (ret) {
1106 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1107 if (prev_state != wpa_s->wpa_state)
1108 wpa_supplicant_set_state(wpa_s, prev_state);
1109 return ret;
1110 }
1111
1112 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1113 if (ssid || !wpa_s->first_sched_scan) {
1114 wpa_s->sched_scan_timed_out = 0;
1115 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1116 wpa_supplicant_sched_scan_timeout,
1117 wpa_s, NULL);
1118 wpa_s->first_sched_scan = 0;
1119 wpa_s->sched_scan_timeout /= 2;
1120 wpa_s->sched_scan_interval *= 2;
a09fc1cc
DS
1121 if (wpa_s->sched_scan_timeout < wpa_s->sched_scan_interval) {
1122 wpa_s->sched_scan_interval = 10;
1123 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1124 }
cbdf3507
LC
1125 }
1126
7ed52f67
DS
1127 /* If there is no more ssids, start next time from the beginning */
1128 if (!ssid)
1129 wpa_s->prev_sched_ssid = NULL;
1130
cbdf3507
LC
1131 return 0;
1132}
1133
1134
6fc6879b
JM
1135/**
1136 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1137 * @wpa_s: Pointer to wpa_supplicant data
1138 *
1139 * This function is used to cancel a scan request scheduled with
1140 * wpa_supplicant_req_scan().
1141 */
1142void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1143{
f049052b 1144 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
6fc6879b 1145 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
b1cd021e 1146 wpas_p2p_continue_after_scan(wpa_s);
6fc6879b 1147}
cb8564b1
DW
1148
1149
cbdf3507
LC
1150/**
1151 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1152 * @wpa_s: Pointer to wpa_supplicant data
1153 *
1154 * This function is used to stop a periodic scheduled scan.
1155 */
1156void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1157{
1158 if (!wpa_s->sched_scanning)
1159 return;
1160
1161 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1162 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1163 wpa_supplicant_stop_sched_scan(wpa_s);
1164}
1165
1166
2c09af30
JM
1167/**
1168 * wpa_supplicant_notify_scanning - Indicate possible scan state change
1169 * @wpa_s: Pointer to wpa_supplicant data
1170 * @scanning: Whether scanning is currently in progress
1171 *
1172 * This function is to generate scanning notifycations. It is called whenever
1173 * there may have been a change in scanning (scan started, completed, stopped).
1174 * wpas_notify_scanning() is called whenever the scanning state changed from the
1175 * previously notified state.
1176 */
cb8564b1
DW
1177void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1178 int scanning)
1179{
1180 if (wpa_s->scanning != scanning) {
1181 wpa_s->scanning = scanning;
8bac466b 1182 wpas_notify_scanning(wpa_s);
cb8564b1
DW
1183 }
1184}
1185
9ba9fa07
JM
1186
1187static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1188{
1189 int rate = 0;
1190 const u8 *ie;
1191 int i;
1192
1193 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1194 for (i = 0; ie && i < ie[1]; i++) {
1195 if ((ie[i + 2] & 0x7f) > rate)
1196 rate = ie[i + 2] & 0x7f;
1197 }
1198
1199 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1200 for (i = 0; ie && i < ie[1]; i++) {
1201 if ((ie[i + 2] & 0x7f) > rate)
1202 rate = ie[i + 2] & 0x7f;
1203 }
1204
1205 return rate;
1206}
1207
1208
2c09af30
JM
1209/**
1210 * wpa_scan_get_ie - Fetch a specified information element from a scan result
1211 * @res: Scan result entry
1212 * @ie: Information element identitifier (WLAN_EID_*)
1213 * Returns: Pointer to the information element (id field) or %NULL if not found
1214 *
1215 * This function returns the first matching information element in the scan
1216 * result.
1217 */
d1f9c410
JM
1218const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1219{
1220 const u8 *end, *pos;
1221
1222 pos = (const u8 *) (res + 1);
1223 end = pos + res->ie_len;
1224
1225 while (pos + 1 < end) {
1226 if (pos + 2 + pos[1] > end)
1227 break;
1228 if (pos[0] == ie)
1229 return pos;
1230 pos += 2 + pos[1];
1231 }
1232
1233 return NULL;
1234}
1235
1236
2c09af30
JM
1237/**
1238 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1239 * @res: Scan result entry
1240 * @vendor_type: Vendor type (four octets starting the IE payload)
1241 * Returns: Pointer to the information element (id field) or %NULL if not found
1242 *
1243 * This function returns the first matching information element in the scan
1244 * result.
1245 */
9ba9fa07
JM
1246const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1247 u32 vendor_type)
1248{
1249 const u8 *end, *pos;
1250
1251 pos = (const u8 *) (res + 1);
1252 end = pos + res->ie_len;
1253
1254 while (pos + 1 < end) {
1255 if (pos + 2 + pos[1] > end)
1256 break;
1257 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1258 vendor_type == WPA_GET_BE32(&pos[2]))
1259 return pos;
1260 pos += 2 + pos[1];
1261 }
1262
1263 return NULL;
1264}
1265
1266
2c09af30
JM
1267/**
1268 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
1269 * @res: Scan result entry
1270 * @vendor_type: Vendor type (four octets starting the IE payload)
1271 * Returns: Pointer to the information element payload or %NULL if not found
1272 *
1273 * This function returns concatenated payload of possibly fragmented vendor
1274 * specific information elements in the scan result. The caller is responsible
1275 * for freeing the returned buffer.
1276 */
9ba9fa07
JM
1277struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1278 u32 vendor_type)
1279{
1280 struct wpabuf *buf;
1281 const u8 *end, *pos;
1282
1283 buf = wpabuf_alloc(res->ie_len);
1284 if (buf == NULL)
1285 return NULL;
1286
1287 pos = (const u8 *) (res + 1);
1288 end = pos + res->ie_len;
1289
54f489be
JM
1290 while (pos + 1 < end) {
1291 if (pos + 2 + pos[1] > end)
1292 break;
1293 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1294 vendor_type == WPA_GET_BE32(&pos[2]))
1295 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1296 pos += 2 + pos[1];
1297 }
1298
1299 if (wpabuf_len(buf) == 0) {
1300 wpabuf_free(buf);
1301 buf = NULL;
1302 }
1303
1304 return buf;
1305}
1306
1307
577db0ae
GM
1308/*
1309 * Channels with a great SNR can operate at full rate. What is a great SNR?
1310 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1311 * rule of thumb is that any SNR above 20 is good." This one
1312 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1313 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1314 * conservative value.
1315 */
1316#define GREAT_SNR 30
1317
9ba9fa07
JM
1318/* Compare function for sorting scan results. Return >0 if @b is considered
1319 * better. */
1320static int wpa_scan_result_compar(const void *a, const void *b)
1321{
577db0ae
GM
1322#define IS_5GHZ(n) (n > 4000)
1323#define MIN(a,b) a < b ? a : b
9ba9fa07
JM
1324 struct wpa_scan_res **_wa = (void *) a;
1325 struct wpa_scan_res **_wb = (void *) b;
1326 struct wpa_scan_res *wa = *_wa;
1327 struct wpa_scan_res *wb = *_wb;
1328 int wpa_a, wpa_b, maxrate_a, maxrate_b;
577db0ae 1329 int snr_a, snr_b;
9ba9fa07
JM
1330
1331 /* WPA/WPA2 support preferred */
1332 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1333 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1334 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1335 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1336
1337 if (wpa_b && !wpa_a)
1338 return 1;
1339 if (!wpa_b && wpa_a)
1340 return -1;
1341
1342 /* privacy support preferred */
1343 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1344 (wb->caps & IEEE80211_CAP_PRIVACY))
1345 return 1;
1346 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1347 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1348 return -1;
1349
577db0ae
GM
1350 if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
1351 !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
1352 snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
1353 snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
1354 } else {
1355 /* Not suitable information to calculate SNR, so use level */
1356 snr_a = wa->level;
1357 snr_b = wb->level;
1358 }
1359
577db0ae
GM
1360 /* best/max rate preferred if SNR close enough */
1361 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
9ba9fa07
JM
1362 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
1363 maxrate_a = wpa_scan_get_max_rate(wa);
1364 maxrate_b = wpa_scan_get_max_rate(wb);
1365 if (maxrate_a != maxrate_b)
1366 return maxrate_b - maxrate_a;
577db0ae
GM
1367 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1368 return IS_5GHZ(wa->freq) ? -1 : 1;
9ba9fa07
JM
1369 }
1370
1371 /* use freq for channel preference */
1372
577db0ae 1373 /* all things being equal, use SNR; if SNRs are
9ba9fa07
JM
1374 * identical, use quality values since some drivers may only report
1375 * that value and leave the signal level zero */
577db0ae 1376 if (snr_b == snr_a)
9ba9fa07 1377 return wb->qual - wa->qual;
577db0ae
GM
1378 return snr_b - snr_a;
1379#undef MIN
1380#undef IS_5GHZ
9ba9fa07
JM
1381}
1382
1383
41e650ae
JM
1384#ifdef CONFIG_WPS
1385/* Compare function for sorting scan results when searching a WPS AP for
1386 * provisioning. Return >0 if @b is considered better. */
1387static int wpa_scan_result_wps_compar(const void *a, const void *b)
1388{
1389 struct wpa_scan_res **_wa = (void *) a;
1390 struct wpa_scan_res **_wb = (void *) b;
1391 struct wpa_scan_res *wa = *_wa;
1392 struct wpa_scan_res *wb = *_wb;
1393 int uses_wps_a, uses_wps_b;
1394 struct wpabuf *wps_a, *wps_b;
1395 int res;
1396
1397 /* Optimization - check WPS IE existence before allocated memory and
1398 * doing full reassembly. */
1399 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1400 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1401 if (uses_wps_a && !uses_wps_b)
1402 return -1;
1403 if (!uses_wps_a && uses_wps_b)
1404 return 1;
1405
1406 if (uses_wps_a && uses_wps_b) {
1407 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1408 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1409 res = wps_ap_priority_compar(wps_a, wps_b);
1410 wpabuf_free(wps_a);
1411 wpabuf_free(wps_b);
1412 if (res)
1413 return res;
1414 }
1415
1416 /*
1417 * Do not use current AP security policy as a sorting criteria during
1418 * WPS provisioning step since the AP may get reconfigured at the
1419 * completion of provisioning.
1420 */
1421
1422 /* all things being equal, use signal level; if signal levels are
1423 * identical, use quality values since some drivers may only report
1424 * that value and leave the signal level zero */
1425 if (wb->level == wa->level)
1426 return wb->qual - wa->qual;
1427 return wb->level - wa->level;
1428}
1429#endif /* CONFIG_WPS */
1430
1431
aa820e02
JM
1432static void dump_scan_res(struct wpa_scan_results *scan_res)
1433{
76202aed 1434#ifndef CONFIG_NO_STDOUT_DEBUG
aa820e02
JM
1435 size_t i;
1436
1437 if (scan_res->res == NULL || scan_res->num == 0)
1438 return;
1439
1440 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1441
1442 for (i = 0; i < scan_res->num; i++) {
1443 struct wpa_scan_res *r = scan_res->res[i];
1cbe86e2 1444 u8 *pos;
aa820e02
JM
1445 if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
1446 == WPA_SCAN_LEVEL_DBM) {
1447 int snr = r->level - r->noise;
1448 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
c5f10e80
JM
1449 "noise=%d level=%d snr=%d%s flags=0x%x "
1450 "age=%u",
aa820e02
JM
1451 MAC2STR(r->bssid), r->freq, r->qual,
1452 r->noise, r->level, snr,
c5f10e80
JM
1453 snr >= GREAT_SNR ? "*" : "", r->flags,
1454 r->age);
aa820e02
JM
1455 } else {
1456 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
c5f10e80 1457 "noise=%d level=%d flags=0x%x age=%u",
aa820e02 1458 MAC2STR(r->bssid), r->freq, r->qual,
c5f10e80 1459 r->noise, r->level, r->flags, r->age);
aa820e02 1460 }
1cbe86e2
JM
1461 pos = (u8 *) (r + 1);
1462 if (r->ie_len)
1463 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
1464 pos += r->ie_len;
1465 if (r->beacon_ie_len)
1466 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
1467 pos, r->beacon_ie_len);
aa820e02 1468 }
76202aed 1469#endif /* CONFIG_NO_STDOUT_DEBUG */
aa820e02
JM
1470}
1471
1472
2c09af30
JM
1473/**
1474 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
1475 * @wpa_s: Pointer to wpa_supplicant data
1476 * @bssid: BSSID to check
1477 * Returns: 0 if the BSSID is filtered or 1 if not
1478 *
1479 * This function is used to filter out specific BSSIDs from scan reslts mainly
1480 * for testing purposes (SET bssid_filter ctrl_iface command).
1481 */
d445a5cd
JM
1482int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
1483 const u8 *bssid)
1484{
1485 size_t i;
1486
1487 if (wpa_s->bssid_filter == NULL)
1488 return 1;
1489
1490 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
1491 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
1492 ETH_ALEN) == 0)
1493 return 1;
1494 }
1495
1496 return 0;
1497}
1498
1499
1500static void filter_scan_res(struct wpa_supplicant *wpa_s,
1501 struct wpa_scan_results *res)
1502{
1503 size_t i, j;
1504
1505 if (wpa_s->bssid_filter == NULL)
1506 return;
1507
1508 for (i = 0, j = 0; i < res->num; i++) {
1509 if (wpa_supplicant_filter_bssid_match(wpa_s,
1510 res->res[i]->bssid)) {
1511 res->res[j++] = res->res[i];
1512 } else {
1513 os_free(res->res[i]);
1514 res->res[i] = NULL;
1515 }
1516 }
1517
1518 if (res->num != j) {
1519 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
1520 (int) (res->num - j));
1521 res->num = j;
1522 }
1523}
1524
1525
9ba9fa07
JM
1526/**
1527 * wpa_supplicant_get_scan_results - Get scan results
1528 * @wpa_s: Pointer to wpa_supplicant data
1529 * @info: Information about what was scanned or %NULL if not available
1530 * @new_scan: Whether a new scan was performed
1531 * Returns: Scan results, %NULL on failure
1532 *
1533 * This function request the current scan results from the driver and updates
1534 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
1535 * results with wpa_scan_results_free().
1536 */
1537struct wpa_scan_results *
1538wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
1539 struct scan_info *info, int new_scan)
1540{
1541 struct wpa_scan_results *scan_res;
1542 size_t i;
41e650ae 1543 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
9ba9fa07 1544
17fbb751 1545 scan_res = wpa_drv_get_scan_results2(wpa_s);
9ba9fa07 1546 if (scan_res == NULL) {
f049052b 1547 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
9ba9fa07
JM
1548 return NULL;
1549 }
c5f10e80
JM
1550 if (scan_res->fetch_time.sec == 0) {
1551 /*
1552 * Make sure we have a valid timestamp if the driver wrapper
1553 * does not set this.
1554 */
1555 os_get_time(&scan_res->fetch_time);
1556 }
d445a5cd 1557 filter_scan_res(wpa_s, scan_res);
9ba9fa07 1558
41e650ae
JM
1559#ifdef CONFIG_WPS
1560 if (wpas_wps_in_progress(wpa_s)) {
f049052b
BG
1561 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
1562 "provisioning rules");
41e650ae
JM
1563 compar = wpa_scan_result_wps_compar;
1564 }
1565#endif /* CONFIG_WPS */
1566
9ba9fa07 1567 qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
41e650ae 1568 compar);
aa820e02 1569 dump_scan_res(scan_res);
9ba9fa07
JM
1570
1571 wpa_bss_update_start(wpa_s);
1572 for (i = 0; i < scan_res->num; i++)
c5f10e80
JM
1573 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
1574 &scan_res->fetch_time);
9ba9fa07
JM
1575 wpa_bss_update_end(wpa_s, info, new_scan);
1576
1577 return scan_res;
1578}
1579
1580
2c09af30
JM
1581/**
1582 * wpa_supplicant_update_scan_results - Update scan results from the driver
1583 * @wpa_s: Pointer to wpa_supplicant data
1584 * Returns: 0 on success, -1 on failure
1585 *
1586 * This function updates the BSS table within wpa_supplicant based on the
1587 * currently available scan results from the driver without requesting a new
1588 * scan. This is used in cases where the driver indicates an association
1589 * (including roaming within ESS) and wpa_supplicant does not yet have the
1590 * needed information to complete the connection (e.g., to perform validation
1591 * steps in 4-way handshake).
1592 */
9ba9fa07
JM
1593int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
1594{
1595 struct wpa_scan_results *scan_res;
1596 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
1597 if (scan_res == NULL)
1598 return -1;
1599 wpa_scan_results_free(scan_res);
1600
1601 return 0;
1602}
66fe0f70
DS
1603
1604
1605/**
1606 * scan_only_handler - Reports scan results
1607 */
1608void scan_only_handler(struct wpa_supplicant *wpa_s,
1609 struct wpa_scan_results *scan_res)
1610{
1611 wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
1612 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1613 wpas_notify_scan_results(wpa_s);
1614 wpas_notify_scan_done(wpa_s, 1);
1615}