wpa_supplicant: "don't care" value for pbss in ssid structure
[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
100         if (wpa_s->p2p_mgmt)
101                 return 0; /* no normal network profiles on p2p_mgmt interface */
102
103         while (ssid) {
104                 if (!wpas_network_disabled(wpa_s, ssid))
105                         count++;
106                 else
107                         disabled++;
108                 ssid = ssid->next;
109         }
110         if (wpa_s->conf->cred && wpa_s->conf->interworking &&
111             wpa_s->conf->auto_interworking)
112                 count++;
113         if (count == 0 && disabled > 0) {
114                 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks (%d disabled "
115                         "networks)", disabled);
116         }
117         return count;
118 }
119
120
121 static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
122                                      struct wpa_ssid *ssid)
123 {
124         while (ssid) {
125                 if (!wpas_network_disabled(wpa_s, ssid))
126                         break;
127                 ssid = ssid->next;
128         }
129
130         /* ap_scan=2 mode - try to associate with each SSID. */
131         if (ssid == NULL) {
132                 wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
133                         "end of scan list - go back to beginning");
134                 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
135                 wpa_supplicant_req_scan(wpa_s, 0, 0);
136                 return;
137         }
138         if (ssid->next) {
139                 /* Continue from the next SSID on the next attempt. */
140                 wpa_s->prev_scan_ssid = ssid;
141         } else {
142                 /* Start from the beginning of the SSID list. */
143                 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
144         }
145         wpa_supplicant_associate(wpa_s, NULL, ssid);
146 }
147
148
149 static void wpas_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
150 {
151         struct wpa_supplicant *wpa_s = work->wpa_s;
152         struct wpa_driver_scan_params *params = work->ctx;
153         int ret;
154
155         if (deinit) {
156                 if (!work->started) {
157                         wpa_scan_free_params(params);
158                         return;
159                 }
160                 wpa_supplicant_notify_scanning(wpa_s, 0);
161                 wpas_notify_scan_done(wpa_s, 0);
162                 wpa_s->scan_work = NULL;
163                 return;
164         }
165
166         if (wpas_update_random_addr_disassoc(wpa_s) < 0) {
167                 wpa_msg(wpa_s, MSG_INFO,
168                         "Failed to assign random MAC address for a scan");
169                 radio_work_done(work);
170                 return;
171         }
172
173         wpa_supplicant_notify_scanning(wpa_s, 1);
174
175         if (wpa_s->clear_driver_scan_cache) {
176                 wpa_printf(MSG_DEBUG,
177                            "Request driver to clear scan cache due to local BSS flush");
178                 params->only_new_results = 1;
179         }
180         ret = wpa_drv_scan(wpa_s, params);
181         wpa_scan_free_params(params);
182         work->ctx = NULL;
183         if (ret) {
184                 int retry = wpa_s->last_scan_req != MANUAL_SCAN_REQ;
185
186                 if (wpa_s->disconnected)
187                         retry = 0;
188
189                 wpa_supplicant_notify_scanning(wpa_s, 0);
190                 wpas_notify_scan_done(wpa_s, 0);
191                 if (wpa_s->wpa_state == WPA_SCANNING)
192                         wpa_supplicant_set_state(wpa_s,
193                                                  wpa_s->scan_prev_wpa_state);
194                 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=%d%s",
195                         ret, retry ? " retry=1" : "");
196                 radio_work_done(work);
197
198                 if (retry) {
199                         /* Restore scan_req since we will try to scan again */
200                         wpa_s->scan_req = wpa_s->last_scan_req;
201                         wpa_supplicant_req_scan(wpa_s, 1, 0);
202                 }
203                 return;
204         }
205
206         os_get_reltime(&wpa_s->scan_trigger_time);
207         wpa_s->scan_runs++;
208         wpa_s->normal_scans++;
209         wpa_s->own_scan_requested = 1;
210         wpa_s->clear_driver_scan_cache = 0;
211         wpa_s->scan_work = work;
212 }
213
214
215 /**
216  * wpa_supplicant_trigger_scan - Request driver to start a scan
217  * @wpa_s: Pointer to wpa_supplicant data
218  * @params: Scan parameters
219  * Returns: 0 on success, -1 on failure
220  */
221 int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
222                                 struct wpa_driver_scan_params *params)
223 {
224         struct wpa_driver_scan_params *ctx;
225
226         if (wpa_s->scan_work) {
227                 wpa_dbg(wpa_s, MSG_INFO, "Reject scan trigger since one is already pending");
228                 return -1;
229         }
230
231         ctx = wpa_scan_clone_params(params);
232         if (ctx == NULL)
233                 return -1;
234
235         if (radio_add_work(wpa_s, 0, "scan", 0, wpas_trigger_scan_cb, ctx) < 0)
236         {
237                 wpa_scan_free_params(ctx);
238                 return -1;
239         }
240
241         return 0;
242 }
243
244
245 static void
246 wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
247 {
248         struct wpa_supplicant *wpa_s = eloop_ctx;
249
250         wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
251
252         if (wpa_supplicant_req_sched_scan(wpa_s))
253                 wpa_supplicant_req_scan(wpa_s, 0, 0);
254 }
255
256
257 static void
258 wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
259 {
260         struct wpa_supplicant *wpa_s = eloop_ctx;
261
262         wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
263
264         wpa_s->sched_scan_timed_out = 1;
265         wpa_supplicant_cancel_sched_scan(wpa_s);
266 }
267
268
269 static int
270 wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
271                                 struct wpa_driver_scan_params *params)
272 {
273         int ret;
274
275         wpa_supplicant_notify_scanning(wpa_s, 1);
276         ret = wpa_drv_sched_scan(wpa_s, params);
277         if (ret)
278                 wpa_supplicant_notify_scanning(wpa_s, 0);
279         else
280                 wpa_s->sched_scanning = 1;
281
282         return ret;
283 }
284
285
286 static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
287 {
288         int ret;
289
290         ret = wpa_drv_stop_sched_scan(wpa_s);
291         if (ret) {
292                 wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
293                 /* TODO: what to do if stopping fails? */
294                 return -1;
295         }
296
297         return ret;
298 }
299
300
301 static struct wpa_driver_scan_filter *
302 wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
303 {
304         struct wpa_driver_scan_filter *ssids;
305         struct wpa_ssid *ssid;
306         size_t count;
307
308         *num_ssids = 0;
309         if (!conf->filter_ssids)
310                 return NULL;
311
312         for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
313                 if (ssid->ssid && ssid->ssid_len)
314                         count++;
315         }
316         if (count == 0)
317                 return NULL;
318         ssids = os_calloc(count, sizeof(struct wpa_driver_scan_filter));
319         if (ssids == NULL)
320                 return NULL;
321
322         for (ssid = conf->ssid; ssid; ssid = ssid->next) {
323                 if (!ssid->ssid || !ssid->ssid_len)
324                         continue;
325                 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
326                 ssids[*num_ssids].ssid_len = ssid->ssid_len;
327                 (*num_ssids)++;
328         }
329
330         return ssids;
331 }
332
333
334 static void wpa_supplicant_optimize_freqs(
335         struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
336 {
337 #ifdef CONFIG_P2P
338         if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
339             wpa_s->go_params) {
340                 /* Optimize provisioning state scan based on GO information */
341                 if (wpa_s->p2p_in_provisioning < 5 &&
342                     wpa_s->go_params->freq > 0) {
343                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
344                                 "preferred frequency %d MHz",
345                                 wpa_s->go_params->freq);
346                         params->freqs = os_calloc(2, sizeof(int));
347                         if (params->freqs)
348                                 params->freqs[0] = wpa_s->go_params->freq;
349                 } else if (wpa_s->p2p_in_provisioning < 8 &&
350                            wpa_s->go_params->freq_list[0]) {
351                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
352                                 "channels");
353                         int_array_concat(&params->freqs,
354                                          wpa_s->go_params->freq_list);
355                         if (params->freqs)
356                                 int_array_sort_unique(params->freqs);
357                 }
358                 wpa_s->p2p_in_provisioning++;
359         }
360
361         if (params->freqs == NULL && wpa_s->p2p_in_invitation) {
362                 /*
363                  * Optimize scan based on GO information during persistent
364                  * group reinvocation
365                  */
366                 if (wpa_s->p2p_in_invitation < 5 &&
367                     wpa_s->p2p_invite_go_freq > 0) {
368                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO preferred frequency %d MHz during invitation",
369                                 wpa_s->p2p_invite_go_freq);
370                         params->freqs = os_calloc(2, sizeof(int));
371                         if (params->freqs)
372                                 params->freqs[0] = wpa_s->p2p_invite_go_freq;
373                 }
374                 wpa_s->p2p_in_invitation++;
375                 if (wpa_s->p2p_in_invitation > 20) {
376                         /*
377                          * This should not really happen since the variable is
378                          * cleared on group removal, but if it does happen, make
379                          * sure we do not get stuck in special invitation scan
380                          * mode.
381                          */
382                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Clear p2p_in_invitation");
383                         wpa_s->p2p_in_invitation = 0;
384                 }
385         }
386 #endif /* CONFIG_P2P */
387
388 #ifdef CONFIG_WPS
389         if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
390                 /*
391                  * Optimize post-provisioning scan based on channel used
392                  * during provisioning.
393                  */
394                 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
395                         "that was used during provisioning", wpa_s->wps_freq);
396                 params->freqs = os_calloc(2, sizeof(int));
397                 if (params->freqs)
398                         params->freqs[0] = wpa_s->wps_freq;
399                 wpa_s->after_wps--;
400         } else if (wpa_s->after_wps)
401                 wpa_s->after_wps--;
402
403         if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
404         {
405                 /* Optimize provisioning scan based on already known channel */
406                 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
407                         wpa_s->wps_freq);
408                 params->freqs = os_calloc(2, sizeof(int));
409                 if (params->freqs)
410                         params->freqs[0] = wpa_s->wps_freq;
411                 wpa_s->known_wps_freq = 0; /* only do this once */
412         }
413 #endif /* CONFIG_WPS */
414 }
415
416
417 #ifdef CONFIG_INTERWORKING
418 static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
419                                            struct wpabuf *buf)
420 {
421         wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
422         wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
423                       1 + ETH_ALEN);
424         wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
425         /* No Venue Info */
426         if (!is_zero_ether_addr(wpa_s->conf->hessid))
427                 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
428 }
429 #endif /* CONFIG_INTERWORKING */
430
431
432 static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
433 {
434         struct wpabuf *extra_ie = NULL;
435         u8 ext_capab[18];
436         int ext_capab_len;
437 #ifdef CONFIG_WPS
438         int wps = 0;
439         enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
440 #endif /* CONFIG_WPS */
441
442         ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
443                                              sizeof(ext_capab));
444         if (ext_capab_len > 0 &&
445             wpabuf_resize(&extra_ie, ext_capab_len) == 0)
446                 wpabuf_put_data(extra_ie, ext_capab, ext_capab_len);
447
448 #ifdef CONFIG_INTERWORKING
449         if (wpa_s->conf->interworking &&
450             wpabuf_resize(&extra_ie, 100) == 0)
451                 wpas_add_interworking_elements(wpa_s, extra_ie);
452 #endif /* CONFIG_INTERWORKING */
453
454 #ifdef CONFIG_WPS
455         wps = wpas_wps_in_use(wpa_s, &req_type);
456
457         if (wps) {
458                 struct wpabuf *wps_ie;
459                 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
460                                                 DEV_PW_DEFAULT,
461                                                 &wpa_s->wps->dev,
462                                                 wpa_s->wps->uuid, req_type,
463                                                 0, NULL);
464                 if (wps_ie) {
465                         if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
466                                 wpabuf_put_buf(extra_ie, wps_ie);
467                         wpabuf_free(wps_ie);
468                 }
469         }
470
471 #ifdef CONFIG_P2P
472         if (wps) {
473                 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
474                 if (wpabuf_resize(&extra_ie, ielen) == 0)
475                         wpas_p2p_scan_ie(wpa_s, extra_ie);
476         }
477 #endif /* CONFIG_P2P */
478
479         wpa_supplicant_mesh_add_scan_ie(wpa_s, &extra_ie);
480
481 #endif /* CONFIG_WPS */
482
483 #ifdef CONFIG_HS20
484         if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 7) == 0)
485                 wpas_hs20_add_indication(extra_ie, -1);
486 #endif /* CONFIG_HS20 */
487
488 #ifdef CONFIG_FST
489         if (wpa_s->fst_ies &&
490             wpabuf_resize(&extra_ie, wpabuf_len(wpa_s->fst_ies)) == 0)
491                 wpabuf_put_buf(extra_ie, wpa_s->fst_ies);
492 #endif /* CONFIG_FST */
493
494 #ifdef CONFIG_MBO
495         /* Send cellular capabilities for potential MBO STAs */
496         if (wpabuf_resize(&extra_ie, 9) == 0)
497                 wpas_mbo_scan_ie(wpa_s, extra_ie);
498 #endif /* CONFIG_MBO */
499
500         return extra_ie;
501 }
502
503
504 #ifdef CONFIG_P2P
505
506 /*
507  * Check whether there are any enabled networks or credentials that could be
508  * used for a non-P2P connection.
509  */
510 static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
511 {
512         struct wpa_ssid *ssid;
513
514         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
515                 if (wpas_network_disabled(wpa_s, ssid))
516                         continue;
517                 if (!ssid->p2p_group)
518                         return 1;
519         }
520
521         if (wpa_s->conf->cred && wpa_s->conf->interworking &&
522             wpa_s->conf->auto_interworking)
523                 return 1;
524
525         return 0;
526 }
527
528 #endif /* CONFIG_P2P */
529
530
531 static void wpa_setband_scan_freqs_list(struct wpa_supplicant *wpa_s,
532                                         enum hostapd_hw_mode band,
533                                         struct wpa_driver_scan_params *params)
534 {
535         /* Include only supported channels for the specified band */
536         struct hostapd_hw_modes *mode;
537         int count, i;
538
539         mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band);
540         if (mode == NULL) {
541                 /* No channels supported in this band - use empty list */
542                 params->freqs = os_zalloc(sizeof(int));
543                 return;
544         }
545
546         params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
547         if (params->freqs == NULL)
548                 return;
549         for (count = 0, i = 0; i < mode->num_channels; i++) {
550                 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
551                         continue;
552                 params->freqs[count++] = mode->channels[i].freq;
553         }
554 }
555
556
557 static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
558                                    struct wpa_driver_scan_params *params)
559 {
560         if (wpa_s->hw.modes == NULL)
561                 return; /* unknown what channels the driver supports */
562         if (params->freqs)
563                 return; /* already using a limited channel set */
564         if (wpa_s->setband == WPA_SETBAND_5G)
565                 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
566                                             params);
567         else if (wpa_s->setband == WPA_SETBAND_2G)
568                 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G,
569                                             params);
570 }
571
572
573 static void wpa_set_scan_ssids(struct wpa_supplicant *wpa_s,
574                                struct wpa_driver_scan_params *params,
575                                size_t max_ssids)
576 {
577         unsigned int i;
578         struct wpa_ssid *ssid;
579
580         for (i = 0; i < wpa_s->scan_id_count; i++) {
581                 unsigned int j;
582
583                 ssid = wpa_config_get_network(wpa_s->conf, wpa_s->scan_id[i]);
584                 if (!ssid || !ssid->scan_ssid)
585                         continue;
586
587                 for (j = 0; j < params->num_ssids; j++) {
588                         if (params->ssids[j].ssid_len == ssid->ssid_len &&
589                             params->ssids[j].ssid &&
590                             os_memcmp(params->ssids[j].ssid, ssid->ssid,
591                                       ssid->ssid_len) == 0)
592                                 break;
593                 }
594                 if (j < params->num_ssids)
595                         continue; /* already in the list */
596
597                 if (params->num_ssids + 1 > max_ssids) {
598                         wpa_printf(MSG_DEBUG,
599                                    "Over max scan SSIDs for manual request");
600                         break;
601                 }
602
603                 wpa_printf(MSG_DEBUG, "Scan SSID (manual request): %s",
604                            wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
605                 params->ssids[params->num_ssids].ssid = ssid->ssid;
606                 params->ssids[params->num_ssids].ssid_len = ssid->ssid_len;
607                 params->num_ssids++;
608         }
609
610         wpa_s->scan_id_count = 0;
611 }
612
613
614 static int wpa_set_ssids_from_scan_req(struct wpa_supplicant *wpa_s,
615                                        struct wpa_driver_scan_params *params,
616                                        size_t max_ssids)
617 {
618         unsigned int i;
619
620         if (wpa_s->ssids_from_scan_req == NULL ||
621             wpa_s->num_ssids_from_scan_req == 0)
622                 return 0;
623
624         if (wpa_s->num_ssids_from_scan_req > max_ssids) {
625                 wpa_s->num_ssids_from_scan_req = max_ssids;
626                 wpa_printf(MSG_DEBUG, "Over max scan SSIDs from scan req: %u",
627                            (unsigned int) max_ssids);
628         }
629
630         for (i = 0; i < wpa_s->num_ssids_from_scan_req; i++) {
631                 params->ssids[i].ssid = wpa_s->ssids_from_scan_req[i].ssid;
632                 params->ssids[i].ssid_len =
633                         wpa_s->ssids_from_scan_req[i].ssid_len;
634                 wpa_hexdump_ascii(MSG_DEBUG, "specific SSID",
635                                   params->ssids[i].ssid,
636                                   params->ssids[i].ssid_len);
637         }
638
639         params->num_ssids = wpa_s->num_ssids_from_scan_req;
640         wpa_s->num_ssids_from_scan_req = 0;
641         return 1;
642 }
643
644
645 static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
646 {
647         struct wpa_supplicant *wpa_s = eloop_ctx;
648         struct wpa_ssid *ssid;
649         int ret, p2p_in_prog;
650         struct wpabuf *extra_ie = NULL;
651         struct wpa_driver_scan_params params;
652         struct wpa_driver_scan_params *scan_params;
653         size_t max_ssids;
654         int connect_without_scan = 0;
655
656         if (wpa_s->pno || wpa_s->pno_sched_pending) {
657                 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - PNO is in progress");
658                 return;
659         }
660
661         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
662                 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
663                 return;
664         }
665
666         if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
667                 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
668                 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
669                 return;
670         }
671
672         if (wpa_s->scanning) {
673                 /*
674                  * If we are already in scanning state, we shall reschedule the
675                  * the incoming scan request.
676                  */
677                 wpa_dbg(wpa_s, MSG_DEBUG, "Already scanning - Reschedule the incoming scan req");
678                 wpa_supplicant_req_scan(wpa_s, 1, 0);
679                 return;
680         }
681
682         if (!wpa_supplicant_enabled_networks(wpa_s) &&
683             wpa_s->scan_req == NORMAL_SCAN_REQ) {
684                 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
685                 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
686                 return;
687         }
688
689         if (wpa_s->conf->ap_scan != 0 &&
690             (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
691                 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
692                         "overriding ap_scan configuration");
693                 wpa_s->conf->ap_scan = 0;
694                 wpas_notify_ap_scan_changed(wpa_s);
695         }
696
697         if (wpa_s->conf->ap_scan == 0) {
698                 wpa_supplicant_gen_assoc_event(wpa_s);
699                 return;
700         }
701
702         ssid = NULL;
703         if (wpa_s->scan_req != MANUAL_SCAN_REQ &&
704             wpa_s->connect_without_scan) {
705                 connect_without_scan = 1;
706                 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
707                         if (ssid == wpa_s->connect_without_scan)
708                                 break;
709                 }
710         }
711
712         p2p_in_prog = wpas_p2p_in_progress(wpa_s);
713         if (p2p_in_prog && p2p_in_prog != 2 &&
714             (!ssid ||
715              (ssid->mode != WPAS_MODE_AP && ssid->mode != WPAS_MODE_P2P_GO))) {
716                 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan while P2P operation is in progress");
717                 wpa_supplicant_req_scan(wpa_s, 5, 0);
718                 return;
719         }
720
721         if (wpa_s->conf->ap_scan == 2)
722                 max_ssids = 1;
723         else {
724                 max_ssids = wpa_s->max_scan_ssids;
725                 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
726                         max_ssids = WPAS_MAX_SCAN_SSIDS;
727         }
728
729         wpa_s->last_scan_req = wpa_s->scan_req;
730         wpa_s->scan_req = NORMAL_SCAN_REQ;
731
732         if (connect_without_scan) {
733                 wpa_s->connect_without_scan = NULL;
734                 if (ssid) {
735                         wpa_printf(MSG_DEBUG, "Start a pre-selected network "
736                                    "without scan step");
737                         wpa_supplicant_associate(wpa_s, NULL, ssid);
738                         return;
739                 }
740         }
741
742         os_memset(&params, 0, sizeof(params));
743
744         wpa_s->scan_prev_wpa_state = wpa_s->wpa_state;
745         if (wpa_s->wpa_state == WPA_DISCONNECTED ||
746             wpa_s->wpa_state == WPA_INACTIVE)
747                 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
748
749         /*
750          * If autoscan has set its own scanning parameters
751          */
752         if (wpa_s->autoscan_params != NULL) {
753                 scan_params = wpa_s->autoscan_params;
754                 goto scan;
755         }
756
757         if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
758             wpa_set_ssids_from_scan_req(wpa_s, &params, max_ssids)) {
759                 wpa_printf(MSG_DEBUG, "Use specific SSIDs from SCAN command");
760                 goto ssid_list_set;
761         }
762
763 #ifdef CONFIG_P2P
764         if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
765             wpa_s->go_params && !wpa_s->conf->passive_scan) {
766                 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
767                            wpa_s->p2p_in_provisioning,
768                            wpa_s->show_group_started);
769                 params.ssids[0].ssid = wpa_s->go_params->ssid;
770                 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
771                 params.num_ssids = 1;
772                 goto ssid_list_set;
773         }
774
775         if (wpa_s->p2p_in_invitation) {
776                 if (wpa_s->current_ssid) {
777                         wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during invitation");
778                         params.ssids[0].ssid = wpa_s->current_ssid->ssid;
779                         params.ssids[0].ssid_len =
780                                 wpa_s->current_ssid->ssid_len;
781                         params.num_ssids = 1;
782                 } else {
783                         wpa_printf(MSG_DEBUG, "P2P: No specific SSID known for scan during invitation");
784                 }
785                 goto ssid_list_set;
786         }
787 #endif /* CONFIG_P2P */
788
789         /* Find the starting point from which to continue scanning */
790         ssid = wpa_s->conf->ssid;
791         if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
792                 while (ssid) {
793                         if (ssid == wpa_s->prev_scan_ssid) {
794                                 ssid = ssid->next;
795                                 break;
796                         }
797                         ssid = ssid->next;
798                 }
799         }
800
801         if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
802 #ifdef CONFIG_AP
803             !wpa_s->ap_iface &&
804 #endif /* CONFIG_AP */
805             wpa_s->conf->ap_scan == 2) {
806                 wpa_s->connect_without_scan = NULL;
807                 wpa_s->prev_scan_wildcard = 0;
808                 wpa_supplicant_assoc_try(wpa_s, ssid);
809                 return;
810         } else if (wpa_s->conf->ap_scan == 2) {
811                 /*
812                  * User-initiated scan request in ap_scan == 2; scan with
813                  * wildcard SSID.
814                  */
815                 ssid = NULL;
816         } else if (wpa_s->reattach && wpa_s->current_ssid != NULL) {
817                 /*
818                  * Perform single-channel single-SSID scan for
819                  * reassociate-to-same-BSS operation.
820                  */
821                 /* Setup SSID */
822                 ssid = wpa_s->current_ssid;
823                 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
824                                   ssid->ssid, ssid->ssid_len);
825                 params.ssids[0].ssid = ssid->ssid;
826                 params.ssids[0].ssid_len = ssid->ssid_len;
827                 params.num_ssids = 1;
828
829                 /*
830                  * Allocate memory for frequency array, allocate one extra
831                  * slot for the zero-terminator.
832                  */
833                 params.freqs = os_malloc(sizeof(int) * 2);
834                 if (params.freqs == NULL) {
835                         wpa_dbg(wpa_s, MSG_ERROR, "Memory allocation failed");
836                         return;
837                 }
838                 params.freqs[0] = wpa_s->assoc_freq;
839                 params.freqs[1] = 0;
840
841                 /*
842                  * Reset the reattach flag so that we fall back to full scan if
843                  * this scan fails.
844                  */
845                 wpa_s->reattach = 0;
846         } else {
847                 struct wpa_ssid *start = ssid, *tssid;
848                 int freqs_set = 0;
849                 if (ssid == NULL && max_ssids > 1)
850                         ssid = wpa_s->conf->ssid;
851                 while (ssid) {
852                         if (!wpas_network_disabled(wpa_s, ssid) &&
853                             ssid->scan_ssid) {
854                                 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
855                                                   ssid->ssid, ssid->ssid_len);
856                                 params.ssids[params.num_ssids].ssid =
857                                         ssid->ssid;
858                                 params.ssids[params.num_ssids].ssid_len =
859                                         ssid->ssid_len;
860                                 params.num_ssids++;
861                                 if (params.num_ssids + 1 >= max_ssids)
862                                         break;
863                         }
864                         ssid = ssid->next;
865                         if (ssid == start)
866                                 break;
867                         if (ssid == NULL && max_ssids > 1 &&
868                             start != wpa_s->conf->ssid)
869                                 ssid = wpa_s->conf->ssid;
870                 }
871
872                 if (wpa_s->scan_id_count &&
873                     wpa_s->last_scan_req == MANUAL_SCAN_REQ)
874                         wpa_set_scan_ssids(wpa_s, &params, max_ssids);
875
876                 for (tssid = wpa_s->conf->ssid;
877                      wpa_s->last_scan_req != MANUAL_SCAN_REQ && tssid;
878                      tssid = tssid->next) {
879                         if (wpas_network_disabled(wpa_s, tssid))
880                                 continue;
881                         if ((params.freqs || !freqs_set) && tssid->scan_freq) {
882                                 int_array_concat(&params.freqs,
883                                                  tssid->scan_freq);
884                         } else {
885                                 os_free(params.freqs);
886                                 params.freqs = NULL;
887                         }
888                         freqs_set = 1;
889                 }
890                 int_array_sort_unique(params.freqs);
891         }
892
893         if (ssid && max_ssids == 1) {
894                 /*
895                  * If the driver is limited to 1 SSID at a time interleave
896                  * wildcard SSID scans with specific SSID scans to avoid
897                  * waiting a long time for a wildcard scan.
898                  */
899                 if (!wpa_s->prev_scan_wildcard) {
900                         params.ssids[0].ssid = NULL;
901                         params.ssids[0].ssid_len = 0;
902                         wpa_s->prev_scan_wildcard = 1;
903                         wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
904                                 "wildcard SSID (Interleave with specific)");
905                 } else {
906                         wpa_s->prev_scan_ssid = ssid;
907                         wpa_s->prev_scan_wildcard = 0;
908                         wpa_dbg(wpa_s, MSG_DEBUG,
909                                 "Starting AP scan for specific SSID: %s",
910                                 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
911                 }
912         } else if (ssid) {
913                 /* max_ssids > 1 */
914
915                 wpa_s->prev_scan_ssid = ssid;
916                 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
917                         "the scan request");
918                 params.num_ssids++;
919         } else if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
920                    wpa_s->manual_scan_passive && params.num_ssids == 0) {
921                 wpa_dbg(wpa_s, MSG_DEBUG, "Use passive scan based on manual request");
922         } else if (wpa_s->conf->passive_scan) {
923                 wpa_dbg(wpa_s, MSG_DEBUG,
924                         "Use passive scan based on configuration");
925         } else {
926                 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
927                 params.num_ssids++;
928                 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
929                         "SSID");
930         }
931
932 ssid_list_set:
933         wpa_supplicant_optimize_freqs(wpa_s, &params);
934         extra_ie = wpa_supplicant_extra_ies(wpa_s);
935
936         if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
937             wpa_s->manual_scan_only_new) {
938                 wpa_printf(MSG_DEBUG,
939                            "Request driver to clear scan cache due to manual only_new=1 scan");
940                 params.only_new_results = 1;
941         }
942
943         if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs == NULL &&
944             wpa_s->manual_scan_freqs) {
945                 wpa_dbg(wpa_s, MSG_DEBUG, "Limit manual scan to specified channels");
946                 params.freqs = wpa_s->manual_scan_freqs;
947                 wpa_s->manual_scan_freqs = NULL;
948         }
949
950         if (params.freqs == NULL && wpa_s->next_scan_freqs) {
951                 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
952                         "generated frequency list");
953                 params.freqs = wpa_s->next_scan_freqs;
954         } else
955                 os_free(wpa_s->next_scan_freqs);
956         wpa_s->next_scan_freqs = NULL;
957         wpa_setband_scan_freqs(wpa_s, &params);
958
959         /* See if user specified frequencies. If so, scan only those. */
960         if (wpa_s->conf->freq_list && !params.freqs) {
961                 wpa_dbg(wpa_s, MSG_DEBUG,
962                         "Optimize scan based on conf->freq_list");
963                 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
964         }
965
966         /* Use current associated channel? */
967         if (wpa_s->conf->scan_cur_freq && !params.freqs) {
968                 unsigned int num = wpa_s->num_multichan_concurrent;
969
970                 params.freqs = os_calloc(num + 1, sizeof(int));
971                 if (params.freqs) {
972                         num = get_shared_radio_freqs(wpa_s, params.freqs, num);
973                         if (num > 0) {
974                                 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
975                                         "current operating channels since "
976                                         "scan_cur_freq is enabled");
977                         } else {
978                                 os_free(params.freqs);
979                                 params.freqs = NULL;
980                         }
981                 }
982         }
983
984         params.filter_ssids = wpa_supplicant_build_filter_ssids(
985                 wpa_s->conf, &params.num_filter_ssids);
986         if (extra_ie) {
987                 params.extra_ies = wpabuf_head(extra_ie);
988                 params.extra_ies_len = wpabuf_len(extra_ie);
989         }
990
991 #ifdef CONFIG_P2P
992         if (wpa_s->p2p_in_provisioning || wpa_s->p2p_in_invitation ||
993             (wpa_s->show_group_started && wpa_s->go_params)) {
994                 /*
995                  * The interface may not yet be in P2P mode, so we have to
996                  * explicitly request P2P probe to disable CCK rates.
997                  */
998                 params.p2p_probe = 1;
999         }
1000 #endif /* CONFIG_P2P */
1001
1002         if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) {
1003                 params.mac_addr_rand = 1;
1004                 if (wpa_s->mac_addr_scan) {
1005                         params.mac_addr = wpa_s->mac_addr_scan;
1006                         params.mac_addr_mask = wpa_s->mac_addr_scan + ETH_ALEN;
1007                 }
1008         }
1009
1010         if (!is_zero_ether_addr(wpa_s->next_scan_bssid)) {
1011                 struct wpa_bss *bss;
1012
1013                 params.bssid = wpa_s->next_scan_bssid;
1014                 bss = wpa_bss_get_bssid_latest(wpa_s, params.bssid);
1015                 if (bss && bss->ssid_len && params.num_ssids == 1 &&
1016                     params.ssids[0].ssid_len == 0) {
1017                         params.ssids[0].ssid = bss->ssid;
1018                         params.ssids[0].ssid_len = bss->ssid_len;
1019                         wpa_dbg(wpa_s, MSG_DEBUG,
1020                                 "Scan a previously specified BSSID " MACSTR
1021                                 " and SSID %s",
1022                                 MAC2STR(params.bssid),
1023                                 wpa_ssid_txt(bss->ssid, bss->ssid_len));
1024                 } else {
1025                         wpa_dbg(wpa_s, MSG_DEBUG,
1026                                 "Scan a previously specified BSSID " MACSTR,
1027                                 MAC2STR(params.bssid));
1028                 }
1029         }
1030
1031         scan_params = &params;
1032
1033 scan:
1034 #ifdef CONFIG_P2P
1035         /*
1036          * If the driver does not support multi-channel concurrency and a
1037          * virtual interface that shares the same radio with the wpa_s interface
1038          * is operating there may not be need to scan other channels apart from
1039          * the current operating channel on the other virtual interface. Filter
1040          * out other channels in case we are trying to find a connection for a
1041          * station interface when we are not configured to prefer station
1042          * connection and a concurrent operation is already in process.
1043          */
1044         if (wpa_s->scan_for_connection &&
1045             wpa_s->last_scan_req == NORMAL_SCAN_REQ &&
1046             !scan_params->freqs && !params.freqs &&
1047             wpas_is_p2p_prioritized(wpa_s) &&
1048             wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
1049             non_p2p_network_enabled(wpa_s)) {
1050                 unsigned int num = wpa_s->num_multichan_concurrent;
1051
1052                 params.freqs = os_calloc(num + 1, sizeof(int));
1053                 if (params.freqs) {
1054                         num = get_shared_radio_freqs(wpa_s, params.freqs, num);
1055                         if (num > 0 && num == wpa_s->num_multichan_concurrent) {
1056                                 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
1057                         } else {
1058                                 os_free(params.freqs);
1059                                 params.freqs = NULL;
1060                         }
1061                 }
1062         }
1063 #endif /* CONFIG_P2P */
1064
1065         ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
1066
1067         if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs &&
1068             !wpa_s->manual_scan_freqs) {
1069                 /* Restore manual_scan_freqs for the next attempt */
1070                 wpa_s->manual_scan_freqs = params.freqs;
1071                 params.freqs = NULL;
1072         }
1073
1074         wpabuf_free(extra_ie);
1075         os_free(params.freqs);
1076         os_free(params.filter_ssids);
1077
1078         if (ret) {
1079                 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
1080                 if (wpa_s->scan_prev_wpa_state != wpa_s->wpa_state)
1081                         wpa_supplicant_set_state(wpa_s,
1082                                                  wpa_s->scan_prev_wpa_state);
1083                 /* Restore scan_req since we will try to scan again */
1084                 wpa_s->scan_req = wpa_s->last_scan_req;
1085                 wpa_supplicant_req_scan(wpa_s, 1, 0);
1086         } else {
1087                 wpa_s->scan_for_connection = 0;
1088 #ifdef CONFIG_INTERWORKING
1089                 wpa_s->interworking_fast_assoc_tried = 0;
1090 #endif /* CONFIG_INTERWORKING */
1091                 if (params.bssid)
1092                         os_memset(wpa_s->next_scan_bssid, 0, ETH_ALEN);
1093         }
1094 }
1095
1096
1097 void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
1098 {
1099         struct os_reltime remaining, new_int;
1100         int cancelled;
1101
1102         cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
1103                                              &remaining);
1104
1105         new_int.sec = sec;
1106         new_int.usec = 0;
1107         if (cancelled && os_reltime_before(&remaining, &new_int)) {
1108                 new_int.sec = remaining.sec;
1109                 new_int.usec = remaining.usec;
1110         }
1111
1112         if (cancelled) {
1113                 eloop_register_timeout(new_int.sec, new_int.usec,
1114                                        wpa_supplicant_scan, wpa_s, NULL);
1115         }
1116         wpa_s->scan_interval = sec;
1117 }
1118
1119
1120 /**
1121  * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
1122  * @wpa_s: Pointer to wpa_supplicant data
1123  * @sec: Number of seconds after which to scan
1124  * @usec: Number of microseconds after which to scan
1125  *
1126  * This function is used to schedule a scan for neighboring access points after
1127  * the specified time.
1128  */
1129 void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
1130 {
1131         int res;
1132
1133         if (wpa_s->p2p_mgmt) {
1134                 wpa_dbg(wpa_s, MSG_DEBUG,
1135                         "Ignore scan request (%d.%06d sec) on p2p_mgmt interface",
1136                         sec, usec);
1137                 return;
1138         }
1139
1140         res = eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s,
1141                                     NULL);
1142         if (res == 1) {
1143                 wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d.%06d sec",
1144                         sec, usec);
1145         } else if (res == 0) {
1146                 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore new scan request for %d.%06d sec since an earlier request is scheduled to trigger sooner",
1147                         sec, usec);
1148         } else {
1149                 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d.%06d sec",
1150                         sec, usec);
1151                 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
1152         }
1153 }
1154
1155
1156 /**
1157  * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
1158  * @wpa_s: Pointer to wpa_supplicant data
1159  * @sec: Number of seconds after which to scan
1160  * @usec: Number of microseconds after which to scan
1161  * Returns: 0 on success or -1 otherwise
1162  *
1163  * This function is used to schedule periodic scans for neighboring
1164  * access points after the specified time.
1165  */
1166 int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
1167                                       int sec, int usec)
1168 {
1169         if (!wpa_s->sched_scan_supported)
1170                 return -1;
1171
1172         eloop_register_timeout(sec, usec,
1173                                wpa_supplicant_delayed_sched_scan_timeout,
1174                                wpa_s, NULL);
1175
1176         return 0;
1177 }
1178
1179
1180 /**
1181  * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
1182  * @wpa_s: Pointer to wpa_supplicant data
1183  * Returns: 0 is sched_scan was started or -1 otherwise
1184  *
1185  * This function is used to schedule periodic scans for neighboring
1186  * access points repeating the scan continuously.
1187  */
1188 int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
1189 {
1190         struct wpa_driver_scan_params params;
1191         struct wpa_driver_scan_params *scan_params;
1192         enum wpa_states prev_state;
1193         struct wpa_ssid *ssid = NULL;
1194         struct wpabuf *extra_ie = NULL;
1195         int ret;
1196         unsigned int max_sched_scan_ssids;
1197         int wildcard = 0;
1198         int need_ssids;
1199         struct sched_scan_plan scan_plan;
1200
1201         if (!wpa_s->sched_scan_supported)
1202                 return -1;
1203
1204         if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
1205                 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
1206         else
1207                 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
1208         if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
1209                 return -1;
1210
1211         if (wpa_s->sched_scanning) {
1212                 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
1213                 return 0;
1214         }
1215
1216         need_ssids = 0;
1217         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1218                 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
1219                         /* Use wildcard SSID to find this network */
1220                         wildcard = 1;
1221                 } else if (!wpas_network_disabled(wpa_s, ssid) &&
1222                            ssid->ssid_len)
1223                         need_ssids++;
1224
1225 #ifdef CONFIG_WPS
1226                 if (!wpas_network_disabled(wpa_s, ssid) &&
1227                     ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1228                         /*
1229                          * Normal scan is more reliable and faster for WPS
1230                          * operations and since these are for short periods of
1231                          * time, the benefit of trying to use sched_scan would
1232                          * be limited.
1233                          */
1234                         wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1235                                 "sched_scan for WPS");
1236                         return -1;
1237                 }
1238 #endif /* CONFIG_WPS */
1239         }
1240         if (wildcard)
1241                 need_ssids++;
1242
1243         if (wpa_s->normal_scans < 3 &&
1244             (need_ssids <= wpa_s->max_scan_ssids ||
1245              wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
1246                 /*
1247                  * When normal scan can speed up operations, use that for the
1248                  * first operations before starting the sched_scan to allow
1249                  * user space sleep more. We do this only if the normal scan
1250                  * has functionality that is suitable for this or if the
1251                  * sched_scan does not have better support for multiple SSIDs.
1252                  */
1253                 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1254                         "sched_scan for initial scans (normal_scans=%d)",
1255                         wpa_s->normal_scans);
1256                 return -1;
1257         }
1258
1259         os_memset(&params, 0, sizeof(params));
1260
1261         /* If we can't allocate space for the filters, we just don't filter */
1262         params.filter_ssids = os_calloc(wpa_s->max_match_sets,
1263                                         sizeof(struct wpa_driver_scan_filter));
1264
1265         prev_state = wpa_s->wpa_state;
1266         if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1267             wpa_s->wpa_state == WPA_INACTIVE)
1268                 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1269
1270         if (wpa_s->autoscan_params != NULL) {
1271                 scan_params = wpa_s->autoscan_params;
1272                 goto scan;
1273         }
1274
1275         /* Find the starting point from which to continue scanning */
1276         ssid = wpa_s->conf->ssid;
1277         if (wpa_s->prev_sched_ssid) {
1278                 while (ssid) {
1279                         if (ssid == wpa_s->prev_sched_ssid) {
1280                                 ssid = ssid->next;
1281                                 break;
1282                         }
1283                         ssid = ssid->next;
1284                 }
1285         }
1286
1287         if (!ssid || !wpa_s->prev_sched_ssid) {
1288                 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
1289                 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1290                 wpa_s->first_sched_scan = 1;
1291                 ssid = wpa_s->conf->ssid;
1292                 wpa_s->prev_sched_ssid = ssid;
1293         }
1294
1295         if (wildcard) {
1296                 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1297                 params.num_ssids++;
1298         }
1299
1300         while (ssid) {
1301                 if (wpas_network_disabled(wpa_s, ssid))
1302                         goto next;
1303
1304                 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1305                     params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1306                         wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1307                                 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1308                         os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1309                                   ssid->ssid, ssid->ssid_len);
1310                         params.filter_ssids[params.num_filter_ssids].ssid_len =
1311                                 ssid->ssid_len;
1312                         params.num_filter_ssids++;
1313                 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1314                 {
1315                         wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1316                                 "filter for sched_scan - drop filter");
1317                         os_free(params.filter_ssids);
1318                         params.filter_ssids = NULL;
1319                         params.num_filter_ssids = 0;
1320                 }
1321
1322                 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1323                         if (params.num_ssids == max_sched_scan_ssids)
1324                                 break; /* only room for broadcast SSID */
1325                         wpa_dbg(wpa_s, MSG_DEBUG,
1326                                 "add to active scan ssid: %s",
1327                                 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1328                         params.ssids[params.num_ssids].ssid =
1329                                 ssid->ssid;
1330                         params.ssids[params.num_ssids].ssid_len =
1331                                 ssid->ssid_len;
1332                         params.num_ssids++;
1333                         if (params.num_ssids >= max_sched_scan_ssids) {
1334                                 wpa_s->prev_sched_ssid = ssid;
1335                                 do {
1336                                         ssid = ssid->next;
1337                                 } while (ssid &&
1338                                          (wpas_network_disabled(wpa_s, ssid) ||
1339                                           !ssid->scan_ssid));
1340                                 break;
1341                         }
1342                 }
1343
1344         next:
1345                 wpa_s->prev_sched_ssid = ssid;
1346                 ssid = ssid->next;
1347         }
1348
1349         if (params.num_filter_ssids == 0) {
1350                 os_free(params.filter_ssids);
1351                 params.filter_ssids = NULL;
1352         }
1353
1354         extra_ie = wpa_supplicant_extra_ies(wpa_s);
1355         if (extra_ie) {
1356                 params.extra_ies = wpabuf_head(extra_ie);
1357                 params.extra_ies_len = wpabuf_len(extra_ie);
1358         }
1359
1360         if (wpa_s->conf->filter_rssi)
1361                 params.filter_rssi = wpa_s->conf->filter_rssi;
1362
1363         /* See if user specified frequencies. If so, scan only those. */
1364         if (wpa_s->conf->freq_list && !params.freqs) {
1365                 wpa_dbg(wpa_s, MSG_DEBUG,
1366                         "Optimize scan based on conf->freq_list");
1367                 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1368         }
1369
1370         scan_params = &params;
1371
1372 scan:
1373         wpa_s->sched_scan_timed_out = 0;
1374
1375         /*
1376          * We cannot support multiple scan plans if the scan request includes
1377          * too many SSID's, so in this case use only the last scan plan and make
1378          * it run infinitely. It will be stopped by the timeout.
1379          */
1380         if (wpa_s->sched_scan_plans_num == 1 ||
1381             (wpa_s->sched_scan_plans_num && !ssid && wpa_s->first_sched_scan)) {
1382                 params.sched_scan_plans = wpa_s->sched_scan_plans;
1383                 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
1384         } else if (wpa_s->sched_scan_plans_num > 1) {
1385                 wpa_dbg(wpa_s, MSG_DEBUG,
1386                         "Too many SSIDs. Default to using single scheduled_scan plan");
1387                 params.sched_scan_plans =
1388                         &wpa_s->sched_scan_plans[wpa_s->sched_scan_plans_num -
1389                                                  1];
1390                 params.sched_scan_plans_num = 1;
1391         } else {
1392                 if (wpa_s->conf->sched_scan_interval)
1393                         scan_plan.interval = wpa_s->conf->sched_scan_interval;
1394                 else
1395                         scan_plan.interval = 10;
1396
1397                 if (scan_plan.interval > wpa_s->max_sched_scan_plan_interval) {
1398                         wpa_printf(MSG_WARNING,
1399                                    "Scan interval too long(%u), use the maximum allowed(%u)",
1400                                    scan_plan.interval,
1401                                    wpa_s->max_sched_scan_plan_interval);
1402                         scan_plan.interval =
1403                                 wpa_s->max_sched_scan_plan_interval;
1404                 }
1405
1406                 scan_plan.iterations = 0;
1407                 params.sched_scan_plans = &scan_plan;
1408                 params.sched_scan_plans_num = 1;
1409         }
1410
1411         if (ssid || !wpa_s->first_sched_scan) {
1412                 wpa_dbg(wpa_s, MSG_DEBUG,
1413                         "Starting sched scan: interval %u timeout %d",
1414                         params.sched_scan_plans[0].interval,
1415                         wpa_s->sched_scan_timeout);
1416         } else {
1417                 wpa_dbg(wpa_s, MSG_DEBUG, "Starting sched scan (no timeout)");
1418         }
1419
1420         wpa_setband_scan_freqs(wpa_s, scan_params);
1421
1422         if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCHED_SCAN) {
1423                 params.mac_addr_rand = 1;
1424                 if (wpa_s->mac_addr_sched_scan) {
1425                         params.mac_addr = wpa_s->mac_addr_sched_scan;
1426                         params.mac_addr_mask = wpa_s->mac_addr_sched_scan +
1427                                 ETH_ALEN;
1428                 }
1429         }
1430
1431         ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params);
1432         wpabuf_free(extra_ie);
1433         os_free(params.filter_ssids);
1434         if (ret) {
1435                 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1436                 if (prev_state != wpa_s->wpa_state)
1437                         wpa_supplicant_set_state(wpa_s, prev_state);
1438                 return ret;
1439         }
1440
1441         /* If we have more SSIDs to scan, add a timeout so we scan them too */
1442         if (ssid || !wpa_s->first_sched_scan) {
1443                 wpa_s->sched_scan_timed_out = 0;
1444                 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1445                                        wpa_supplicant_sched_scan_timeout,
1446                                        wpa_s, NULL);
1447                 wpa_s->first_sched_scan = 0;
1448                 wpa_s->sched_scan_timeout /= 2;
1449                 params.sched_scan_plans[0].interval *= 2;
1450                 if ((unsigned int) wpa_s->sched_scan_timeout <
1451                     params.sched_scan_plans[0].interval ||
1452                     params.sched_scan_plans[0].interval >
1453                     wpa_s->max_sched_scan_plan_interval) {
1454                         params.sched_scan_plans[0].interval = 10;
1455                         wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1456                 }
1457         }
1458
1459         /* If there is no more ssids, start next time from the beginning */
1460         if (!ssid)
1461                 wpa_s->prev_sched_ssid = NULL;
1462
1463         return 0;
1464 }
1465
1466
1467 /**
1468  * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1469  * @wpa_s: Pointer to wpa_supplicant data
1470  *
1471  * This function is used to cancel a scan request scheduled with
1472  * wpa_supplicant_req_scan().
1473  */
1474 void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1475 {
1476         wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1477         eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
1478 }
1479
1480
1481 /**
1482  * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
1483  * @wpa_s: Pointer to wpa_supplicant data
1484  *
1485  * This function is used to stop a delayed scheduled scan.
1486  */
1487 void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
1488 {
1489         if (!wpa_s->sched_scan_supported)
1490                 return;
1491
1492         wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
1493         eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
1494                              wpa_s, NULL);
1495 }
1496
1497
1498 /**
1499  * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1500  * @wpa_s: Pointer to wpa_supplicant data
1501  *
1502  * This function is used to stop a periodic scheduled scan.
1503  */
1504 void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1505 {
1506         if (!wpa_s->sched_scanning)
1507                 return;
1508
1509         wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1510         eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1511         wpa_supplicant_stop_sched_scan(wpa_s);
1512 }
1513
1514
1515 /**
1516  * wpa_supplicant_notify_scanning - Indicate possible scan state change
1517  * @wpa_s: Pointer to wpa_supplicant data
1518  * @scanning: Whether scanning is currently in progress
1519  *
1520  * This function is to generate scanning notifycations. It is called whenever
1521  * there may have been a change in scanning (scan started, completed, stopped).
1522  * wpas_notify_scanning() is called whenever the scanning state changed from the
1523  * previously notified state.
1524  */
1525 void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1526                                     int scanning)
1527 {
1528         if (wpa_s->scanning != scanning) {
1529                 wpa_s->scanning = scanning;
1530                 wpas_notify_scanning(wpa_s);
1531         }
1532 }
1533
1534
1535 static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1536 {
1537         int rate = 0;
1538         const u8 *ie;
1539         int i;
1540
1541         ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1542         for (i = 0; ie && i < ie[1]; i++) {
1543                 if ((ie[i + 2] & 0x7f) > rate)
1544                         rate = ie[i + 2] & 0x7f;
1545         }
1546
1547         ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1548         for (i = 0; ie && i < ie[1]; i++) {
1549                 if ((ie[i + 2] & 0x7f) > rate)
1550                         rate = ie[i + 2] & 0x7f;
1551         }
1552
1553         return rate;
1554 }
1555
1556
1557 /**
1558  * wpa_scan_get_ie - Fetch a specified information element from a scan result
1559  * @res: Scan result entry
1560  * @ie: Information element identitifier (WLAN_EID_*)
1561  * Returns: Pointer to the information element (id field) or %NULL if not found
1562  *
1563  * This function returns the first matching information element in the scan
1564  * result.
1565  */
1566 const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1567 {
1568         return get_ie((const u8 *) (res + 1), res->ie_len, ie);
1569 }
1570
1571
1572 /**
1573  * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1574  * @res: Scan result entry
1575  * @vendor_type: Vendor type (four octets starting the IE payload)
1576  * Returns: Pointer to the information element (id field) or %NULL if not found
1577  *
1578  * This function returns the first matching information element in the scan
1579  * result.
1580  */
1581 const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1582                                   u32 vendor_type)
1583 {
1584         const u8 *end, *pos;
1585
1586         pos = (const u8 *) (res + 1);
1587         end = pos + res->ie_len;
1588
1589         while (end - pos > 1) {
1590                 if (2 + pos[1] > end - pos)
1591                         break;
1592                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1593                     vendor_type == WPA_GET_BE32(&pos[2]))
1594                         return pos;
1595                 pos += 2 + pos[1];
1596         }
1597
1598         return NULL;
1599 }
1600
1601
1602 /**
1603  * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
1604  * @res: Scan result entry
1605  * @vendor_type: Vendor type (four octets starting the IE payload)
1606  * Returns: Pointer to the information element (id field) or %NULL if not found
1607  *
1608  * This function returns the first matching information element in the scan
1609  * result.
1610  *
1611  * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
1612  * from Beacon frames instead of either Beacon or Probe Response frames.
1613  */
1614 const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
1615                                          u32 vendor_type)
1616 {
1617         const u8 *end, *pos;
1618
1619         if (res->beacon_ie_len == 0)
1620                 return NULL;
1621
1622         pos = (const u8 *) (res + 1);
1623         pos += res->ie_len;
1624         end = pos + res->beacon_ie_len;
1625
1626         while (end - pos > 1) {
1627                 if (2 + pos[1] > end - pos)
1628                         break;
1629                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1630                     vendor_type == WPA_GET_BE32(&pos[2]))
1631                         return pos;
1632                 pos += 2 + pos[1];
1633         }
1634
1635         return NULL;
1636 }
1637
1638
1639 /**
1640  * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
1641  * @res: Scan result entry
1642  * @vendor_type: Vendor type (four octets starting the IE payload)
1643  * Returns: Pointer to the information element payload or %NULL if not found
1644  *
1645  * This function returns concatenated payload of possibly fragmented vendor
1646  * specific information elements in the scan result. The caller is responsible
1647  * for freeing the returned buffer.
1648  */
1649 struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1650                                              u32 vendor_type)
1651 {
1652         struct wpabuf *buf;
1653         const u8 *end, *pos;
1654
1655         buf = wpabuf_alloc(res->ie_len);
1656         if (buf == NULL)
1657                 return NULL;
1658
1659         pos = (const u8 *) (res + 1);
1660         end = pos + res->ie_len;
1661
1662         while (end - pos > 1) {
1663                 if (2 + pos[1] > end - pos)
1664                         break;
1665                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1666                     vendor_type == WPA_GET_BE32(&pos[2]))
1667                         wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1668                 pos += 2 + pos[1];
1669         }
1670
1671         if (wpabuf_len(buf) == 0) {
1672                 wpabuf_free(buf);
1673                 buf = NULL;
1674         }
1675
1676         return buf;
1677 }
1678
1679
1680 /*
1681  * Channels with a great SNR can operate at full rate. What is a great SNR?
1682  * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1683  * rule of thumb is that any SNR above 20 is good." This one
1684  * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1685  * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1686  * conservative value.
1687  */
1688 #define GREAT_SNR 30
1689
1690 #define IS_5GHZ(n) (n > 4000)
1691
1692 /* Compare function for sorting scan results. Return >0 if @b is considered
1693  * better. */
1694 static int wpa_scan_result_compar(const void *a, const void *b)
1695 {
1696 #define MIN(a,b) a < b ? a : b
1697         struct wpa_scan_res **_wa = (void *) a;
1698         struct wpa_scan_res **_wb = (void *) b;
1699         struct wpa_scan_res *wa = *_wa;
1700         struct wpa_scan_res *wb = *_wb;
1701         int wpa_a, wpa_b;
1702         int snr_a, snr_b, snr_a_full, snr_b_full;
1703
1704         /* WPA/WPA2 support preferred */
1705         wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1706                 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1707         wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1708                 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1709
1710         if (wpa_b && !wpa_a)
1711                 return 1;
1712         if (!wpa_b && wpa_a)
1713                 return -1;
1714
1715         /* privacy support preferred */
1716         if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1717             (wb->caps & IEEE80211_CAP_PRIVACY))
1718                 return 1;
1719         if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1720             (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1721                 return -1;
1722
1723         if (wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) {
1724                 snr_a_full = wa->snr;
1725                 snr_a = MIN(wa->snr, GREAT_SNR);
1726                 snr_b_full = wb->snr;
1727                 snr_b = MIN(wb->snr, GREAT_SNR);
1728         } else {
1729                 /* Level is not in dBm, so we can't calculate
1730                  * SNR. Just use raw level (units unknown). */
1731                 snr_a = snr_a_full = wa->level;
1732                 snr_b = snr_b_full = wb->level;
1733         }
1734
1735         /* if SNR is close, decide by max rate or frequency band */
1736         if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
1737             (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
1738                 if (wa->est_throughput != wb->est_throughput)
1739                         return wb->est_throughput - wa->est_throughput;
1740                 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1741                         return IS_5GHZ(wa->freq) ? -1 : 1;
1742         }
1743
1744         /* all things being equal, use SNR; if SNRs are
1745          * identical, use quality values since some drivers may only report
1746          * that value and leave the signal level zero */
1747         if (snr_b_full == snr_a_full)
1748                 return wb->qual - wa->qual;
1749         return snr_b_full - snr_a_full;
1750 #undef MIN
1751 }
1752
1753
1754 #ifdef CONFIG_WPS
1755 /* Compare function for sorting scan results when searching a WPS AP for
1756  * provisioning. Return >0 if @b is considered better. */
1757 static int wpa_scan_result_wps_compar(const void *a, const void *b)
1758 {
1759         struct wpa_scan_res **_wa = (void *) a;
1760         struct wpa_scan_res **_wb = (void *) b;
1761         struct wpa_scan_res *wa = *_wa;
1762         struct wpa_scan_res *wb = *_wb;
1763         int uses_wps_a, uses_wps_b;
1764         struct wpabuf *wps_a, *wps_b;
1765         int res;
1766
1767         /* Optimization - check WPS IE existence before allocated memory and
1768          * doing full reassembly. */
1769         uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1770         uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1771         if (uses_wps_a && !uses_wps_b)
1772                 return -1;
1773         if (!uses_wps_a && uses_wps_b)
1774                 return 1;
1775
1776         if (uses_wps_a && uses_wps_b) {
1777                 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1778                 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1779                 res = wps_ap_priority_compar(wps_a, wps_b);
1780                 wpabuf_free(wps_a);
1781                 wpabuf_free(wps_b);
1782                 if (res)
1783                         return res;
1784         }
1785
1786         /*
1787          * Do not use current AP security policy as a sorting criteria during
1788          * WPS provisioning step since the AP may get reconfigured at the
1789          * completion of provisioning.
1790          */
1791
1792         /* all things being equal, use signal level; if signal levels are
1793          * identical, use quality values since some drivers may only report
1794          * that value and leave the signal level zero */
1795         if (wb->level == wa->level)
1796                 return wb->qual - wa->qual;
1797         return wb->level - wa->level;
1798 }
1799 #endif /* CONFIG_WPS */
1800
1801
1802 static void dump_scan_res(struct wpa_scan_results *scan_res)
1803 {
1804 #ifndef CONFIG_NO_STDOUT_DEBUG
1805         size_t i;
1806
1807         if (scan_res->res == NULL || scan_res->num == 0)
1808                 return;
1809
1810         wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1811
1812         for (i = 0; i < scan_res->num; i++) {
1813                 struct wpa_scan_res *r = scan_res->res[i];
1814                 u8 *pos;
1815                 if (r->flags & WPA_SCAN_LEVEL_DBM) {
1816                         int noise_valid = !(r->flags & WPA_SCAN_NOISE_INVALID);
1817
1818                         wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1819                                    "noise=%d%s level=%d snr=%d%s flags=0x%x age=%u est=%u",
1820                                    MAC2STR(r->bssid), r->freq, r->qual,
1821                                    r->noise, noise_valid ? "" : "~", r->level,
1822                                    r->snr, r->snr >= GREAT_SNR ? "*" : "",
1823                                    r->flags,
1824                                    r->age, r->est_throughput);
1825                 } else {
1826                         wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1827                                    "noise=%d level=%d flags=0x%x age=%u est=%u",
1828                                    MAC2STR(r->bssid), r->freq, r->qual,
1829                                    r->noise, r->level, r->flags, r->age,
1830                                    r->est_throughput);
1831                 }
1832                 pos = (u8 *) (r + 1);
1833                 if (r->ie_len)
1834                         wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
1835                 pos += r->ie_len;
1836                 if (r->beacon_ie_len)
1837                         wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
1838                                     pos, r->beacon_ie_len);
1839         }
1840 #endif /* CONFIG_NO_STDOUT_DEBUG */
1841 }
1842
1843
1844 /**
1845  * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
1846  * @wpa_s: Pointer to wpa_supplicant data
1847  * @bssid: BSSID to check
1848  * Returns: 0 if the BSSID is filtered or 1 if not
1849  *
1850  * This function is used to filter out specific BSSIDs from scan reslts mainly
1851  * for testing purposes (SET bssid_filter ctrl_iface command).
1852  */
1853 int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
1854                                       const u8 *bssid)
1855 {
1856         size_t i;
1857
1858         if (wpa_s->bssid_filter == NULL)
1859                 return 1;
1860
1861         for (i = 0; i < wpa_s->bssid_filter_count; i++) {
1862                 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
1863                               ETH_ALEN) == 0)
1864                         return 1;
1865         }
1866
1867         return 0;
1868 }
1869
1870
1871 static void filter_scan_res(struct wpa_supplicant *wpa_s,
1872                             struct wpa_scan_results *res)
1873 {
1874         size_t i, j;
1875
1876         if (wpa_s->bssid_filter == NULL)
1877                 return;
1878
1879         for (i = 0, j = 0; i < res->num; i++) {
1880                 if (wpa_supplicant_filter_bssid_match(wpa_s,
1881                                                       res->res[i]->bssid)) {
1882                         res->res[j++] = res->res[i];
1883                 } else {
1884                         os_free(res->res[i]);
1885                         res->res[i] = NULL;
1886                 }
1887         }
1888
1889         if (res->num != j) {
1890                 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
1891                            (int) (res->num - j));
1892                 res->num = j;
1893         }
1894 }
1895
1896
1897 /*
1898  * Noise floor values to use when we have signal strength
1899  * measurements, but no noise floor measurements. These values were
1900  * measured in an office environment with many APs.
1901  */
1902 #define DEFAULT_NOISE_FLOOR_2GHZ (-89)
1903 #define DEFAULT_NOISE_FLOOR_5GHZ (-92)
1904
1905 static void scan_snr(struct wpa_scan_res *res)
1906 {
1907         if (res->flags & WPA_SCAN_NOISE_INVALID) {
1908                 res->noise = IS_5GHZ(res->freq) ?
1909                         DEFAULT_NOISE_FLOOR_5GHZ :
1910                         DEFAULT_NOISE_FLOOR_2GHZ;
1911         }
1912
1913         if (res->flags & WPA_SCAN_LEVEL_DBM) {
1914                 res->snr = res->level - res->noise;
1915         } else {
1916                 /* Level is not in dBm, so we can't calculate
1917                  * SNR. Just use raw level (units unknown). */
1918                 res->snr = res->level;
1919         }
1920 }
1921
1922
1923 static unsigned int max_ht20_rate(int snr)
1924 {
1925         if (snr < 6)
1926                 return 6500; /* HT20 MCS0 */
1927         if (snr < 8)
1928                 return 13000; /* HT20 MCS1 */
1929         if (snr < 13)
1930                 return 19500; /* HT20 MCS2 */
1931         if (snr < 17)
1932                 return 26000; /* HT20 MCS3 */
1933         if (snr < 20)
1934                 return 39000; /* HT20 MCS4 */
1935         if (snr < 23)
1936                 return 52000; /* HT20 MCS5 */
1937         if (snr < 24)
1938                 return 58500; /* HT20 MCS6 */
1939         return 65000; /* HT20 MCS7 */
1940 }
1941
1942
1943 static unsigned int max_ht40_rate(int snr)
1944 {
1945         if (snr < 3)
1946                 return 13500; /* HT40 MCS0 */
1947         if (snr < 6)
1948                 return 27000; /* HT40 MCS1 */
1949         if (snr < 10)
1950                 return 40500; /* HT40 MCS2 */
1951         if (snr < 15)
1952                 return 54000; /* HT40 MCS3 */
1953         if (snr < 17)
1954                 return 81000; /* HT40 MCS4 */
1955         if (snr < 22)
1956                 return 108000; /* HT40 MCS5 */
1957         if (snr < 24)
1958                 return 121500; /* HT40 MCS6 */
1959         return 135000; /* HT40 MCS7 */
1960 }
1961
1962
1963 static unsigned int max_vht80_rate(int snr)
1964 {
1965         if (snr < 1)
1966                 return 0;
1967         if (snr < 2)
1968                 return 29300; /* VHT80 MCS0 */
1969         if (snr < 5)
1970                 return 58500; /* VHT80 MCS1 */
1971         if (snr < 9)
1972                 return 87800; /* VHT80 MCS2 */
1973         if (snr < 11)
1974                 return 117000; /* VHT80 MCS3 */
1975         if (snr < 15)
1976                 return 175500; /* VHT80 MCS4 */
1977         if (snr < 16)
1978                 return 234000; /* VHT80 MCS5 */
1979         if (snr < 18)
1980                 return 263300; /* VHT80 MCS6 */
1981         if (snr < 20)
1982                 return 292500; /* VHT80 MCS7 */
1983         if (snr < 22)
1984                 return 351000; /* VHT80 MCS8 */
1985         return 390000; /* VHT80 MCS9 */
1986 }
1987
1988
1989 static void scan_est_throughput(struct wpa_supplicant *wpa_s,
1990                                 struct wpa_scan_res *res)
1991 {
1992         enum local_hw_capab capab = wpa_s->hw_capab;
1993         int rate; /* max legacy rate in 500 kb/s units */
1994         const u8 *ie;
1995         unsigned int est, tmp;
1996         int snr = res->snr;
1997
1998         if (res->est_throughput)
1999                 return;
2000
2001         /* Get maximum legacy rate */
2002         rate = wpa_scan_get_max_rate(res);
2003
2004         /* Limit based on estimated SNR */
2005         if (rate > 1 * 2 && snr < 1)
2006                 rate = 1 * 2;
2007         else if (rate > 2 * 2 && snr < 4)
2008                 rate = 2 * 2;
2009         else if (rate > 6 * 2 && snr < 5)
2010                 rate = 6 * 2;
2011         else if (rate > 9 * 2 && snr < 6)
2012                 rate = 9 * 2;
2013         else if (rate > 12 * 2 && snr < 7)
2014                 rate = 12 * 2;
2015         else if (rate > 18 * 2 && snr < 10)
2016                 rate = 18 * 2;
2017         else if (rate > 24 * 2 && snr < 11)
2018                 rate = 24 * 2;
2019         else if (rate > 36 * 2 && snr < 15)
2020                 rate = 36 * 2;
2021         else if (rate > 48 * 2 && snr < 19)
2022                 rate = 48 * 2;
2023         else if (rate > 54 * 2 && snr < 21)
2024                 rate = 54 * 2;
2025         est = rate * 500;
2026
2027         if (capab == CAPAB_HT || capab == CAPAB_HT40 || capab == CAPAB_VHT) {
2028                 ie = wpa_scan_get_ie(res, WLAN_EID_HT_CAP);
2029                 if (ie) {
2030                         tmp = max_ht20_rate(snr);
2031                         if (tmp > est)
2032                                 est = tmp;
2033                 }
2034         }
2035
2036         if (capab == CAPAB_HT40 || capab == CAPAB_VHT) {
2037                 ie = wpa_scan_get_ie(res, WLAN_EID_HT_OPERATION);
2038                 if (ie && ie[1] >= 2 &&
2039                     (ie[3] & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
2040                         tmp = max_ht40_rate(snr);
2041                         if (tmp > est)
2042                                 est = tmp;
2043                 }
2044         }
2045
2046         if (capab == CAPAB_VHT) {
2047                 /* Use +1 to assume VHT is always faster than HT */
2048                 ie = wpa_scan_get_ie(res, WLAN_EID_VHT_CAP);
2049                 if (ie) {
2050                         tmp = max_ht20_rate(snr) + 1;
2051                         if (tmp > est)
2052                                 est = tmp;
2053
2054                         ie = wpa_scan_get_ie(res, WLAN_EID_HT_OPERATION);
2055                         if (ie && ie[1] >= 2 &&
2056                             (ie[3] &
2057                              HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
2058                                 tmp = max_ht40_rate(snr) + 1;
2059                                 if (tmp > est)
2060                                         est = tmp;
2061                         }
2062
2063                         ie = wpa_scan_get_ie(res, WLAN_EID_VHT_OPERATION);
2064                         if (ie && ie[1] >= 1 &&
2065                             (ie[2] & VHT_OPMODE_CHANNEL_WIDTH_MASK)) {
2066                                 tmp = max_vht80_rate(snr) + 1;
2067                                 if (tmp > est)
2068                                         est = tmp;
2069                         }
2070                 }
2071         }
2072
2073         /* TODO: channel utilization and AP load (e.g., from AP Beacon) */
2074
2075         res->est_throughput = est;
2076 }
2077
2078
2079 /**
2080  * wpa_supplicant_get_scan_results - Get scan results
2081  * @wpa_s: Pointer to wpa_supplicant data
2082  * @info: Information about what was scanned or %NULL if not available
2083  * @new_scan: Whether a new scan was performed
2084  * Returns: Scan results, %NULL on failure
2085  *
2086  * This function request the current scan results from the driver and updates
2087  * the local BSS list wpa_s->bss. The caller is responsible for freeing the
2088  * results with wpa_scan_results_free().
2089  */
2090 struct wpa_scan_results *
2091 wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
2092                                 struct scan_info *info, int new_scan)
2093 {
2094         struct wpa_scan_results *scan_res;
2095         size_t i;
2096         int (*compar)(const void *, const void *) = wpa_scan_result_compar;
2097
2098         scan_res = wpa_drv_get_scan_results2(wpa_s);
2099         if (scan_res == NULL) {
2100                 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
2101                 return NULL;
2102         }
2103         if (scan_res->fetch_time.sec == 0) {
2104                 /*
2105                  * Make sure we have a valid timestamp if the driver wrapper
2106                  * does not set this.
2107                  */
2108                 os_get_reltime(&scan_res->fetch_time);
2109         }
2110         filter_scan_res(wpa_s, scan_res);
2111
2112         for (i = 0; i < scan_res->num; i++) {
2113                 struct wpa_scan_res *scan_res_item = scan_res->res[i];
2114
2115                 scan_snr(scan_res_item);
2116                 scan_est_throughput(wpa_s, scan_res_item);
2117         }
2118
2119 #ifdef CONFIG_WPS
2120         if (wpas_wps_searching(wpa_s)) {
2121                 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
2122                         "provisioning rules");
2123                 compar = wpa_scan_result_wps_compar;
2124         }
2125 #endif /* CONFIG_WPS */
2126
2127         qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
2128               compar);
2129         dump_scan_res(scan_res);
2130
2131         wpa_bss_update_start(wpa_s);
2132         for (i = 0; i < scan_res->num; i++)
2133                 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
2134                                         &scan_res->fetch_time);
2135         wpa_bss_update_end(wpa_s, info, new_scan);
2136
2137         return scan_res;
2138 }
2139
2140
2141 /**
2142  * wpa_supplicant_update_scan_results - Update scan results from the driver
2143  * @wpa_s: Pointer to wpa_supplicant data
2144  * Returns: 0 on success, -1 on failure
2145  *
2146  * This function updates the BSS table within wpa_supplicant based on the
2147  * currently available scan results from the driver without requesting a new
2148  * scan. This is used in cases where the driver indicates an association
2149  * (including roaming within ESS) and wpa_supplicant does not yet have the
2150  * needed information to complete the connection (e.g., to perform validation
2151  * steps in 4-way handshake).
2152  */
2153 int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
2154 {
2155         struct wpa_scan_results *scan_res;
2156         scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
2157         if (scan_res == NULL)
2158                 return -1;
2159         wpa_scan_results_free(scan_res);
2160
2161         return 0;
2162 }
2163
2164
2165 /**
2166  * scan_only_handler - Reports scan results
2167  */
2168 void scan_only_handler(struct wpa_supplicant *wpa_s,
2169                        struct wpa_scan_results *scan_res)
2170 {
2171         wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
2172         if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
2173             wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
2174                 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
2175                              wpa_s->manual_scan_id);
2176                 wpa_s->manual_scan_use_id = 0;
2177         } else {
2178                 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
2179         }
2180         wpas_notify_scan_results(wpa_s);
2181         wpas_notify_scan_done(wpa_s, 1);
2182         if (wpa_s->scan_work) {
2183                 struct wpa_radio_work *work = wpa_s->scan_work;
2184                 wpa_s->scan_work = NULL;
2185                 radio_work_done(work);
2186         }
2187
2188         if (wpa_s->wpa_state == WPA_SCANNING)
2189                 wpa_supplicant_set_state(wpa_s, wpa_s->scan_prev_wpa_state);
2190 }
2191
2192
2193 int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
2194 {
2195         return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
2196 }
2197
2198
2199 struct wpa_driver_scan_params *
2200 wpa_scan_clone_params(const struct wpa_driver_scan_params *src)
2201 {
2202         struct wpa_driver_scan_params *params;
2203         size_t i;
2204         u8 *n;
2205
2206         params = os_zalloc(sizeof(*params));
2207         if (params == NULL)
2208                 return NULL;
2209
2210         for (i = 0; i < src->num_ssids; i++) {
2211                 if (src->ssids[i].ssid) {
2212                         n = os_malloc(src->ssids[i].ssid_len);
2213                         if (n == NULL)
2214                                 goto failed;
2215                         os_memcpy(n, src->ssids[i].ssid,
2216                                   src->ssids[i].ssid_len);
2217                         params->ssids[i].ssid = n;
2218                         params->ssids[i].ssid_len = src->ssids[i].ssid_len;
2219                 }
2220         }
2221         params->num_ssids = src->num_ssids;
2222
2223         if (src->extra_ies) {
2224                 n = os_malloc(src->extra_ies_len);
2225                 if (n == NULL)
2226                         goto failed;
2227                 os_memcpy(n, src->extra_ies, src->extra_ies_len);
2228                 params->extra_ies = n;
2229                 params->extra_ies_len = src->extra_ies_len;
2230         }
2231
2232         if (src->freqs) {
2233                 int len = int_array_len(src->freqs);
2234                 params->freqs = os_malloc((len + 1) * sizeof(int));
2235                 if (params->freqs == NULL)
2236                         goto failed;
2237                 os_memcpy(params->freqs, src->freqs, (len + 1) * sizeof(int));
2238         }
2239
2240         if (src->filter_ssids) {
2241                 params->filter_ssids = os_malloc(sizeof(*params->filter_ssids) *
2242                                                  src->num_filter_ssids);
2243                 if (params->filter_ssids == NULL)
2244                         goto failed;
2245                 os_memcpy(params->filter_ssids, src->filter_ssids,
2246                           sizeof(*params->filter_ssids) *
2247                           src->num_filter_ssids);
2248                 params->num_filter_ssids = src->num_filter_ssids;
2249         }
2250
2251         params->filter_rssi = src->filter_rssi;
2252         params->p2p_probe = src->p2p_probe;
2253         params->only_new_results = src->only_new_results;
2254         params->low_priority = src->low_priority;
2255
2256         if (src->sched_scan_plans_num > 0) {
2257                 params->sched_scan_plans =
2258                         os_malloc(sizeof(*src->sched_scan_plans) *
2259                                   src->sched_scan_plans_num);
2260                 if (!params->sched_scan_plans)
2261                         goto failed;
2262
2263                 os_memcpy(params->sched_scan_plans, src->sched_scan_plans,
2264                           sizeof(*src->sched_scan_plans) *
2265                           src->sched_scan_plans_num);
2266                 params->sched_scan_plans_num = src->sched_scan_plans_num;
2267         }
2268
2269         if (src->mac_addr_rand) {
2270                 params->mac_addr_rand = src->mac_addr_rand;
2271
2272                 if (src->mac_addr && src->mac_addr_mask) {
2273                         u8 *mac_addr;
2274
2275                         mac_addr = os_malloc(2 * ETH_ALEN);
2276                         if (!mac_addr)
2277                                 goto failed;
2278
2279                         os_memcpy(mac_addr, src->mac_addr, ETH_ALEN);
2280                         os_memcpy(mac_addr + ETH_ALEN, src->mac_addr_mask,
2281                                   ETH_ALEN);
2282                         params->mac_addr = mac_addr;
2283                         params->mac_addr_mask = mac_addr + ETH_ALEN;
2284                 }
2285         }
2286
2287         if (src->bssid) {
2288                 u8 *bssid;
2289
2290                 bssid = os_malloc(ETH_ALEN);
2291                 if (!bssid)
2292                         goto failed;
2293                 os_memcpy(bssid, src->bssid, ETH_ALEN);
2294                 params->bssid = bssid;
2295         }
2296
2297         return params;
2298
2299 failed:
2300         wpa_scan_free_params(params);
2301         return NULL;
2302 }
2303
2304
2305 void wpa_scan_free_params(struct wpa_driver_scan_params *params)
2306 {
2307         size_t i;
2308
2309         if (params == NULL)
2310                 return;
2311
2312         for (i = 0; i < params->num_ssids; i++)
2313                 os_free((u8 *) params->ssids[i].ssid);
2314         os_free((u8 *) params->extra_ies);
2315         os_free(params->freqs);
2316         os_free(params->filter_ssids);
2317         os_free(params->sched_scan_plans);
2318
2319         /*
2320          * Note: params->mac_addr_mask points to same memory allocation and
2321          * must not be freed separately.
2322          */
2323         os_free((u8 *) params->mac_addr);
2324
2325         os_free((u8 *) params->bssid);
2326
2327         os_free(params);
2328 }
2329
2330
2331 int wpas_start_pno(struct wpa_supplicant *wpa_s)
2332 {
2333         int ret, prio;
2334         size_t i, num_ssid, num_match_ssid;
2335         struct wpa_ssid *ssid;
2336         struct wpa_driver_scan_params params;
2337         struct sched_scan_plan scan_plan;
2338         unsigned int max_sched_scan_ssids;
2339
2340         if (!wpa_s->sched_scan_supported)
2341                 return -1;
2342
2343         if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
2344                 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
2345         else
2346                 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
2347         if (max_sched_scan_ssids < 1)
2348                 return -1;
2349
2350         if (wpa_s->pno || wpa_s->pno_sched_pending)
2351                 return 0;
2352
2353         if ((wpa_s->wpa_state > WPA_SCANNING) &&
2354             (wpa_s->wpa_state <= WPA_COMPLETED)) {
2355                 wpa_printf(MSG_ERROR, "PNO: In assoc process");
2356                 return -EAGAIN;
2357         }
2358
2359         if (wpa_s->wpa_state == WPA_SCANNING) {
2360                 wpa_supplicant_cancel_scan(wpa_s);
2361                 if (wpa_s->sched_scanning) {
2362                         wpa_printf(MSG_DEBUG, "Schedule PNO on completion of "
2363                                    "ongoing sched scan");
2364                         wpa_supplicant_cancel_sched_scan(wpa_s);
2365                         wpa_s->pno_sched_pending = 1;
2366                         return 0;
2367                 }
2368         }
2369
2370         os_memset(&params, 0, sizeof(params));
2371
2372         num_ssid = num_match_ssid = 0;
2373         ssid = wpa_s->conf->ssid;
2374         while (ssid) {
2375                 if (!wpas_network_disabled(wpa_s, ssid)) {
2376                         num_match_ssid++;
2377                         if (ssid->scan_ssid)
2378                                 num_ssid++;
2379                 }
2380                 ssid = ssid->next;
2381         }
2382
2383         if (num_match_ssid == 0) {
2384                 wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
2385                 return -1;
2386         }
2387
2388         if (num_match_ssid > num_ssid) {
2389                 params.num_ssids++; /* wildcard */
2390                 num_ssid++;
2391         }
2392
2393         if (num_ssid > max_sched_scan_ssids) {
2394                 wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
2395                            "%u", max_sched_scan_ssids, (unsigned int) num_ssid);
2396                 num_ssid = max_sched_scan_ssids;
2397         }
2398
2399         if (num_match_ssid > wpa_s->max_match_sets) {
2400                 num_match_ssid = wpa_s->max_match_sets;
2401                 wpa_dbg(wpa_s, MSG_DEBUG, "PNO: Too many SSIDs to match");
2402         }
2403         params.filter_ssids = os_calloc(num_match_ssid,
2404                                         sizeof(struct wpa_driver_scan_filter));
2405         if (params.filter_ssids == NULL)
2406                 return -1;
2407
2408         i = 0;
2409         prio = 0;
2410         ssid = wpa_s->conf->pssid[prio];
2411         while (ssid) {
2412                 if (!wpas_network_disabled(wpa_s, ssid)) {
2413                         if (ssid->scan_ssid && params.num_ssids < num_ssid) {
2414                                 params.ssids[params.num_ssids].ssid =
2415                                         ssid->ssid;
2416                                 params.ssids[params.num_ssids].ssid_len =
2417                                          ssid->ssid_len;
2418                                 params.num_ssids++;
2419                         }
2420                         os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
2421                                   ssid->ssid_len);
2422                         params.filter_ssids[i].ssid_len = ssid->ssid_len;
2423                         params.num_filter_ssids++;
2424                         i++;
2425                         if (i == num_match_ssid)
2426                                 break;
2427                 }
2428                 if (ssid->pnext)
2429                         ssid = ssid->pnext;
2430                 else if (prio + 1 == wpa_s->conf->num_prio)
2431                         break;
2432                 else
2433                         ssid = wpa_s->conf->pssid[++prio];
2434         }
2435
2436         if (wpa_s->conf->filter_rssi)
2437                 params.filter_rssi = wpa_s->conf->filter_rssi;
2438
2439         if (wpa_s->sched_scan_plans_num) {
2440                 params.sched_scan_plans = wpa_s->sched_scan_plans;
2441                 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
2442         } else {
2443                 /* Set one scan plan that will run infinitely */
2444                 if (wpa_s->conf->sched_scan_interval)
2445                         scan_plan.interval = wpa_s->conf->sched_scan_interval;
2446                 else
2447                         scan_plan.interval = 10;
2448
2449                 scan_plan.iterations = 0;
2450                 params.sched_scan_plans = &scan_plan;
2451                 params.sched_scan_plans_num = 1;
2452         }
2453
2454         if (params.freqs == NULL && wpa_s->manual_sched_scan_freqs) {
2455                 wpa_dbg(wpa_s, MSG_DEBUG, "Limit sched scan to specified channels");
2456                 params.freqs = wpa_s->manual_sched_scan_freqs;
2457         }
2458
2459         if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_PNO) {
2460                 params.mac_addr_rand = 1;
2461                 if (wpa_s->mac_addr_pno) {
2462                         params.mac_addr = wpa_s->mac_addr_pno;
2463                         params.mac_addr_mask = wpa_s->mac_addr_pno + ETH_ALEN;
2464                 }
2465         }
2466
2467         ret = wpa_supplicant_start_sched_scan(wpa_s, &params);
2468         os_free(params.filter_ssids);
2469         if (ret == 0)
2470                 wpa_s->pno = 1;
2471         else
2472                 wpa_msg(wpa_s, MSG_ERROR, "Failed to schedule PNO");
2473         return ret;
2474 }
2475
2476
2477 int wpas_stop_pno(struct wpa_supplicant *wpa_s)
2478 {
2479         int ret = 0;
2480
2481         if (!wpa_s->pno)
2482                 return 0;
2483
2484         ret = wpa_supplicant_stop_sched_scan(wpa_s);
2485
2486         wpa_s->pno = 0;
2487         wpa_s->pno_sched_pending = 0;
2488
2489         if (wpa_s->wpa_state == WPA_SCANNING)
2490                 wpa_supplicant_req_scan(wpa_s, 0, 0);
2491
2492         return ret;
2493 }
2494
2495
2496 void wpas_mac_addr_rand_scan_clear(struct wpa_supplicant *wpa_s,
2497                                     unsigned int type)
2498 {
2499         type &= MAC_ADDR_RAND_ALL;
2500         wpa_s->mac_addr_rand_enable &= ~type;
2501
2502         if (type & MAC_ADDR_RAND_SCAN) {
2503                 os_free(wpa_s->mac_addr_scan);
2504                 wpa_s->mac_addr_scan = NULL;
2505         }
2506
2507         if (type & MAC_ADDR_RAND_SCHED_SCAN) {
2508                 os_free(wpa_s->mac_addr_sched_scan);
2509                 wpa_s->mac_addr_sched_scan = NULL;
2510         }
2511
2512         if (type & MAC_ADDR_RAND_PNO) {
2513                 os_free(wpa_s->mac_addr_pno);
2514                 wpa_s->mac_addr_pno = NULL;
2515         }
2516 }
2517
2518
2519 int wpas_mac_addr_rand_scan_set(struct wpa_supplicant *wpa_s,
2520                                 unsigned int type, const u8 *addr,
2521                                 const u8 *mask)
2522 {
2523         u8 *tmp = NULL;
2524
2525         wpas_mac_addr_rand_scan_clear(wpa_s, type);
2526
2527         if (addr) {
2528                 tmp = os_malloc(2 * ETH_ALEN);
2529                 if (!tmp)
2530                         return -1;
2531                 os_memcpy(tmp, addr, ETH_ALEN);
2532                 os_memcpy(tmp + ETH_ALEN, mask, ETH_ALEN);
2533         }
2534
2535         if (type == MAC_ADDR_RAND_SCAN) {
2536                 wpa_s->mac_addr_scan = tmp;
2537         } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
2538                 wpa_s->mac_addr_sched_scan = tmp;
2539         } else if (type == MAC_ADDR_RAND_PNO) {
2540                 wpa_s->mac_addr_pno = tmp;
2541         } else {
2542                 wpa_printf(MSG_INFO,
2543                            "scan: Invalid MAC randomization type=0x%x",
2544                            type);
2545                 os_free(tmp);
2546                 return -1;
2547         }
2548
2549         wpa_s->mac_addr_rand_enable |= type;
2550         return 0;
2551 }
2552
2553
2554 int wpas_abort_ongoing_scan(struct wpa_supplicant *wpa_s)
2555 {
2556         int scan_work = !!wpa_s->scan_work;
2557
2558 #ifdef CONFIG_P2P
2559         scan_work |= !!wpa_s->p2p_scan_work;
2560 #endif /* CONFIG_P2P */
2561
2562         if (scan_work && wpa_s->own_scan_running) {
2563                 wpa_dbg(wpa_s, MSG_DEBUG, "Abort an ongoing scan");
2564                 return wpa_drv_abort_scan(wpa_s);
2565         }
2566
2567         return 0;
2568 }
2569
2570
2571 int wpas_sched_scan_plans_set(struct wpa_supplicant *wpa_s, const char *cmd)
2572 {
2573         struct sched_scan_plan *scan_plans = NULL;
2574         const char *token, *context = NULL;
2575         unsigned int num = 0;
2576
2577         if (!cmd)
2578                 return -1;
2579
2580         if (!cmd[0]) {
2581                 wpa_printf(MSG_DEBUG, "Clear sched scan plans");
2582                 os_free(wpa_s->sched_scan_plans);
2583                 wpa_s->sched_scan_plans = NULL;
2584                 wpa_s->sched_scan_plans_num = 0;
2585                 return 0;
2586         }
2587
2588         while ((token = cstr_token(cmd, " ", &context))) {
2589                 int ret;
2590                 struct sched_scan_plan *scan_plan, *n;
2591
2592                 n = os_realloc_array(scan_plans, num + 1, sizeof(*scan_plans));
2593                 if (!n)
2594                         goto fail;
2595
2596                 scan_plans = n;
2597                 scan_plan = &scan_plans[num];
2598                 num++;
2599
2600                 ret = sscanf(token, "%u:%u", &scan_plan->interval,
2601                              &scan_plan->iterations);
2602                 if (ret <= 0 || ret > 2 || !scan_plan->interval) {
2603                         wpa_printf(MSG_ERROR,
2604                                    "Invalid sched scan plan input: %s", token);
2605                         goto fail;
2606                 }
2607
2608                 if (!scan_plan->interval) {
2609                         wpa_printf(MSG_ERROR,
2610                                    "scan plan %u: Interval cannot be zero",
2611                                    num);
2612                         goto fail;
2613                 }
2614
2615                 if (scan_plan->interval > wpa_s->max_sched_scan_plan_interval) {
2616                         wpa_printf(MSG_WARNING,
2617                                    "scan plan %u: Scan interval too long(%u), use the maximum allowed(%u)",
2618                                    num, scan_plan->interval,
2619                                    wpa_s->max_sched_scan_plan_interval);
2620                         scan_plan->interval =
2621                                 wpa_s->max_sched_scan_plan_interval;
2622                 }
2623
2624                 if (ret == 1) {
2625                         scan_plan->iterations = 0;
2626                         break;
2627                 }
2628
2629                 if (!scan_plan->iterations) {
2630                         wpa_printf(MSG_ERROR,
2631                                    "scan plan %u: Number of iterations cannot be zero",
2632                                    num);
2633                         goto fail;
2634                 }
2635
2636                 if (scan_plan->iterations >
2637                     wpa_s->max_sched_scan_plan_iterations) {
2638                         wpa_printf(MSG_WARNING,
2639                                    "scan plan %u: Too many iterations(%u), use the maximum allowed(%u)",
2640                                    num, scan_plan->iterations,
2641                                    wpa_s->max_sched_scan_plan_iterations);
2642                         scan_plan->iterations =
2643                                 wpa_s->max_sched_scan_plan_iterations;
2644                 }
2645
2646                 wpa_printf(MSG_DEBUG,
2647                            "scan plan %u: interval=%u iterations=%u",
2648                            num, scan_plan->interval, scan_plan->iterations);
2649         }
2650
2651         if (!scan_plans) {
2652                 wpa_printf(MSG_ERROR, "Invalid scan plans entry");
2653                 goto fail;
2654         }
2655
2656         if (cstr_token(cmd, " ", &context) || scan_plans[num - 1].iterations) {
2657                 wpa_printf(MSG_ERROR,
2658                            "All scan plans but the last must specify a number of iterations");
2659                 goto fail;
2660         }
2661
2662         wpa_printf(MSG_DEBUG, "scan plan %u (last plan): interval=%u",
2663                    num, scan_plans[num - 1].interval);
2664
2665         if (num > wpa_s->max_sched_scan_plans) {
2666                 wpa_printf(MSG_WARNING,
2667                            "Too many scheduled scan plans (only %u supported)",
2668                            wpa_s->max_sched_scan_plans);
2669                 wpa_printf(MSG_WARNING,
2670                            "Use only the first %u scan plans, and the last one (in infinite loop)",
2671                            wpa_s->max_sched_scan_plans - 1);
2672                 os_memcpy(&scan_plans[wpa_s->max_sched_scan_plans - 1],
2673                           &scan_plans[num - 1], sizeof(*scan_plans));
2674                 num = wpa_s->max_sched_scan_plans;
2675         }
2676
2677         os_free(wpa_s->sched_scan_plans);
2678         wpa_s->sched_scan_plans = scan_plans;
2679         wpa_s->sched_scan_plans_num = num;
2680
2681         return 0;
2682
2683 fail:
2684         os_free(scan_plans);
2685         wpa_printf(MSG_ERROR, "invalid scan plans list");
2686         return -1;
2687 }