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