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