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