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