]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/scan.c
Clear wpa_psk memory when setting up wpa_supplicant AP mode
[thirdparty/hostap.git] / wpa_supplicant / scan.c
CommitLineData
6fc6879b
JM
1/*
2 * WPA Supplicant - Scanning
dd43aaa5 3 * Copyright (c) 2003-2014, 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"
fbca4c89 25#include "mesh.h"
6fc6879b
JM
26
27
28static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
29{
30 struct wpa_ssid *ssid;
31 union wpa_event_data data;
32
33 ssid = wpa_supplicant_get_ssid(wpa_s);
34 if (ssid == NULL)
35 return;
36
8bac466b 37 if (wpa_s->current_ssid == NULL) {
6fc6879b 38 wpa_s->current_ssid = ssid;
8bac466b
JM
39 if (wpa_s->current_ssid != NULL)
40 wpas_notify_network_changed(wpa_s);
41 }
6fc6879b 42 wpa_supplicant_initiate_eapol(wpa_s);
f049052b
BG
43 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with a configured "
44 "network - generating associated event");
6fc6879b
JM
45 os_memset(&data, 0, sizeof(data));
46 wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
47}
48
49
ad08c363 50#ifdef CONFIG_WPS
5f738a21 51static int wpas_wps_in_use(struct wpa_supplicant *wpa_s,
f90c86d4 52 enum wps_request_type *req_type)
ad08c363
JM
53{
54 struct wpa_ssid *ssid;
55 int wps = 0;
ad08c363 56
5f738a21 57 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
ad08c363
JM
58 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
59 continue;
60
61 wps = 1;
b01c18a8 62 *req_type = wpas_wps_get_req_type(ssid);
ad08c363
JM
63 if (!ssid->eap.phase1)
64 continue;
65
ad08c363
JM
66 if (os_strstr(ssid->eap.phase1, "pbc=1"))
67 return 2;
68 }
69
5f738a21 70#ifdef CONFIG_P2P
7a808c7e
JM
71 if (!wpa_s->global->p2p_disabled && wpa_s->global->p2p &&
72 !wpa_s->conf->p2p_disabled) {
b21ff9cb
JM
73 wpa_s->wps->dev.p2p = 1;
74 if (!wps) {
75 wps = 1;
76 *req_type = WPS_REQ_ENROLLEE_INFO;
77 }
5f738a21
LC
78 }
79#endif /* CONFIG_P2P */
80
ad08c363
JM
81 return wps;
82}
83#endif /* CONFIG_WPS */
84
e76baaac 85
2c09af30
JM
86/**
87 * wpa_supplicant_enabled_networks - Check whether there are enabled networks
88 * @wpa_s: Pointer to wpa_supplicant data
89 * Returns: 0 if no networks are enabled, >0 if networks are enabled
90 *
91 * This function is used to figure out whether any networks (or Interworking
92 * with enabled credentials and auto_interworking) are present in the current
93 * configuration.
94 */
349493bd 95int wpa_supplicant_enabled_networks(struct wpa_supplicant *wpa_s)
e76baaac 96{
349493bd 97 struct wpa_ssid *ssid = wpa_s->conf->ssid;
d4534bde 98 int count = 0, disabled = 0;
e76baaac 99 while (ssid) {
349493bd 100 if (!wpas_network_disabled(wpa_s, ssid))
5471c343 101 count++;
d4534bde
JM
102 else
103 disabled++;
e76baaac
JM
104 ssid = ssid->next;
105 }
4d5bda5f
JM
106 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
107 wpa_s->conf->auto_interworking)
108 count++;
d4534bde
JM
109 if (count == 0 && disabled > 0) {
110 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks (%d disabled "
111 "networks)", disabled);
112 }
5471c343 113 return count;
e76baaac
JM
114}
115
116
117static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
118 struct wpa_ssid *ssid)
119{
120 while (ssid) {
349493bd 121 if (!wpas_network_disabled(wpa_s, ssid))
e76baaac
JM
122 break;
123 ssid = ssid->next;
124 }
125
126 /* ap_scan=2 mode - try to associate with each SSID. */
127 if (ssid == NULL) {
f049052b
BG
128 wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
129 "end of scan list - go back to beginning");
ba2a573c 130 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
e76baaac
JM
131 wpa_supplicant_req_scan(wpa_s, 0, 0);
132 return;
133 }
134 if (ssid->next) {
135 /* Continue from the next SSID on the next attempt. */
136 wpa_s->prev_scan_ssid = ssid;
137 } else {
138 /* Start from the beginning of the SSID list. */
ba2a573c 139 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
e76baaac
JM
140 }
141 wpa_supplicant_associate(wpa_s, NULL, ssid);
142}
143
144
d12a51b5 145static void wpas_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
60b94c98 146{
d12a51b5
JM
147 struct wpa_supplicant *wpa_s = work->wpa_s;
148 struct wpa_driver_scan_params *params = work->ctx;
60b94c98
JM
149 int ret;
150
d12a51b5 151 if (deinit) {
b3253ebb
AO
152 if (!work->started) {
153 wpa_scan_free_params(params);
154 return;
155 }
156 wpa_supplicant_notify_scanning(wpa_s, 0);
157 wpas_notify_scan_done(wpa_s, 0);
158 wpa_s->scan_work = NULL;
d12a51b5
JM
159 return;
160 }
161
c267753b
JM
162 if (wpas_update_random_addr_disassoc(wpa_s) < 0) {
163 wpa_msg(wpa_s, MSG_INFO,
164 "Failed to assign random MAC address for a scan");
165 radio_work_done(work);
166 return;
167 }
168
60b94c98
JM
169 wpa_supplicant_notify_scanning(wpa_s, 1);
170
949938aa
JM
171 if (wpa_s->clear_driver_scan_cache)
172 params->only_new_results = 1;
17fbb751 173 ret = wpa_drv_scan(wpa_s, params);
d12a51b5
JM
174 wpa_scan_free_params(params);
175 work->ctx = NULL;
60b94c98
JM
176 if (ret) {
177 wpa_supplicant_notify_scanning(wpa_s, 0);
178 wpas_notify_scan_done(wpa_s, 0);
d1079219
DS
179 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=%d",
180 ret);
d12a51b5
JM
181 radio_work_done(work);
182 return;
0b7a25c0 183 }
60b94c98 184
d12a51b5
JM
185 os_get_reltime(&wpa_s->scan_trigger_time);
186 wpa_s->scan_runs++;
187 wpa_s->normal_scans++;
188 wpa_s->own_scan_requested = 1;
189 wpa_s->clear_driver_scan_cache = 0;
190 wpa_s->scan_work = work;
191}
192
193
194/**
195 * wpa_supplicant_trigger_scan - Request driver to start a scan
196 * @wpa_s: Pointer to wpa_supplicant data
197 * @params: Scan parameters
198 * Returns: 0 on success, -1 on failure
199 */
200int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
201 struct wpa_driver_scan_params *params)
202{
203 struct wpa_driver_scan_params *ctx;
204
205 if (wpa_s->scan_work) {
206 wpa_dbg(wpa_s, MSG_INFO, "Reject scan trigger since one is already pending");
207 return -1;
208 }
209
210 ctx = wpa_scan_clone_params(params);
211 if (ctx == NULL)
212 return -1;
213
214 if (radio_add_work(wpa_s, 0, "scan", 0, wpas_trigger_scan_cb, ctx) < 0)
215 {
216 wpa_scan_free_params(ctx);
217 return -1;
218 }
219
220 return 0;
60b94c98
JM
221}
222
223
6a90053c
LC
224static void
225wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
226{
227 struct wpa_supplicant *wpa_s = eloop_ctx;
228
229 wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
230
231 if (wpa_supplicant_req_sched_scan(wpa_s))
232 wpa_supplicant_req_scan(wpa_s, 0, 0);
233}
234
235
cbdf3507
LC
236static void
237wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
238{
239 struct wpa_supplicant *wpa_s = eloop_ctx;
240
241 wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
242
243 wpa_s->sched_scan_timed_out = 1;
244 wpa_supplicant_cancel_sched_scan(wpa_s);
245}
246
247
f86d282f
JJ
248int wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
249 struct wpa_driver_scan_params *params,
250 int interval)
cbdf3507
LC
251{
252 int ret;
253
cbdf3507
LC
254 wpa_supplicant_notify_scanning(wpa_s, 1);
255 ret = wpa_drv_sched_scan(wpa_s, params, interval * 1000);
256 if (ret)
257 wpa_supplicant_notify_scanning(wpa_s, 0);
258 else
259 wpa_s->sched_scanning = 1;
260
261 return ret;
262}
263
264
f86d282f 265int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
cbdf3507
LC
266{
267 int ret;
268
269 ret = wpa_drv_stop_sched_scan(wpa_s);
270 if (ret) {
271 wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
272 /* TODO: what to do if stopping fails? */
273 return -1;
274 }
275
276 return ret;
277}
278
279
3812464c
JM
280static struct wpa_driver_scan_filter *
281wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
282{
283 struct wpa_driver_scan_filter *ssids;
284 struct wpa_ssid *ssid;
285 size_t count;
286
287 *num_ssids = 0;
288 if (!conf->filter_ssids)
289 return NULL;
290
291 for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
292 if (ssid->ssid && ssid->ssid_len)
293 count++;
294 }
295 if (count == 0)
296 return NULL;
faebdeaa 297 ssids = os_calloc(count, sizeof(struct wpa_driver_scan_filter));
3812464c
JM
298 if (ssids == NULL)
299 return NULL;
300
301 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
302 if (!ssid->ssid || !ssid->ssid_len)
303 continue;
304 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
305 ssids[*num_ssids].ssid_len = ssid->ssid_len;
306 (*num_ssids)++;
307 }
308
309 return ssids;
310}
311
312
5f738a21
LC
313static void wpa_supplicant_optimize_freqs(
314 struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
315{
316#ifdef CONFIG_P2P
317 if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
318 wpa_s->go_params) {
319 /* Optimize provisioning state scan based on GO information */
320 if (wpa_s->p2p_in_provisioning < 5 &&
321 wpa_s->go_params->freq > 0) {
322 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
323 "preferred frequency %d MHz",
324 wpa_s->go_params->freq);
faebdeaa 325 params->freqs = os_calloc(2, sizeof(int));
5f738a21
LC
326 if (params->freqs)
327 params->freqs[0] = wpa_s->go_params->freq;
328 } else if (wpa_s->p2p_in_provisioning < 8 &&
329 wpa_s->go_params->freq_list[0]) {
330 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
331 "channels");
332 int_array_concat(&params->freqs,
333 wpa_s->go_params->freq_list);
334 if (params->freqs)
335 int_array_sort_unique(params->freqs);
336 }
337 wpa_s->p2p_in_provisioning++;
338 }
41d5ce9e
RR
339
340 if (params->freqs == NULL && wpa_s->p2p_in_invitation) {
341 /*
342 * Optimize scan based on GO information during persistent
343 * group reinvocation
344 */
28fa4eb2 345 if (wpa_s->p2p_in_invitation < 5 &&
41d5ce9e
RR
346 wpa_s->p2p_invite_go_freq > 0) {
347 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO preferred frequency %d MHz during invitation",
348 wpa_s->p2p_invite_go_freq);
faebdeaa 349 params->freqs = os_calloc(2, sizeof(int));
41d5ce9e
RR
350 if (params->freqs)
351 params->freqs[0] = wpa_s->p2p_invite_go_freq;
352 }
353 wpa_s->p2p_in_invitation++;
354 if (wpa_s->p2p_in_invitation > 20) {
355 /*
356 * This should not really happen since the variable is
357 * cleared on group removal, but if it does happen, make
358 * sure we do not get stuck in special invitation scan
359 * mode.
360 */
361 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Clear p2p_in_invitation");
362 wpa_s->p2p_in_invitation = 0;
363 }
364 }
5f738a21
LC
365#endif /* CONFIG_P2P */
366
367#ifdef CONFIG_WPS
368 if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
369 /*
370 * Optimize post-provisioning scan based on channel used
371 * during provisioning.
372 */
373 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
374 "that was used during provisioning", wpa_s->wps_freq);
faebdeaa 375 params->freqs = os_calloc(2, sizeof(int));
5f738a21
LC
376 if (params->freqs)
377 params->freqs[0] = wpa_s->wps_freq;
378 wpa_s->after_wps--;
662b40b1
JM
379 } else if (wpa_s->after_wps)
380 wpa_s->after_wps--;
5f738a21 381
1e7fb4f1
JM
382 if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
383 {
384 /* Optimize provisioning scan based on already known channel */
385 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
386 wpa_s->wps_freq);
faebdeaa 387 params->freqs = os_calloc(2, sizeof(int));
1e7fb4f1
JM
388 if (params->freqs)
389 params->freqs[0] = wpa_s->wps_freq;
390 wpa_s->known_wps_freq = 0; /* only do this once */
391 }
5f738a21
LC
392#endif /* CONFIG_WPS */
393}
394
395
46ee0427
JM
396#ifdef CONFIG_INTERWORKING
397static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
398 struct wpabuf *buf)
399{
400 if (wpa_s->conf->interworking == 0)
401 return;
402
403 wpabuf_put_u8(buf, WLAN_EID_EXT_CAPAB);
95a3ea94 404 wpabuf_put_u8(buf, 6);
46ee0427
JM
405 wpabuf_put_u8(buf, 0x00);
406 wpabuf_put_u8(buf, 0x00);
407 wpabuf_put_u8(buf, 0x00);
408 wpabuf_put_u8(buf, 0x80); /* Bit 31 - Interworking */
95a3ea94
JM
409 wpabuf_put_u8(buf, 0x00);
410#ifdef CONFIG_HS20
411 wpabuf_put_u8(buf, 0x40); /* Bit 46 - WNM-Notification */
412#else /* CONFIG_HS20 */
413 wpabuf_put_u8(buf, 0x00);
414#endif /* CONFIG_HS20 */
46ee0427
JM
415
416 wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
417 wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
418 1 + ETH_ALEN);
11540c0b 419 wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
46ee0427
JM
420 /* No Venue Info */
421 if (!is_zero_ether_addr(wpa_s->conf->hessid))
422 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
423}
424#endif /* CONFIG_INTERWORKING */
425
426
a13e07ec 427static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
6fc6879b 428{
46ee0427 429 struct wpabuf *extra_ie = NULL;
b01c18a8 430#ifdef CONFIG_WPS
509a3972 431 int wps = 0;
f90c86d4 432 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
46ee0427
JM
433#endif /* CONFIG_WPS */
434
435#ifdef CONFIG_INTERWORKING
436 if (wpa_s->conf->interworking &&
437 wpabuf_resize(&extra_ie, 100) == 0)
438 wpas_add_interworking_elements(wpa_s, extra_ie);
439#endif /* CONFIG_INTERWORKING */
5f738a21 440
46ee0427 441#ifdef CONFIG_WPS
5f738a21
LC
442 wps = wpas_wps_in_use(wpa_s, &req_type);
443
444 if (wps) {
46ee0427 445 struct wpabuf *wps_ie;
360182ed
JM
446 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
447 DEV_PW_DEFAULT,
448 &wpa_s->wps->dev,
5f738a21
LC
449 wpa_s->wps->uuid, req_type,
450 0, NULL);
451 if (wps_ie) {
46ee0427
JM
452 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
453 wpabuf_put_buf(extra_ie, wps_ie);
454 wpabuf_free(wps_ie);
5f738a21
LC
455 }
456 }
457
458#ifdef CONFIG_P2P
46ee0427 459 if (wps) {
5f738a21 460 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
46ee0427
JM
461 if (wpabuf_resize(&extra_ie, ielen) == 0)
462 wpas_p2p_scan_ie(wpa_s, extra_ie);
5f738a21
LC
463 }
464#endif /* CONFIG_P2P */
465
fbca4c89
JA
466 wpa_supplicant_mesh_add_scan_ie(wpa_s, &extra_ie);
467
b01c18a8 468#endif /* CONFIG_WPS */
5f738a21 469
0f105f9e
JM
470#ifdef CONFIG_HS20
471 if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 7) == 0)
f9cd147d 472 wpas_hs20_add_indication(extra_ie, -1);
0f105f9e
JM
473#endif /* CONFIG_HS20 */
474
46ee0427 475 return extra_ie;
5f738a21
LC
476}
477
478
5cc70322
JM
479#ifdef CONFIG_P2P
480
481/*
482 * Check whether there are any enabled networks or credentials that could be
483 * used for a non-P2P connection.
484 */
485static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
486{
487 struct wpa_ssid *ssid;
488
489 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
490 if (wpas_network_disabled(wpa_s, ssid))
491 continue;
492 if (!ssid->p2p_group)
493 return 1;
494 }
495
496 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
497 wpa_s->conf->auto_interworking)
498 return 1;
499
500 return 0;
501}
502
6124e858 503#endif /* CONFIG_P2P */
5cc70322 504
5cc70322 505
faf9a858
JM
506static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
507 u16 num_modes,
508 enum hostapd_hw_mode mode)
509{
510 u16 i;
511
512 for (i = 0; i < num_modes; i++) {
513 if (modes[i].mode == mode)
514 return &modes[i];
515 }
516
517 return NULL;
518}
519
520
521static void wpa_setband_scan_freqs_list(struct wpa_supplicant *wpa_s,
522 enum hostapd_hw_mode band,
523 struct wpa_driver_scan_params *params)
524{
525 /* Include only supported channels for the specified band */
526 struct hostapd_hw_modes *mode;
527 int count, i;
528
529 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band);
530 if (mode == NULL) {
531 /* No channels supported in this band - use empty list */
532 params->freqs = os_zalloc(sizeof(int));
533 return;
534 }
535
faebdeaa 536 params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
faf9a858
JM
537 if (params->freqs == NULL)
538 return;
539 for (count = 0, i = 0; i < mode->num_channels; i++) {
540 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
541 continue;
542 params->freqs[count++] = mode->channels[i].freq;
543 }
544}
545
546
547static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
548 struct wpa_driver_scan_params *params)
549{
550 if (wpa_s->hw.modes == NULL)
551 return; /* unknown what channels the driver supports */
552 if (params->freqs)
553 return; /* already using a limited channel set */
554 if (wpa_s->setband == WPA_SETBAND_5G)
555 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
556 params);
557 else if (wpa_s->setband == WPA_SETBAND_2G)
558 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G,
559 params);
560}
561
562
6891f0e6
LJ
563static void wpa_set_scan_ssids(struct wpa_supplicant *wpa_s,
564 struct wpa_driver_scan_params *params,
565 size_t max_ssids)
566{
567 unsigned int i;
568 struct wpa_ssid *ssid;
569
570 for (i = 0; i < wpa_s->scan_id_count; i++) {
571 unsigned int j;
572
573 ssid = wpa_config_get_network(wpa_s->conf, wpa_s->scan_id[i]);
574 if (!ssid || !ssid->scan_ssid)
575 continue;
576
577 for (j = 0; j < params->num_ssids; j++) {
578 if (params->ssids[j].ssid_len == ssid->ssid_len &&
579 params->ssids[j].ssid &&
580 os_memcmp(params->ssids[j].ssid, ssid->ssid,
581 ssid->ssid_len) == 0)
582 break;
583 }
584 if (j < params->num_ssids)
585 continue; /* already in the list */
586
587 if (params->num_ssids + 1 > max_ssids) {
588 wpa_printf(MSG_DEBUG,
589 "Over max scan SSIDs for manual request");
590 break;
591 }
592
593 wpa_printf(MSG_DEBUG, "Scan SSID (manual request): %s",
594 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
595 params->ssids[params->num_ssids].ssid = ssid->ssid;
596 params->ssids[params->num_ssids].ssid_len = ssid->ssid_len;
597 params->num_ssids++;
598 }
599
600 wpa_s->scan_id_count = 0;
601}
602
603
5f738a21
LC
604static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
605{
606 struct wpa_supplicant *wpa_s = eloop_ctx;
607 struct wpa_ssid *ssid;
49e3eea8 608 int ret, p2p_in_prog;
b9b12d02 609 struct wpabuf *extra_ie = NULL;
e76baaac 610 struct wpa_driver_scan_params params;
7c865c68 611 struct wpa_driver_scan_params *scan_params;
e76baaac 612 size_t max_ssids;
207ef3fb 613 enum wpa_states prev_state;
6fc6879b 614
cf70d298 615 if (wpa_s->pno || wpa_s->pno_sched_pending) {
dd271857
RM
616 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - PNO is in progress");
617 return;
618 }
619
8401a6b0 620 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
f049052b 621 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
8401a6b0
JM
622 return;
623 }
624
4115303b 625 if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
ac06fb12 626 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
3180d7a2 627 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
6fc6879b 628 return;
3180d7a2 629 }
6fc6879b 630
f86d282f
JJ
631 if (wpa_s->scanning) {
632 /*
633 * If we are already in scanning state, we shall reschedule the
634 * the incoming scan request.
635 */
636 wpa_dbg(wpa_s, MSG_DEBUG, "Already scanning - Reschedule the incoming scan req");
637 wpa_supplicant_req_scan(wpa_s, 1, 0);
638 return;
639 }
640
349493bd 641 if (!wpa_supplicant_enabled_networks(wpa_s) &&
4115303b 642 wpa_s->scan_req == NORMAL_SCAN_REQ) {
f049052b 643 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
6fc6879b
JM
644 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
645 return;
646 }
6fc6879b 647
c2a04078
JM
648 if (wpa_s->conf->ap_scan != 0 &&
649 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
f049052b
BG
650 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
651 "overriding ap_scan configuration");
6fc6879b 652 wpa_s->conf->ap_scan = 0;
8bac466b 653 wpas_notify_ap_scan_changed(wpa_s);
6fc6879b
JM
654 }
655
656 if (wpa_s->conf->ap_scan == 0) {
657 wpa_supplicant_gen_assoc_event(wpa_s);
658 return;
659 }
660
49e3eea8
JM
661 p2p_in_prog = wpas_p2p_in_progress(wpa_s);
662 if (p2p_in_prog && p2p_in_prog != 2) {
1b5d4714
JM
663 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan while P2P operation is in progress");
664 wpa_supplicant_req_scan(wpa_s, 5, 0);
665 return;
303f60d3 666 }
303f60d3 667
17fbb751 668 if (wpa_s->conf->ap_scan == 2)
e76baaac
JM
669 max_ssids = 1;
670 else {
671 max_ssids = wpa_s->max_scan_ssids;
672 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
673 max_ssids = WPAS_MAX_SCAN_SSIDS;
674 }
675
12455031 676 wpa_s->last_scan_req = wpa_s->scan_req;
4115303b 677 wpa_s->scan_req = NORMAL_SCAN_REQ;
e76baaac
JM
678
679 os_memset(&params, 0, sizeof(params));
680
207ef3fb 681 prev_state = wpa_s->wpa_state;
6fc6879b
JM
682 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
683 wpa_s->wpa_state == WPA_INACTIVE)
684 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
685
7c865c68
TB
686 /*
687 * If autoscan has set its own scanning parameters
688 */
689 if (wpa_s->autoscan_params != NULL) {
690 scan_params = wpa_s->autoscan_params;
691 goto scan;
692 }
693
12455031
LP
694 if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
695 wpa_s->connect_without_scan) {
7dcdcfd6
JM
696 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
697 if (ssid == wpa_s->connect_without_scan)
698 break;
699 }
700 wpa_s->connect_without_scan = NULL;
701 if (ssid) {
702 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
703 "without scan step");
704 wpa_supplicant_associate(wpa_s, NULL, ssid);
705 return;
706 }
707 }
708
a21c05db 709#ifdef CONFIG_P2P
85ea132a
JM
710 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
711 wpa_s->go_params) {
9100b660
JM
712 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
713 wpa_s->p2p_in_provisioning,
714 wpa_s->show_group_started);
a21c05db
JM
715 params.ssids[0].ssid = wpa_s->go_params->ssid;
716 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
717 params.num_ssids = 1;
718 goto ssid_list_set;
719 }
41d5ce9e
RR
720
721 if (wpa_s->p2p_in_invitation) {
722 if (wpa_s->current_ssid) {
723 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during invitation");
724 params.ssids[0].ssid = wpa_s->current_ssid->ssid;
725 params.ssids[0].ssid_len =
726 wpa_s->current_ssid->ssid_len;
727 params.num_ssids = 1;
728 } else {
729 wpa_printf(MSG_DEBUG, "P2P: No specific SSID known for scan during invitation");
730 }
731 goto ssid_list_set;
732 }
a21c05db
JM
733#endif /* CONFIG_P2P */
734
e76baaac 735 /* Find the starting point from which to continue scanning */
6fc6879b 736 ssid = wpa_s->conf->ssid;
ba2a573c 737 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
6fc6879b
JM
738 while (ssid) {
739 if (ssid == wpa_s->prev_scan_ssid) {
740 ssid = ssid->next;
741 break;
742 }
743 ssid = ssid->next;
744 }
745 }
6fc6879b 746
12455031
LP
747 if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
748 wpa_s->conf->ap_scan == 2) {
7dcdcfd6 749 wpa_s->connect_without_scan = NULL;
b3aa456b 750 wpa_s->prev_scan_wildcard = 0;
e76baaac
JM
751 wpa_supplicant_assoc_try(wpa_s, ssid);
752 return;
753 } else if (wpa_s->conf->ap_scan == 2) {
6fc6879b 754 /*
ba2a573c
JM
755 * User-initiated scan request in ap_scan == 2; scan with
756 * wildcard SSID.
6fc6879b 757 */
e76baaac 758 ssid = NULL;
0f44ec8e
PQ
759 } else if (wpa_s->reattach && wpa_s->current_ssid != NULL) {
760 /*
761 * Perform single-channel single-SSID scan for
762 * reassociate-to-same-BSS operation.
763 */
764 /* Setup SSID */
765 ssid = wpa_s->current_ssid;
766 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
767 ssid->ssid, ssid->ssid_len);
768 params.ssids[0].ssid = ssid->ssid;
769 params.ssids[0].ssid_len = ssid->ssid_len;
770 params.num_ssids = 1;
771
772 /*
773 * Allocate memory for frequency array, allocate one extra
774 * slot for the zero-terminator.
775 */
776 params.freqs = os_malloc(sizeof(int) * 2);
777 if (params.freqs == NULL) {
778 wpa_dbg(wpa_s, MSG_ERROR, "Memory allocation failed");
779 return;
780 }
781 params.freqs[0] = wpa_s->assoc_freq;
782 params.freqs[1] = 0;
783
784 /*
785 * Reset the reattach flag so that we fall back to full scan if
786 * this scan fails.
787 */
788 wpa_s->reattach = 0;
e76baaac 789 } else {
5be45e2e 790 struct wpa_ssid *start = ssid, *tssid;
d3a98225 791 int freqs_set = 0;
e76baaac
JM
792 if (ssid == NULL && max_ssids > 1)
793 ssid = wpa_s->conf->ssid;
794 while (ssid) {
349493bd
JM
795 if (!wpas_network_disabled(wpa_s, ssid) &&
796 ssid->scan_ssid) {
e76baaac
JM
797 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
798 ssid->ssid, ssid->ssid_len);
799 params.ssids[params.num_ssids].ssid =
800 ssid->ssid;
801 params.ssids[params.num_ssids].ssid_len =
802 ssid->ssid_len;
803 params.num_ssids++;
804 if (params.num_ssids + 1 >= max_ssids)
805 break;
806 }
807 ssid = ssid->next;
808 if (ssid == start)
809 break;
810 if (ssid == NULL && max_ssids > 1 &&
811 start != wpa_s->conf->ssid)
812 ssid = wpa_s->conf->ssid;
6fc6879b 813 }
d3a98225 814
6891f0e6
LJ
815 if (wpa_s->scan_id_count &&
816 wpa_s->last_scan_req == MANUAL_SCAN_REQ)
817 wpa_set_scan_ssids(wpa_s, &params, max_ssids);
818
0855e2e1
JM
819 for (tssid = wpa_s->conf->ssid;
820 wpa_s->last_scan_req != MANUAL_SCAN_REQ && tssid;
821 tssid = tssid->next) {
349493bd 822 if (wpas_network_disabled(wpa_s, tssid))
d3a98225 823 continue;
5be45e2e 824 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
d3a98225 825 int_array_concat(&params.freqs,
5be45e2e 826 tssid->scan_freq);
d3a98225
JM
827 } else {
828 os_free(params.freqs);
829 params.freqs = NULL;
830 }
831 freqs_set = 1;
832 }
833 int_array_sort_unique(params.freqs);
6fc6879b
JM
834 }
835
b3aa456b
ES
836 if (ssid && max_ssids == 1) {
837 /*
838 * If the driver is limited to 1 SSID at a time interleave
839 * wildcard SSID scans with specific SSID scans to avoid
840 * waiting a long time for a wildcard scan.
841 */
842 if (!wpa_s->prev_scan_wildcard) {
843 params.ssids[0].ssid = NULL;
844 params.ssids[0].ssid_len = 0;
845 wpa_s->prev_scan_wildcard = 1;
846 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
847 "wildcard SSID (Interleave with specific)");
848 } else {
849 wpa_s->prev_scan_ssid = ssid;
850 wpa_s->prev_scan_wildcard = 0;
851 wpa_dbg(wpa_s, MSG_DEBUG,
852 "Starting AP scan for specific SSID: %s",
853 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
e76baaac 854 }
b3aa456b
ES
855 } else if (ssid) {
856 /* max_ssids > 1 */
857
858 wpa_s->prev_scan_ssid = ssid;
859 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
860 "the scan request");
861 params.num_ssids++;
88c2d488
JM
862 } else if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
863 wpa_s->manual_scan_passive && params.num_ssids == 0) {
864 wpa_dbg(wpa_s, MSG_DEBUG, "Use passive scan based on manual request");
e76baaac 865 } else {
ba2a573c 866 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
e76baaac 867 params.num_ssids++;
f049052b
BG
868 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
869 "SSID");
6fc6879b 870 }
a21c05db
JM
871#ifdef CONFIG_P2P
872ssid_list_set:
873#endif /* CONFIG_P2P */
6fc6879b 874
5f738a21 875 wpa_supplicant_optimize_freqs(wpa_s, &params);
a13e07ec 876 extra_ie = wpa_supplicant_extra_ies(wpa_s);
0e65037c 877
949938aa
JM
878 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
879 wpa_s->manual_scan_only_new)
880 params.only_new_results = 1;
881
fee52342
JM
882 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs == NULL &&
883 wpa_s->manual_scan_freqs) {
884 wpa_dbg(wpa_s, MSG_DEBUG, "Limit manual scan to specified channels");
885 params.freqs = wpa_s->manual_scan_freqs;
886 wpa_s->manual_scan_freqs = NULL;
887 }
888
f47d639d 889 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
f049052b
BG
890 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
891 "generated frequency list");
f47d639d
JM
892 params.freqs = wpa_s->next_scan_freqs;
893 } else
894 os_free(wpa_s->next_scan_freqs);
895 wpa_s->next_scan_freqs = NULL;
faf9a858 896 wpa_setband_scan_freqs(wpa_s, &params);
f47d639d 897
f5ffc348
BG
898 /* See if user specified frequencies. If so, scan only those. */
899 if (wpa_s->conf->freq_list && !params.freqs) {
900 wpa_dbg(wpa_s, MSG_DEBUG,
901 "Optimize scan based on conf->freq_list");
902 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
903 }
904
6124e858
BG
905 /* Use current associated channel? */
906 if (wpa_s->conf->scan_cur_freq && !params.freqs) {
53c5dfc2
IP
907 unsigned int num = wpa_s->num_multichan_concurrent;
908
909 params.freqs = os_calloc(num + 1, sizeof(int));
910 if (params.freqs) {
911 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
912 if (num > 0) {
913 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
914 "current operating channels since "
915 "scan_cur_freq is enabled");
916 } else {
917 os_free(params.freqs);
918 params.freqs = NULL;
919 }
6124e858
BG
920 }
921 }
922
3812464c
JM
923 params.filter_ssids = wpa_supplicant_build_filter_ssids(
924 wpa_s->conf, &params.num_filter_ssids);
46ee0427
JM
925 if (extra_ie) {
926 params.extra_ies = wpabuf_head(extra_ie);
927 params.extra_ies_len = wpabuf_len(extra_ie);
928 }
3812464c 929
d1dd48e3 930#ifdef CONFIG_P2P
8235f89f 931 if (wpa_s->p2p_in_provisioning || wpa_s->p2p_in_invitation ||
85ea132a 932 (wpa_s->show_group_started && wpa_s->go_params)) {
d1dd48e3
JM
933 /*
934 * The interface may not yet be in P2P mode, so we have to
935 * explicitly request P2P probe to disable CCK rates.
936 */
937 params.p2p_probe = 1;
938 }
939#endif /* CONFIG_P2P */
940
7c865c68
TB
941 scan_params = &params;
942
943scan:
5cc70322
JM
944#ifdef CONFIG_P2P
945 /*
946 * If the driver does not support multi-channel concurrency and a
947 * virtual interface that shares the same radio with the wpa_s interface
948 * is operating there may not be need to scan other channels apart from
949 * the current operating channel on the other virtual interface. Filter
950 * out other channels in case we are trying to find a connection for a
951 * station interface when we are not configured to prefer station
952 * connection and a concurrent operation is already in process.
953 */
12455031
LP
954 if (wpa_s->scan_for_connection &&
955 wpa_s->last_scan_req == NORMAL_SCAN_REQ &&
5cc70322
JM
956 !scan_params->freqs && !params.freqs &&
957 wpas_is_p2p_prioritized(wpa_s) &&
5cc70322
JM
958 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
959 non_p2p_network_enabled(wpa_s)) {
53c5dfc2
IP
960 unsigned int num = wpa_s->num_multichan_concurrent;
961
962 params.freqs = os_calloc(num + 1, sizeof(int));
963 if (params.freqs) {
964 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
965 if (num > 0 && num == wpa_s->num_multichan_concurrent) {
966 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
967 } else {
968 os_free(params.freqs);
969 params.freqs = NULL;
970 }
5cc70322
JM
971 }
972 }
973#endif /* CONFIG_P2P */
974
7c865c68 975 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
6fc6879b 976
fee52342
JM
977 if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs &&
978 !wpa_s->manual_scan_freqs) {
979 /* Restore manual_scan_freqs for the next attempt */
980 wpa_s->manual_scan_freqs = params.freqs;
981 params.freqs = NULL;
982 }
983
46ee0427 984 wpabuf_free(extra_ie);
d3a98225 985 os_free(params.freqs);
3812464c 986 os_free(params.filter_ssids);
ad08c363 987
6fc6879b 988 if (ret) {
f049052b 989 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
207ef3fb
JM
990 if (prev_state != wpa_s->wpa_state)
991 wpa_supplicant_set_state(wpa_s, prev_state);
23270cd8 992 /* Restore scan_req since we will try to scan again */
12455031 993 wpa_s->scan_req = wpa_s->last_scan_req;
1c4c9c50 994 wpa_supplicant_req_scan(wpa_s, 1, 0);
5cc70322
JM
995 } else {
996 wpa_s->scan_for_connection = 0;
d902a9c1 997 }
6fc6879b
JM
998}
999
1000
9e737f08
PF
1001void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
1002{
594516b4 1003 struct os_reltime remaining, new_int;
9e737f08
PF
1004 int cancelled;
1005
1006 cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
1007 &remaining);
1008
1009 new_int.sec = sec;
1010 new_int.usec = 0;
594516b4 1011 if (cancelled && os_reltime_before(&remaining, &new_int)) {
9e737f08
PF
1012 new_int.sec = remaining.sec;
1013 new_int.usec = remaining.usec;
1014 }
1015
c6f5dec9
PF
1016 if (cancelled) {
1017 eloop_register_timeout(new_int.sec, new_int.usec,
1018 wpa_supplicant_scan, wpa_s, NULL);
1019 }
9e737f08
PF
1020 wpa_s->scan_interval = sec;
1021}
1022
1023
6fc6879b
JM
1024/**
1025 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
1026 * @wpa_s: Pointer to wpa_supplicant data
1027 * @sec: Number of seconds after which to scan
1028 * @usec: Number of microseconds after which to scan
1029 *
1030 * This function is used to schedule a scan for neighboring access points after
1031 * the specified time.
1032 */
1033void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
1034{
a09ffd5f
JM
1035 int res = eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s,
1036 NULL);
1037 if (res == 1) {
1038 wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d.%06d sec",
e2f5a988 1039 sec, usec);
a09ffd5f
JM
1040 } else if (res == 0) {
1041 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore new scan request for %d.%06d sec since an earlier request is scheduled to trigger sooner",
1042 sec, usec);
1043 } else {
1044 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d.%06d sec",
1045 sec, usec);
1046 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
e2f5a988 1047 }
6fc6879b
JM
1048}
1049
1050
6a90053c
LC
1051/**
1052 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
1053 * @wpa_s: Pointer to wpa_supplicant data
1054 * @sec: Number of seconds after which to scan
1055 * @usec: Number of microseconds after which to scan
2c09af30 1056 * Returns: 0 on success or -1 otherwise
6a90053c
LC
1057 *
1058 * This function is used to schedule periodic scans for neighboring
1059 * access points after the specified time.
1060 */
1061int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
1062 int sec, int usec)
1063{
1064 if (!wpa_s->sched_scan_supported)
1065 return -1;
1066
1067 eloop_register_timeout(sec, usec,
1068 wpa_supplicant_delayed_sched_scan_timeout,
1069 wpa_s, NULL);
1070
1071 return 0;
1072}
1073
1074
cbdf3507
LC
1075/**
1076 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
1077 * @wpa_s: Pointer to wpa_supplicant data
2c09af30 1078 * Returns: 0 is sched_scan was started or -1 otherwise
cbdf3507
LC
1079 *
1080 * This function is used to schedule periodic scans for neighboring
1081 * access points repeating the scan continuously.
1082 */
1083int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
1084{
1085 struct wpa_driver_scan_params params;
7c865c68 1086 struct wpa_driver_scan_params *scan_params;
cbdf3507 1087 enum wpa_states prev_state;
7c865c68 1088 struct wpa_ssid *ssid = NULL;
a13e07ec 1089 struct wpabuf *extra_ie = NULL;
cbdf3507 1090 int ret;
cbdf3507 1091 unsigned int max_sched_scan_ssids;
76a5249e 1092 int wildcard = 0;
0b7a25c0 1093 int need_ssids;
cbdf3507
LC
1094
1095 if (!wpa_s->sched_scan_supported)
1096 return -1;
1097
1098 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
1099 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
1100 else
1101 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
54ddd743 1102 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
76a5249e 1103 return -1;
cbdf3507 1104
1966e3d1
ES
1105 if (wpa_s->sched_scanning) {
1106 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
cbdf3507 1107 return 0;
1966e3d1 1108 }
cbdf3507 1109
0b7a25c0
JM
1110 need_ssids = 0;
1111 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
349493bd 1112 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
0b7a25c0
JM
1113 /* Use wildcard SSID to find this network */
1114 wildcard = 1;
349493bd
JM
1115 } else if (!wpas_network_disabled(wpa_s, ssid) &&
1116 ssid->ssid_len)
0b7a25c0 1117 need_ssids++;
aa283ddd
JM
1118
1119#ifdef CONFIG_WPS
349493bd 1120 if (!wpas_network_disabled(wpa_s, ssid) &&
fea7c3a0 1121 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
aa283ddd
JM
1122 /*
1123 * Normal scan is more reliable and faster for WPS
1124 * operations and since these are for short periods of
1125 * time, the benefit of trying to use sched_scan would
1126 * be limited.
1127 */
1128 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1129 "sched_scan for WPS");
1130 return -1;
1131 }
1132#endif /* CONFIG_WPS */
0b7a25c0
JM
1133 }
1134 if (wildcard)
1135 need_ssids++;
1136
1137 if (wpa_s->normal_scans < 3 &&
1138 (need_ssids <= wpa_s->max_scan_ssids ||
1139 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
1140 /*
1141 * When normal scan can speed up operations, use that for the
1142 * first operations before starting the sched_scan to allow
1143 * user space sleep more. We do this only if the normal scan
1144 * has functionality that is suitable for this or if the
1145 * sched_scan does not have better support for multiple SSIDs.
1146 */
1147 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1148 "sched_scan for initial scans (normal_scans=%d)",
1149 wpa_s->normal_scans);
1150 return -1;
1151 }
1152
cbdf3507
LC
1153 os_memset(&params, 0, sizeof(params));
1154
b59e6f26 1155 /* If we can't allocate space for the filters, we just don't filter */
faebdeaa 1156 params.filter_ssids = os_calloc(wpa_s->max_match_sets,
b59e6f26
LC
1157 sizeof(struct wpa_driver_scan_filter));
1158
cbdf3507
LC
1159 prev_state = wpa_s->wpa_state;
1160 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1161 wpa_s->wpa_state == WPA_INACTIVE)
1162 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1163
7c865c68
TB
1164 if (wpa_s->autoscan_params != NULL) {
1165 scan_params = wpa_s->autoscan_params;
1166 goto scan;
1167 }
1168
cbdf3507
LC
1169 /* Find the starting point from which to continue scanning */
1170 ssid = wpa_s->conf->ssid;
1171 if (wpa_s->prev_sched_ssid) {
1172 while (ssid) {
1173 if (ssid == wpa_s->prev_sched_ssid) {
1174 ssid = ssid->next;
1175 break;
1176 }
1177 ssid = ssid->next;
1178 }
1179 }
1180
1181 if (!ssid || !wpa_s->prev_sched_ssid) {
1182 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
4aa81868
SF
1183 if (wpa_s->conf->sched_scan_interval)
1184 wpa_s->sched_scan_interval =
1185 wpa_s->conf->sched_scan_interval;
7c865c68
TB
1186 if (wpa_s->sched_scan_interval == 0)
1187 wpa_s->sched_scan_interval = 10;
cbdf3507
LC
1188 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1189 wpa_s->first_sched_scan = 1;
1190 ssid = wpa_s->conf->ssid;
1191 wpa_s->prev_sched_ssid = ssid;
1192 }
1193
76a5249e
JM
1194 if (wildcard) {
1195 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1196 params.num_ssids++;
1197 }
1198
cbdf3507 1199 while (ssid) {
349493bd 1200 if (wpas_network_disabled(wpa_s, ssid))
5edddf41 1201 goto next;
cbdf3507 1202
fcd16847
JM
1203 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1204 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1966e3d1
ES
1205 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1206 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
b59e6f26
LC
1207 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1208 ssid->ssid, ssid->ssid_len);
1209 params.filter_ssids[params.num_filter_ssids].ssid_len =
1210 ssid->ssid_len;
1211 params.num_filter_ssids++;
86b47aaf
JM
1212 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1213 {
1214 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1215 "filter for sched_scan - drop filter");
1216 os_free(params.filter_ssids);
1217 params.filter_ssids = NULL;
1218 params.num_filter_ssids = 0;
b59e6f26
LC
1219 }
1220
3f56f3a4 1221 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
76a5249e
JM
1222 if (params.num_ssids == max_sched_scan_ssids)
1223 break; /* only room for broadcast SSID */
3f56f3a4
JM
1224 wpa_dbg(wpa_s, MSG_DEBUG,
1225 "add to active scan ssid: %s",
1966e3d1 1226 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
cbdf3507
LC
1227 params.ssids[params.num_ssids].ssid =
1228 ssid->ssid;
1229 params.ssids[params.num_ssids].ssid_len =
1230 ssid->ssid_len;
1231 params.num_ssids++;
b59e6f26 1232 if (params.num_ssids >= max_sched_scan_ssids) {
cbdf3507 1233 wpa_s->prev_sched_ssid = ssid;
b55aca46
JM
1234 do {
1235 ssid = ssid->next;
1236 } while (ssid &&
349493bd 1237 (wpas_network_disabled(wpa_s, ssid) ||
fea7c3a0 1238 !ssid->scan_ssid));
cbdf3507
LC
1239 break;
1240 }
1241 }
b59e6f26 1242
5edddf41 1243 next:
cbdf3507
LC
1244 wpa_s->prev_sched_ssid = ssid;
1245 ssid = ssid->next;
1246 }
1247
7c6a266c
JM
1248 if (params.num_filter_ssids == 0) {
1249 os_free(params.filter_ssids);
1250 params.filter_ssids = NULL;
1251 }
1252
a13e07ec
ES
1253 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1254 if (extra_ie) {
1255 params.extra_ies = wpabuf_head(extra_ie);
1256 params.extra_ies_len = wpabuf_len(extra_ie);
1257 }
cbdf3507 1258
cf70d298
RM
1259 if (wpa_s->conf->filter_rssi)
1260 params.filter_rssi = wpa_s->conf->filter_rssi;
1261
1cc0d6a0
BP
1262 /* See if user specified frequencies. If so, scan only those. */
1263 if (wpa_s->conf->freq_list && !params.freqs) {
1264 wpa_dbg(wpa_s, MSG_DEBUG,
1265 "Optimize scan based on conf->freq_list");
1266 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1267 }
1268
7c865c68
TB
1269 scan_params = &params;
1270
1271scan:
a8cb5a88 1272 if (ssid || !wpa_s->first_sched_scan) {
a8cb5a88
JM
1273 wpa_dbg(wpa_s, MSG_DEBUG,
1274 "Starting sched scan: interval %d timeout %d",
1275 wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
7d21a223
JM
1276 } else {
1277 wpa_dbg(wpa_s, MSG_DEBUG,
1278 "Starting sched scan: interval %d (no timeout)",
1279 wpa_s->sched_scan_interval);
a8cb5a88 1280 }
cbdf3507 1281
faf9a858
JM
1282 wpa_setband_scan_freqs(wpa_s, scan_params);
1283
7c865c68 1284 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params,
cbdf3507 1285 wpa_s->sched_scan_interval);
a13e07ec 1286 wpabuf_free(extra_ie);
b59e6f26 1287 os_free(params.filter_ssids);
cbdf3507
LC
1288 if (ret) {
1289 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1290 if (prev_state != wpa_s->wpa_state)
1291 wpa_supplicant_set_state(wpa_s, prev_state);
1292 return ret;
1293 }
1294
1295 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1296 if (ssid || !wpa_s->first_sched_scan) {
1297 wpa_s->sched_scan_timed_out = 0;
1298 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1299 wpa_supplicant_sched_scan_timeout,
1300 wpa_s, NULL);
1301 wpa_s->first_sched_scan = 0;
1302 wpa_s->sched_scan_timeout /= 2;
1303 wpa_s->sched_scan_interval *= 2;
a09fc1cc
DS
1304 if (wpa_s->sched_scan_timeout < wpa_s->sched_scan_interval) {
1305 wpa_s->sched_scan_interval = 10;
1306 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1307 }
cbdf3507
LC
1308 }
1309
7ed52f67
DS
1310 /* If there is no more ssids, start next time from the beginning */
1311 if (!ssid)
1312 wpa_s->prev_sched_ssid = NULL;
1313
cbdf3507
LC
1314 return 0;
1315}
1316
1317
6fc6879b
JM
1318/**
1319 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1320 * @wpa_s: Pointer to wpa_supplicant data
1321 *
1322 * This function is used to cancel a scan request scheduled with
1323 * wpa_supplicant_req_scan().
1324 */
1325void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1326{
f049052b 1327 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
6fc6879b
JM
1328 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
1329}
cb8564b1
DW
1330
1331
831770bf
CZ
1332/**
1333 * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
1334 * @wpa_s: Pointer to wpa_supplicant data
1335 *
1336 * This function is used to stop a delayed scheduled scan.
1337 */
1338void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
1339{
1340 if (!wpa_s->sched_scan_supported)
1341 return;
1342
1343 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
1344 eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
1345 wpa_s, NULL);
1346}
1347
1348
cbdf3507
LC
1349/**
1350 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1351 * @wpa_s: Pointer to wpa_supplicant data
1352 *
1353 * This function is used to stop a periodic scheduled scan.
1354 */
1355void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1356{
1357 if (!wpa_s->sched_scanning)
1358 return;
1359
1360 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1361 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1362 wpa_supplicant_stop_sched_scan(wpa_s);
1363}
1364
1365
2c09af30
JM
1366/**
1367 * wpa_supplicant_notify_scanning - Indicate possible scan state change
1368 * @wpa_s: Pointer to wpa_supplicant data
1369 * @scanning: Whether scanning is currently in progress
1370 *
1371 * This function is to generate scanning notifycations. It is called whenever
1372 * there may have been a change in scanning (scan started, completed, stopped).
1373 * wpas_notify_scanning() is called whenever the scanning state changed from the
1374 * previously notified state.
1375 */
cb8564b1
DW
1376void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1377 int scanning)
1378{
1379 if (wpa_s->scanning != scanning) {
1380 wpa_s->scanning = scanning;
8bac466b 1381 wpas_notify_scanning(wpa_s);
cb8564b1
DW
1382 }
1383}
1384
9ba9fa07
JM
1385
1386static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1387{
1388 int rate = 0;
1389 const u8 *ie;
1390 int i;
1391
1392 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1393 for (i = 0; ie && i < ie[1]; i++) {
1394 if ((ie[i + 2] & 0x7f) > rate)
1395 rate = ie[i + 2] & 0x7f;
1396 }
1397
1398 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1399 for (i = 0; ie && i < ie[1]; i++) {
1400 if ((ie[i + 2] & 0x7f) > rate)
1401 rate = ie[i + 2] & 0x7f;
1402 }
1403
1404 return rate;
1405}
1406
1407
2c09af30
JM
1408/**
1409 * wpa_scan_get_ie - Fetch a specified information element from a scan result
1410 * @res: Scan result entry
1411 * @ie: Information element identitifier (WLAN_EID_*)
1412 * Returns: Pointer to the information element (id field) or %NULL if not found
1413 *
1414 * This function returns the first matching information element in the scan
1415 * result.
1416 */
d1f9c410
JM
1417const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1418{
1419 const u8 *end, *pos;
1420
1421 pos = (const u8 *) (res + 1);
1422 end = pos + res->ie_len;
1423
1424 while (pos + 1 < end) {
1425 if (pos + 2 + pos[1] > end)
1426 break;
1427 if (pos[0] == ie)
1428 return pos;
1429 pos += 2 + pos[1];
1430 }
1431
1432 return NULL;
1433}
1434
1435
2c09af30
JM
1436/**
1437 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1438 * @res: Scan result entry
1439 * @vendor_type: Vendor type (four octets starting the IE payload)
1440 * Returns: Pointer to the information element (id field) or %NULL if not found
1441 *
1442 * This function returns the first matching information element in the scan
1443 * result.
1444 */
9ba9fa07
JM
1445const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1446 u32 vendor_type)
1447{
1448 const u8 *end, *pos;
1449
1450 pos = (const u8 *) (res + 1);
1451 end = pos + res->ie_len;
1452
1453 while (pos + 1 < end) {
1454 if (pos + 2 + pos[1] > end)
1455 break;
1456 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1457 vendor_type == WPA_GET_BE32(&pos[2]))
1458 return pos;
1459 pos += 2 + pos[1];
1460 }
1461
1462 return NULL;
1463}
1464
1465
aaeb9c98
JM
1466/**
1467 * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
1468 * @res: Scan result entry
1469 * @vendor_type: Vendor type (four octets starting the IE payload)
1470 * Returns: Pointer to the information element (id field) or %NULL if not found
1471 *
1472 * This function returns the first matching information element in the scan
1473 * result.
1474 *
1475 * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
1476 * from Beacon frames instead of either Beacon or Probe Response frames.
1477 */
1478const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
1479 u32 vendor_type)
1480{
1481 const u8 *end, *pos;
1482
1483 if (res->beacon_ie_len == 0)
1484 return NULL;
1485
1486 pos = (const u8 *) (res + 1);
1487 pos += res->ie_len;
1488 end = pos + res->beacon_ie_len;
1489
1490 while (pos + 1 < end) {
1491 if (pos + 2 + pos[1] > end)
1492 break;
1493 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1494 vendor_type == WPA_GET_BE32(&pos[2]))
1495 return pos;
1496 pos += 2 + pos[1];
1497 }
1498
1499 return NULL;
1500}
1501
1502
2c09af30
JM
1503/**
1504 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
1505 * @res: Scan result entry
1506 * @vendor_type: Vendor type (four octets starting the IE payload)
1507 * Returns: Pointer to the information element payload or %NULL if not found
1508 *
1509 * This function returns concatenated payload of possibly fragmented vendor
1510 * specific information elements in the scan result. The caller is responsible
1511 * for freeing the returned buffer.
1512 */
9ba9fa07
JM
1513struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1514 u32 vendor_type)
1515{
1516 struct wpabuf *buf;
1517 const u8 *end, *pos;
1518
1519 buf = wpabuf_alloc(res->ie_len);
1520 if (buf == NULL)
1521 return NULL;
1522
1523 pos = (const u8 *) (res + 1);
1524 end = pos + res->ie_len;
1525
54f489be
JM
1526 while (pos + 1 < end) {
1527 if (pos + 2 + pos[1] > end)
1528 break;
1529 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1530 vendor_type == WPA_GET_BE32(&pos[2]))
1531 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1532 pos += 2 + pos[1];
1533 }
1534
1535 if (wpabuf_len(buf) == 0) {
1536 wpabuf_free(buf);
1537 buf = NULL;
1538 }
1539
1540 return buf;
1541}
1542
1543
577db0ae
GM
1544/*
1545 * Channels with a great SNR can operate at full rate. What is a great SNR?
1546 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1547 * rule of thumb is that any SNR above 20 is good." This one
1548 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1549 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1550 * conservative value.
1551 */
1552#define GREAT_SNR 30
1553
9ba9fa07
JM
1554/* Compare function for sorting scan results. Return >0 if @b is considered
1555 * better. */
1556static int wpa_scan_result_compar(const void *a, const void *b)
1557{
577db0ae
GM
1558#define IS_5GHZ(n) (n > 4000)
1559#define MIN(a,b) a < b ? a : b
9ba9fa07
JM
1560 struct wpa_scan_res **_wa = (void *) a;
1561 struct wpa_scan_res **_wb = (void *) b;
1562 struct wpa_scan_res *wa = *_wa;
1563 struct wpa_scan_res *wb = *_wb;
1564 int wpa_a, wpa_b, maxrate_a, maxrate_b;
577db0ae 1565 int snr_a, snr_b;
9ba9fa07
JM
1566
1567 /* WPA/WPA2 support preferred */
1568 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1569 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1570 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1571 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1572
1573 if (wpa_b && !wpa_a)
1574 return 1;
1575 if (!wpa_b && wpa_a)
1576 return -1;
1577
1578 /* privacy support preferred */
1579 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1580 (wb->caps & IEEE80211_CAP_PRIVACY))
1581 return 1;
1582 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1583 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1584 return -1;
1585
577db0ae
GM
1586 if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
1587 !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
1588 snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
1589 snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
1590 } else {
1591 /* Not suitable information to calculate SNR, so use level */
1592 snr_a = wa->level;
1593 snr_b = wb->level;
1594 }
1595
577db0ae
GM
1596 /* best/max rate preferred if SNR close enough */
1597 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
9ba9fa07
JM
1598 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
1599 maxrate_a = wpa_scan_get_max_rate(wa);
1600 maxrate_b = wpa_scan_get_max_rate(wb);
1601 if (maxrate_a != maxrate_b)
1602 return maxrate_b - maxrate_a;
577db0ae
GM
1603 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1604 return IS_5GHZ(wa->freq) ? -1 : 1;
9ba9fa07
JM
1605 }
1606
1607 /* use freq for channel preference */
1608
577db0ae 1609 /* all things being equal, use SNR; if SNRs are
9ba9fa07
JM
1610 * identical, use quality values since some drivers may only report
1611 * that value and leave the signal level zero */
577db0ae 1612 if (snr_b == snr_a)
9ba9fa07 1613 return wb->qual - wa->qual;
577db0ae
GM
1614 return snr_b - snr_a;
1615#undef MIN
1616#undef IS_5GHZ
9ba9fa07
JM
1617}
1618
1619
41e650ae
JM
1620#ifdef CONFIG_WPS
1621/* Compare function for sorting scan results when searching a WPS AP for
1622 * provisioning. Return >0 if @b is considered better. */
1623static int wpa_scan_result_wps_compar(const void *a, const void *b)
1624{
1625 struct wpa_scan_res **_wa = (void *) a;
1626 struct wpa_scan_res **_wb = (void *) b;
1627 struct wpa_scan_res *wa = *_wa;
1628 struct wpa_scan_res *wb = *_wb;
1629 int uses_wps_a, uses_wps_b;
1630 struct wpabuf *wps_a, *wps_b;
1631 int res;
1632
1633 /* Optimization - check WPS IE existence before allocated memory and
1634 * doing full reassembly. */
1635 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1636 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1637 if (uses_wps_a && !uses_wps_b)
1638 return -1;
1639 if (!uses_wps_a && uses_wps_b)
1640 return 1;
1641
1642 if (uses_wps_a && uses_wps_b) {
1643 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1644 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1645 res = wps_ap_priority_compar(wps_a, wps_b);
1646 wpabuf_free(wps_a);
1647 wpabuf_free(wps_b);
1648 if (res)
1649 return res;
1650 }
1651
1652 /*
1653 * Do not use current AP security policy as a sorting criteria during
1654 * WPS provisioning step since the AP may get reconfigured at the
1655 * completion of provisioning.
1656 */
1657
1658 /* all things being equal, use signal level; if signal levels are
1659 * identical, use quality values since some drivers may only report
1660 * that value and leave the signal level zero */
1661 if (wb->level == wa->level)
1662 return wb->qual - wa->qual;
1663 return wb->level - wa->level;
1664}
1665#endif /* CONFIG_WPS */
1666
1667
aa820e02
JM
1668static void dump_scan_res(struct wpa_scan_results *scan_res)
1669{
76202aed 1670#ifndef CONFIG_NO_STDOUT_DEBUG
aa820e02
JM
1671 size_t i;
1672
1673 if (scan_res->res == NULL || scan_res->num == 0)
1674 return;
1675
1676 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1677
1678 for (i = 0; i < scan_res->num; i++) {
1679 struct wpa_scan_res *r = scan_res->res[i];
1cbe86e2 1680 u8 *pos;
aa820e02
JM
1681 if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
1682 == WPA_SCAN_LEVEL_DBM) {
1683 int snr = r->level - r->noise;
1684 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
c5f10e80
JM
1685 "noise=%d level=%d snr=%d%s flags=0x%x "
1686 "age=%u",
aa820e02
JM
1687 MAC2STR(r->bssid), r->freq, r->qual,
1688 r->noise, r->level, snr,
c5f10e80
JM
1689 snr >= GREAT_SNR ? "*" : "", r->flags,
1690 r->age);
aa820e02
JM
1691 } else {
1692 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
c5f10e80 1693 "noise=%d level=%d flags=0x%x age=%u",
aa820e02 1694 MAC2STR(r->bssid), r->freq, r->qual,
c5f10e80 1695 r->noise, r->level, r->flags, r->age);
aa820e02 1696 }
1cbe86e2
JM
1697 pos = (u8 *) (r + 1);
1698 if (r->ie_len)
1699 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
1700 pos += r->ie_len;
1701 if (r->beacon_ie_len)
1702 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
1703 pos, r->beacon_ie_len);
aa820e02 1704 }
76202aed 1705#endif /* CONFIG_NO_STDOUT_DEBUG */
aa820e02
JM
1706}
1707
1708
2c09af30
JM
1709/**
1710 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
1711 * @wpa_s: Pointer to wpa_supplicant data
1712 * @bssid: BSSID to check
1713 * Returns: 0 if the BSSID is filtered or 1 if not
1714 *
1715 * This function is used to filter out specific BSSIDs from scan reslts mainly
1716 * for testing purposes (SET bssid_filter ctrl_iface command).
1717 */
d445a5cd
JM
1718int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
1719 const u8 *bssid)
1720{
1721 size_t i;
1722
1723 if (wpa_s->bssid_filter == NULL)
1724 return 1;
1725
1726 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
1727 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
1728 ETH_ALEN) == 0)
1729 return 1;
1730 }
1731
1732 return 0;
1733}
1734
1735
1736static void filter_scan_res(struct wpa_supplicant *wpa_s,
1737 struct wpa_scan_results *res)
1738{
1739 size_t i, j;
1740
1741 if (wpa_s->bssid_filter == NULL)
1742 return;
1743
1744 for (i = 0, j = 0; i < res->num; i++) {
1745 if (wpa_supplicant_filter_bssid_match(wpa_s,
1746 res->res[i]->bssid)) {
1747 res->res[j++] = res->res[i];
1748 } else {
1749 os_free(res->res[i]);
1750 res->res[i] = NULL;
1751 }
1752 }
1753
1754 if (res->num != j) {
1755 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
1756 (int) (res->num - j));
1757 res->num = j;
1758 }
1759}
1760
1761
9ba9fa07
JM
1762/**
1763 * wpa_supplicant_get_scan_results - Get scan results
1764 * @wpa_s: Pointer to wpa_supplicant data
1765 * @info: Information about what was scanned or %NULL if not available
1766 * @new_scan: Whether a new scan was performed
1767 * Returns: Scan results, %NULL on failure
1768 *
1769 * This function request the current scan results from the driver and updates
1770 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
1771 * results with wpa_scan_results_free().
1772 */
1773struct wpa_scan_results *
1774wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
1775 struct scan_info *info, int new_scan)
1776{
1777 struct wpa_scan_results *scan_res;
1778 size_t i;
41e650ae 1779 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
9ba9fa07 1780
17fbb751 1781 scan_res = wpa_drv_get_scan_results2(wpa_s);
9ba9fa07 1782 if (scan_res == NULL) {
f049052b 1783 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
9ba9fa07
JM
1784 return NULL;
1785 }
c5f10e80
JM
1786 if (scan_res->fetch_time.sec == 0) {
1787 /*
1788 * Make sure we have a valid timestamp if the driver wrapper
1789 * does not set this.
1790 */
acb69cec 1791 os_get_reltime(&scan_res->fetch_time);
c5f10e80 1792 }
d445a5cd 1793 filter_scan_res(wpa_s, scan_res);
9ba9fa07 1794
41e650ae 1795#ifdef CONFIG_WPS
3187fd90 1796 if (wpas_wps_searching(wpa_s)) {
f049052b
BG
1797 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
1798 "provisioning rules");
41e650ae
JM
1799 compar = wpa_scan_result_wps_compar;
1800 }
1801#endif /* CONFIG_WPS */
1802
9ba9fa07 1803 qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
41e650ae 1804 compar);
aa820e02 1805 dump_scan_res(scan_res);
9ba9fa07
JM
1806
1807 wpa_bss_update_start(wpa_s);
1808 for (i = 0; i < scan_res->num; i++)
c5f10e80
JM
1809 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
1810 &scan_res->fetch_time);
9ba9fa07
JM
1811 wpa_bss_update_end(wpa_s, info, new_scan);
1812
1813 return scan_res;
1814}
1815
1816
2c09af30
JM
1817/**
1818 * wpa_supplicant_update_scan_results - Update scan results from the driver
1819 * @wpa_s: Pointer to wpa_supplicant data
1820 * Returns: 0 on success, -1 on failure
1821 *
1822 * This function updates the BSS table within wpa_supplicant based on the
1823 * currently available scan results from the driver without requesting a new
1824 * scan. This is used in cases where the driver indicates an association
1825 * (including roaming within ESS) and wpa_supplicant does not yet have the
1826 * needed information to complete the connection (e.g., to perform validation
1827 * steps in 4-way handshake).
1828 */
9ba9fa07
JM
1829int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
1830{
1831 struct wpa_scan_results *scan_res;
1832 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
1833 if (scan_res == NULL)
1834 return -1;
1835 wpa_scan_results_free(scan_res);
1836
1837 return 0;
1838}
66fe0f70
DS
1839
1840
1841/**
1842 * scan_only_handler - Reports scan results
1843 */
1844void scan_only_handler(struct wpa_supplicant *wpa_s,
1845 struct wpa_scan_results *scan_res)
1846{
1847 wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
d81c73be
JM
1848 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1849 wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
1850 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
1851 wpa_s->manual_scan_id);
1852 wpa_s->manual_scan_use_id = 0;
1853 } else {
1854 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1855 }
66fe0f70
DS
1856 wpas_notify_scan_results(wpa_s);
1857 wpas_notify_scan_done(wpa_s, 1);
d12a51b5
JM
1858 if (wpa_s->scan_work) {
1859 struct wpa_radio_work *work = wpa_s->scan_work;
1860 wpa_s->scan_work = NULL;
1861 radio_work_done(work);
1862 }
66fe0f70 1863}
407be00b
JM
1864
1865
1866int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
1867{
1868 return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
1869}
dd43aaa5
JM
1870
1871
1872struct wpa_driver_scan_params *
1873wpa_scan_clone_params(const struct wpa_driver_scan_params *src)
1874{
1875 struct wpa_driver_scan_params *params;
1876 size_t i;
1877 u8 *n;
1878
1879 params = os_zalloc(sizeof(*params));
1880 if (params == NULL)
1881 return NULL;
1882
1883 for (i = 0; i < src->num_ssids; i++) {
1884 if (src->ssids[i].ssid) {
1885 n = os_malloc(src->ssids[i].ssid_len);
1886 if (n == NULL)
1887 goto failed;
1888 os_memcpy(n, src->ssids[i].ssid,
1889 src->ssids[i].ssid_len);
1890 params->ssids[i].ssid = n;
1891 params->ssids[i].ssid_len = src->ssids[i].ssid_len;
1892 }
1893 }
1894 params->num_ssids = src->num_ssids;
1895
1896 if (src->extra_ies) {
1897 n = os_malloc(src->extra_ies_len);
1898 if (n == NULL)
1899 goto failed;
1900 os_memcpy(n, src->extra_ies, src->extra_ies_len);
1901 params->extra_ies = n;
1902 params->extra_ies_len = src->extra_ies_len;
1903 }
1904
1905 if (src->freqs) {
1906 int len = int_array_len(src->freqs);
1907 params->freqs = os_malloc((len + 1) * sizeof(int));
1908 if (params->freqs == NULL)
1909 goto failed;
1910 os_memcpy(params->freqs, src->freqs, (len + 1) * sizeof(int));
1911 }
1912
1913 if (src->filter_ssids) {
fd67275b 1914 params->filter_ssids = os_malloc(sizeof(*params->filter_ssids) *
dd43aaa5
JM
1915 src->num_filter_ssids);
1916 if (params->filter_ssids == NULL)
1917 goto failed;
1918 os_memcpy(params->filter_ssids, src->filter_ssids,
fd67275b
EL
1919 sizeof(*params->filter_ssids) *
1920 src->num_filter_ssids);
dd43aaa5
JM
1921 params->num_filter_ssids = src->num_filter_ssids;
1922 }
1923
1924 params->filter_rssi = src->filter_rssi;
1925 params->p2p_probe = src->p2p_probe;
1926 params->only_new_results = src->only_new_results;
57a8f8af 1927 params->low_priority = src->low_priority;
dd43aaa5
JM
1928
1929 return params;
1930
1931failed:
1932 wpa_scan_free_params(params);
1933 return NULL;
1934}
1935
1936
1937void wpa_scan_free_params(struct wpa_driver_scan_params *params)
1938{
1939 size_t i;
1940
1941 if (params == NULL)
1942 return;
1943
1944 for (i = 0; i < params->num_ssids; i++)
1945 os_free((u8 *) params->ssids[i].ssid);
1946 os_free((u8 *) params->extra_ies);
1947 os_free(params->freqs);
1948 os_free(params->filter_ssids);
1949 os_free(params);
1950}
737e7a08
AB
1951
1952
1953int wpas_start_pno(struct wpa_supplicant *wpa_s)
1954{
1955 int ret, interval;
4ed34922 1956 size_t i, num_ssid, num_match_ssid;
737e7a08
AB
1957 struct wpa_ssid *ssid;
1958 struct wpa_driver_scan_params params;
1959
5e3ddf4d
AB
1960 if (!wpa_s->sched_scan_supported)
1961 return -1;
1962
737e7a08
AB
1963 if (wpa_s->pno || wpa_s->pno_sched_pending)
1964 return 0;
1965
1966 if ((wpa_s->wpa_state > WPA_SCANNING) &&
1967 (wpa_s->wpa_state <= WPA_COMPLETED)) {
1968 wpa_printf(MSG_ERROR, "PNO: In assoc process");
1969 return -EAGAIN;
1970 }
1971
1972 if (wpa_s->wpa_state == WPA_SCANNING) {
1973 wpa_supplicant_cancel_scan(wpa_s);
1974 if (wpa_s->sched_scanning) {
1975 wpa_printf(MSG_DEBUG, "Schedule PNO on completion of "
1976 "ongoing sched scan");
1977 wpa_supplicant_cancel_sched_scan(wpa_s);
1978 wpa_s->pno_sched_pending = 1;
1979 return 0;
1980 }
1981 }
1982
1983 os_memset(&params, 0, sizeof(params));
1984
4ed34922 1985 num_ssid = num_match_ssid = 0;
737e7a08
AB
1986 ssid = wpa_s->conf->ssid;
1987 while (ssid) {
4ed34922
DS
1988 if (!wpas_network_disabled(wpa_s, ssid)) {
1989 num_match_ssid++;
1990 if (ssid->scan_ssid)
1991 num_ssid++;
1992 }
737e7a08
AB
1993 ssid = ssid->next;
1994 }
4ed34922
DS
1995
1996 if (num_match_ssid == 0) {
1997 wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
1998 return -1;
1999 }
2000
2001 if (num_match_ssid > num_ssid) {
2002 params.num_ssids++; /* wildcard */
2003 num_ssid++;
2004 }
2005
737e7a08
AB
2006 if (num_ssid > WPAS_MAX_SCAN_SSIDS) {
2007 wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
2008 "%u", WPAS_MAX_SCAN_SSIDS, (unsigned int) num_ssid);
2009 num_ssid = WPAS_MAX_SCAN_SSIDS;
2010 }
2011
4ed34922
DS
2012 if (num_match_ssid > wpa_s->max_match_sets) {
2013 num_match_ssid = wpa_s->max_match_sets;
2014 wpa_dbg(wpa_s, MSG_DEBUG, "PNO: Too many SSIDs to match");
737e7a08 2015 }
4ed34922
DS
2016 params.filter_ssids = os_calloc(num_match_ssid,
2017 sizeof(struct wpa_driver_scan_filter));
737e7a08
AB
2018 if (params.filter_ssids == NULL)
2019 return -1;
2020 i = 0;
2021 ssid = wpa_s->conf->ssid;
2022 while (ssid) {
2023 if (!wpas_network_disabled(wpa_s, ssid)) {
4ed34922
DS
2024 if (ssid->scan_ssid && params.num_ssids < num_ssid) {
2025 params.ssids[params.num_ssids].ssid =
2026 ssid->ssid;
2027 params.ssids[params.num_ssids].ssid_len =
2028 ssid->ssid_len;
2029 params.num_ssids++;
2030 }
737e7a08
AB
2031 os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
2032 ssid->ssid_len);
2033 params.filter_ssids[i].ssid_len = ssid->ssid_len;
2034 params.num_filter_ssids++;
2035 i++;
4ed34922 2036 if (i == num_match_ssid)
737e7a08
AB
2037 break;
2038 }
2039 ssid = ssid->next;
2040 }
2041
2042 if (wpa_s->conf->filter_rssi)
2043 params.filter_rssi = wpa_s->conf->filter_rssi;
2044
2045 interval = wpa_s->conf->sched_scan_interval ?
2046 wpa_s->conf->sched_scan_interval : 10;
2047
d3c9c35f
DS
2048 if (params.freqs == NULL && wpa_s->manual_sched_scan_freqs) {
2049 wpa_dbg(wpa_s, MSG_DEBUG, "Limit sched scan to specified channels");
2050 params.freqs = wpa_s->manual_sched_scan_freqs;
2051 }
2052
737e7a08
AB
2053 ret = wpa_supplicant_start_sched_scan(wpa_s, &params, interval);
2054 os_free(params.filter_ssids);
2055 if (ret == 0)
2056 wpa_s->pno = 1;
5e3ddf4d
AB
2057 else
2058 wpa_msg(wpa_s, MSG_ERROR, "Failed to schedule PNO");
737e7a08
AB
2059 return ret;
2060}
2061
2062
2063int wpas_stop_pno(struct wpa_supplicant *wpa_s)
2064{
2065 int ret = 0;
2066
5e3ddf4d
AB
2067 if (!wpa_s->pno)
2068 return 0;
2069
2070 ret = wpa_supplicant_stop_sched_scan(wpa_s);
737e7a08 2071
5e3ddf4d 2072 wpa_s->pno = 0;
737e7a08
AB
2073 wpa_s->pno_sched_pending = 0;
2074
2075 if (wpa_s->wpa_state == WPA_SCANNING)
2076 wpa_supplicant_req_scan(wpa_s, 0, 0);
2077
2078 return ret;
2079}