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