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