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