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