Reschedule own scan request if an externally started one is in progress
[mech_eap.git] / wpa_supplicant / scan.c
1 /*
2  * WPA Supplicant - Scanning
3  * Copyright (c) 2003-2012, 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 "gas_query.h"
25 #include "scan.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         while (ssid) {
100                 if (!wpas_network_disabled(wpa_s, ssid))
101                         count++;
102                 else
103                         disabled++;
104                 ssid = ssid->next;
105         }
106         if (wpa_s->conf->cred && wpa_s->conf->interworking &&
107             wpa_s->conf->auto_interworking)
108                 count++;
109         if (count == 0 && disabled > 0) {
110                 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks (%d disabled "
111                         "networks)", disabled);
112         }
113         return count;
114 }
115
116
117 static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
118                                      struct wpa_ssid *ssid)
119 {
120         while (ssid) {
121                 if (!wpas_network_disabled(wpa_s, ssid))
122                         break;
123                 ssid = ssid->next;
124         }
125
126         /* ap_scan=2 mode - try to associate with each SSID. */
127         if (ssid == NULL) {
128                 wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
129                         "end of scan list - go back to beginning");
130                 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
131                 wpa_supplicant_req_scan(wpa_s, 0, 0);
132                 return;
133         }
134         if (ssid->next) {
135                 /* Continue from the next SSID on the next attempt. */
136                 wpa_s->prev_scan_ssid = ssid;
137         } else {
138                 /* Start from the beginning of the SSID list. */
139                 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
140         }
141         wpa_supplicant_associate(wpa_s, NULL, ssid);
142 }
143
144
145 /**
146  * wpa_supplicant_trigger_scan - Request driver to start a scan
147  * @wpa_s: Pointer to wpa_supplicant data
148  * @params: Scan parameters
149  * Returns: 0 on success, -1 on failure
150  */
151 int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
152                                 struct wpa_driver_scan_params *params)
153 {
154         int ret;
155
156         wpa_supplicant_notify_scanning(wpa_s, 1);
157
158         ret = wpa_drv_scan(wpa_s, params);
159         if (ret) {
160                 wpa_supplicant_notify_scanning(wpa_s, 0);
161                 wpas_notify_scan_done(wpa_s, 0);
162         } else {
163                 os_get_reltime(&wpa_s->scan_trigger_time);
164                 wpa_s->scan_runs++;
165                 wpa_s->normal_scans++;
166                 wpa_s->own_scan_requested = 1;
167         }
168
169         return ret;
170 }
171
172
173 static void
174 wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
175 {
176         struct wpa_supplicant *wpa_s = eloop_ctx;
177
178         wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
179
180         if (wpa_supplicant_req_sched_scan(wpa_s))
181                 wpa_supplicant_req_scan(wpa_s, 0, 0);
182 }
183
184
185 static void
186 wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
187 {
188         struct wpa_supplicant *wpa_s = eloop_ctx;
189
190         wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
191
192         wpa_s->sched_scan_timed_out = 1;
193         wpa_supplicant_cancel_sched_scan(wpa_s);
194 }
195
196
197 int wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
198                                     struct wpa_driver_scan_params *params,
199                                     int interval)
200 {
201         int ret;
202
203         wpa_supplicant_notify_scanning(wpa_s, 1);
204         ret = wpa_drv_sched_scan(wpa_s, params, interval * 1000);
205         if (ret)
206                 wpa_supplicant_notify_scanning(wpa_s, 0);
207         else
208                 wpa_s->sched_scanning = 1;
209
210         return ret;
211 }
212
213
214 int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
215 {
216         int ret;
217
218         ret = wpa_drv_stop_sched_scan(wpa_s);
219         if (ret) {
220                 wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
221                 /* TODO: what to do if stopping fails? */
222                 return -1;
223         }
224
225         return ret;
226 }
227
228
229 static struct wpa_driver_scan_filter *
230 wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
231 {
232         struct wpa_driver_scan_filter *ssids;
233         struct wpa_ssid *ssid;
234         size_t count;
235
236         *num_ssids = 0;
237         if (!conf->filter_ssids)
238                 return NULL;
239
240         for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
241                 if (ssid->ssid && ssid->ssid_len)
242                         count++;
243         }
244         if (count == 0)
245                 return NULL;
246         ssids = os_zalloc(count * sizeof(struct wpa_driver_scan_filter));
247         if (ssids == NULL)
248                 return NULL;
249
250         for (ssid = conf->ssid; ssid; ssid = ssid->next) {
251                 if (!ssid->ssid || !ssid->ssid_len)
252                         continue;
253                 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
254                 ssids[*num_ssids].ssid_len = ssid->ssid_len;
255                 (*num_ssids)++;
256         }
257
258         return ssids;
259 }
260
261
262 static void wpa_supplicant_optimize_freqs(
263         struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
264 {
265 #ifdef CONFIG_P2P
266         if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
267             wpa_s->go_params) {
268                 /* Optimize provisioning state scan based on GO information */
269                 if (wpa_s->p2p_in_provisioning < 5 &&
270                     wpa_s->go_params->freq > 0) {
271                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
272                                 "preferred frequency %d MHz",
273                                 wpa_s->go_params->freq);
274                         params->freqs = os_zalloc(2 * sizeof(int));
275                         if (params->freqs)
276                                 params->freqs[0] = wpa_s->go_params->freq;
277                 } else if (wpa_s->p2p_in_provisioning < 8 &&
278                            wpa_s->go_params->freq_list[0]) {
279                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
280                                 "channels");
281                         int_array_concat(&params->freqs,
282                                          wpa_s->go_params->freq_list);
283                         if (params->freqs)
284                                 int_array_sort_unique(params->freqs);
285                 }
286                 wpa_s->p2p_in_provisioning++;
287         }
288 #endif /* CONFIG_P2P */
289
290 #ifdef CONFIG_WPS
291         if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
292                 /*
293                  * Optimize post-provisioning scan based on channel used
294                  * during provisioning.
295                  */
296                 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
297                         "that was used during provisioning", wpa_s->wps_freq);
298                 params->freqs = os_zalloc(2 * sizeof(int));
299                 if (params->freqs)
300                         params->freqs[0] = wpa_s->wps_freq;
301                 wpa_s->after_wps--;
302         }
303
304         if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
305         {
306                 /* Optimize provisioning scan based on already known channel */
307                 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
308                         wpa_s->wps_freq);
309                 params->freqs = os_zalloc(2 * sizeof(int));
310                 if (params->freqs)
311                         params->freqs[0] = wpa_s->wps_freq;
312                 wpa_s->known_wps_freq = 0; /* only do this once */
313         }
314 #endif /* CONFIG_WPS */
315 }
316
317
318 #ifdef CONFIG_INTERWORKING
319 static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
320                                            struct wpabuf *buf)
321 {
322         if (wpa_s->conf->interworking == 0)
323                 return;
324
325         wpabuf_put_u8(buf, WLAN_EID_EXT_CAPAB);
326         wpabuf_put_u8(buf, 4);
327         wpabuf_put_u8(buf, 0x00);
328         wpabuf_put_u8(buf, 0x00);
329         wpabuf_put_u8(buf, 0x00);
330         wpabuf_put_u8(buf, 0x80); /* Bit 31 - Interworking */
331
332         wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
333         wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
334                       1 + ETH_ALEN);
335         wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
336         /* No Venue Info */
337         if (!is_zero_ether_addr(wpa_s->conf->hessid))
338                 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
339 }
340 #endif /* CONFIG_INTERWORKING */
341
342
343 static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
344 {
345         struct wpabuf *extra_ie = NULL;
346 #ifdef CONFIG_WPS
347         int wps = 0;
348         enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
349 #endif /* CONFIG_WPS */
350
351 #ifdef CONFIG_INTERWORKING
352         if (wpa_s->conf->interworking &&
353             wpabuf_resize(&extra_ie, 100) == 0)
354                 wpas_add_interworking_elements(wpa_s, extra_ie);
355 #endif /* CONFIG_INTERWORKING */
356
357 #ifdef CONFIG_WPS
358         wps = wpas_wps_in_use(wpa_s, &req_type);
359
360         if (wps) {
361                 struct wpabuf *wps_ie;
362                 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
363                                                 DEV_PW_DEFAULT,
364                                                 &wpa_s->wps->dev,
365                                                 wpa_s->wps->uuid, req_type,
366                                                 0, NULL);
367                 if (wps_ie) {
368                         if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
369                                 wpabuf_put_buf(extra_ie, wps_ie);
370                         wpabuf_free(wps_ie);
371                 }
372         }
373
374 #ifdef CONFIG_P2P
375         if (wps) {
376                 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
377                 if (wpabuf_resize(&extra_ie, ielen) == 0)
378                         wpas_p2p_scan_ie(wpa_s, extra_ie);
379         }
380 #endif /* CONFIG_P2P */
381
382 #endif /* CONFIG_WPS */
383
384 #ifdef CONFIG_HS20
385         if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 7) == 0)
386                 wpas_hs20_add_indication(extra_ie);
387 #endif /* CONFIG_HS20 */
388
389         return extra_ie;
390 }
391
392
393 #ifdef CONFIG_P2P
394
395 /*
396  * Check whether there are any enabled networks or credentials that could be
397  * used for a non-P2P connection.
398  */
399 static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
400 {
401         struct wpa_ssid *ssid;
402
403         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
404                 if (wpas_network_disabled(wpa_s, ssid))
405                         continue;
406                 if (!ssid->p2p_group)
407                         return 1;
408         }
409
410         if (wpa_s->conf->cred && wpa_s->conf->interworking &&
411             wpa_s->conf->auto_interworking)
412                 return 1;
413
414         return 0;
415 }
416
417 #endif /* CONFIG_P2P */
418
419
420 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
421                                           u16 num_modes,
422                                           enum hostapd_hw_mode mode)
423 {
424         u16 i;
425
426         for (i = 0; i < num_modes; i++) {
427                 if (modes[i].mode == mode)
428                         return &modes[i];
429         }
430
431         return NULL;
432 }
433
434
435 static void wpa_setband_scan_freqs_list(struct wpa_supplicant *wpa_s,
436                                         enum hostapd_hw_mode band,
437                                         struct wpa_driver_scan_params *params)
438 {
439         /* Include only supported channels for the specified band */
440         struct hostapd_hw_modes *mode;
441         int count, i;
442
443         mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band);
444         if (mode == NULL) {
445                 /* No channels supported in this band - use empty list */
446                 params->freqs = os_zalloc(sizeof(int));
447                 return;
448         }
449
450         params->freqs = os_zalloc((mode->num_channels + 1) * sizeof(int));
451         if (params->freqs == NULL)
452                 return;
453         for (count = 0, i = 0; i < mode->num_channels; i++) {
454                 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
455                         continue;
456                 params->freqs[count++] = mode->channels[i].freq;
457         }
458 }
459
460
461 static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
462                                    struct wpa_driver_scan_params *params)
463 {
464         if (wpa_s->hw.modes == NULL)
465                 return; /* unknown what channels the driver supports */
466         if (params->freqs)
467                 return; /* already using a limited channel set */
468         if (wpa_s->setband == WPA_SETBAND_5G)
469                 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
470                                             params);
471         else if (wpa_s->setband == WPA_SETBAND_2G)
472                 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G,
473                                             params);
474 }
475
476
477 static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
478 {
479         struct wpa_supplicant *wpa_s = eloop_ctx;
480         struct wpa_ssid *ssid;
481         int ret;
482         struct wpabuf *extra_ie = NULL;
483         struct wpa_driver_scan_params params;
484         struct wpa_driver_scan_params *scan_params;
485         size_t max_ssids;
486         enum wpa_states prev_state;
487
488         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
489                 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
490                 wpas_p2p_continue_after_scan(wpa_s);
491                 return;
492         }
493
494         if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
495                 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
496                 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
497                 wpas_p2p_continue_after_scan(wpa_s);
498                 return;
499         }
500
501         if (wpa_s->scanning) {
502                 /*
503                  * If we are already in scanning state, we shall reschedule the
504                  * the incoming scan request.
505                  */
506                 wpa_dbg(wpa_s, MSG_DEBUG, "Already scanning - Reschedule the incoming scan req");
507                 wpa_supplicant_req_scan(wpa_s, 1, 0);
508                 return;
509         }
510
511         if (wpa_s->external_scan_running) {
512                 struct os_reltime now, diff;
513                 os_get_reltime(&now);
514                 os_reltime_sub(&now, &wpa_s->scan_start_time, &diff);
515                 if (diff.sec < 30) {
516                         wpa_dbg(wpa_s, MSG_DEBUG, "Externally triggered scan running - Reschedule the incoming scan req");
517                         wpa_supplicant_req_scan(wpa_s, 1, 0);
518                         return;
519                 }
520         }
521
522         if (!wpa_supplicant_enabled_networks(wpa_s) &&
523             wpa_s->scan_req == NORMAL_SCAN_REQ) {
524                 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
525                 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
526                 wpas_p2p_continue_after_scan(wpa_s);
527                 return;
528         }
529
530         if (wpa_s->conf->ap_scan != 0 &&
531             (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
532                 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
533                         "overriding ap_scan configuration");
534                 wpa_s->conf->ap_scan = 0;
535                 wpas_notify_ap_scan_changed(wpa_s);
536         }
537
538         if (wpa_s->conf->ap_scan == 0) {
539                 wpa_supplicant_gen_assoc_event(wpa_s);
540                 return;
541         }
542
543 #ifdef CONFIG_P2P
544         if (wpas_p2p_in_progress(wpa_s) || wpas_wpa_is_in_progress(wpa_s, 0)) {
545                 if (wpa_s->sta_scan_pending &&
546                     wpas_p2p_in_progress(wpa_s) == 2 &&
547                     wpa_s->global->p2p_cb_on_scan_complete) {
548                         wpa_dbg(wpa_s, MSG_DEBUG, "Process pending station "
549                                 "mode scan during P2P search");
550                 } else {
551                         wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan "
552                                 "while P2P operation is in progress");
553                         wpa_s->sta_scan_pending = 1;
554                         wpa_supplicant_req_scan(wpa_s, 5, 0);
555                         return;
556                 }
557         }
558 #endif /* CONFIG_P2P */
559
560 #ifdef CONFIG_GAS
561         if (gas_query_in_progress(wpa_s->gas)) {
562                 wpa_dbg(wpa_s, MSG_DEBUG, "Delay scan while GAS query is in progress");
563                 wpa_supplicant_req_scan(wpa_s, 1, 0);
564                 return;
565         }
566 #endif /* CONFIG_GAS */
567
568         if (wpa_s->conf->ap_scan == 2)
569                 max_ssids = 1;
570         else {
571                 max_ssids = wpa_s->max_scan_ssids;
572                 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
573                         max_ssids = WPAS_MAX_SCAN_SSIDS;
574         }
575
576         wpa_s->last_scan_req = wpa_s->scan_req;
577         wpa_s->scan_req = NORMAL_SCAN_REQ;
578
579         os_memset(&params, 0, sizeof(params));
580
581         prev_state = wpa_s->wpa_state;
582         if (wpa_s->wpa_state == WPA_DISCONNECTED ||
583             wpa_s->wpa_state == WPA_INACTIVE)
584                 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
585
586         /*
587          * If autoscan has set its own scanning parameters
588          */
589         if (wpa_s->autoscan_params != NULL) {
590                 scan_params = wpa_s->autoscan_params;
591                 goto scan;
592         }
593
594         if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
595             wpa_s->connect_without_scan) {
596                 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
597                         if (ssid == wpa_s->connect_without_scan)
598                                 break;
599                 }
600                 wpa_s->connect_without_scan = NULL;
601                 if (ssid) {
602                         wpa_printf(MSG_DEBUG, "Start a pre-selected network "
603                                    "without scan step");
604                         wpa_supplicant_associate(wpa_s, NULL, ssid);
605                         return;
606                 }
607         }
608
609 #ifdef CONFIG_P2P
610         if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
611             wpa_s->go_params) {
612                 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
613                            wpa_s->p2p_in_provisioning,
614                            wpa_s->show_group_started);
615                 params.ssids[0].ssid = wpa_s->go_params->ssid;
616                 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
617                 params.num_ssids = 1;
618                 goto ssid_list_set;
619         }
620 #endif /* CONFIG_P2P */
621
622         /* Find the starting point from which to continue scanning */
623         ssid = wpa_s->conf->ssid;
624         if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
625                 while (ssid) {
626                         if (ssid == wpa_s->prev_scan_ssid) {
627                                 ssid = ssid->next;
628                                 break;
629                         }
630                         ssid = ssid->next;
631                 }
632         }
633
634         if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
635             wpa_s->conf->ap_scan == 2) {
636                 wpa_s->connect_without_scan = NULL;
637                 wpa_s->prev_scan_wildcard = 0;
638                 wpa_supplicant_assoc_try(wpa_s, ssid);
639                 return;
640         } else if (wpa_s->conf->ap_scan == 2) {
641                 /*
642                  * User-initiated scan request in ap_scan == 2; scan with
643                  * wildcard SSID.
644                  */
645                 ssid = NULL;
646         } else {
647                 struct wpa_ssid *start = ssid, *tssid;
648                 int freqs_set = 0;
649                 if (ssid == NULL && max_ssids > 1)
650                         ssid = wpa_s->conf->ssid;
651                 while (ssid) {
652                         if (!wpas_network_disabled(wpa_s, ssid) &&
653                             ssid->scan_ssid) {
654                                 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
655                                                   ssid->ssid, ssid->ssid_len);
656                                 params.ssids[params.num_ssids].ssid =
657                                         ssid->ssid;
658                                 params.ssids[params.num_ssids].ssid_len =
659                                         ssid->ssid_len;
660                                 params.num_ssids++;
661                                 if (params.num_ssids + 1 >= max_ssids)
662                                         break;
663                         }
664                         ssid = ssid->next;
665                         if (ssid == start)
666                                 break;
667                         if (ssid == NULL && max_ssids > 1 &&
668                             start != wpa_s->conf->ssid)
669                                 ssid = wpa_s->conf->ssid;
670                 }
671
672                 for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
673                         if (wpas_network_disabled(wpa_s, tssid))
674                                 continue;
675                         if ((params.freqs || !freqs_set) && tssid->scan_freq) {
676                                 int_array_concat(&params.freqs,
677                                                  tssid->scan_freq);
678                         } else {
679                                 os_free(params.freqs);
680                                 params.freqs = NULL;
681                         }
682                         freqs_set = 1;
683                 }
684                 int_array_sort_unique(params.freqs);
685         }
686
687         if (ssid && max_ssids == 1) {
688                 /*
689                  * If the driver is limited to 1 SSID at a time interleave
690                  * wildcard SSID scans with specific SSID scans to avoid
691                  * waiting a long time for a wildcard scan.
692                  */
693                 if (!wpa_s->prev_scan_wildcard) {
694                         params.ssids[0].ssid = NULL;
695                         params.ssids[0].ssid_len = 0;
696                         wpa_s->prev_scan_wildcard = 1;
697                         wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
698                                 "wildcard SSID (Interleave with specific)");
699                 } else {
700                         wpa_s->prev_scan_ssid = ssid;
701                         wpa_s->prev_scan_wildcard = 0;
702                         wpa_dbg(wpa_s, MSG_DEBUG,
703                                 "Starting AP scan for specific SSID: %s",
704                                 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
705                 }
706         } else if (ssid) {
707                 /* max_ssids > 1 */
708
709                 wpa_s->prev_scan_ssid = ssid;
710                 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
711                         "the scan request");
712                 params.num_ssids++;
713         } else if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
714                    wpa_s->manual_scan_passive && params.num_ssids == 0) {
715                 wpa_dbg(wpa_s, MSG_DEBUG, "Use passive scan based on manual request");
716         } else {
717                 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
718                 params.num_ssids++;
719                 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
720                         "SSID");
721         }
722 #ifdef CONFIG_P2P
723 ssid_list_set:
724 #endif /* CONFIG_P2P */
725
726         wpa_supplicant_optimize_freqs(wpa_s, &params);
727         extra_ie = wpa_supplicant_extra_ies(wpa_s);
728
729         if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs == NULL &&
730             wpa_s->manual_scan_freqs) {
731                 wpa_dbg(wpa_s, MSG_DEBUG, "Limit manual scan to specified channels");
732                 params.freqs = wpa_s->manual_scan_freqs;
733                 wpa_s->manual_scan_freqs = NULL;
734         }
735
736         if (params.freqs == NULL && wpa_s->next_scan_freqs) {
737                 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
738                         "generated frequency list");
739                 params.freqs = wpa_s->next_scan_freqs;
740         } else
741                 os_free(wpa_s->next_scan_freqs);
742         wpa_s->next_scan_freqs = NULL;
743         wpa_setband_scan_freqs(wpa_s, &params);
744
745         /* See if user specified frequencies. If so, scan only those. */
746         if (wpa_s->conf->freq_list && !params.freqs) {
747                 wpa_dbg(wpa_s, MSG_DEBUG,
748                         "Optimize scan based on conf->freq_list");
749                 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
750         }
751
752         /* Use current associated channel? */
753         if (wpa_s->conf->scan_cur_freq && !params.freqs) {
754                 unsigned int num = wpa_s->num_multichan_concurrent;
755
756                 params.freqs = os_calloc(num + 1, sizeof(int));
757                 if (params.freqs) {
758                         num = get_shared_radio_freqs(wpa_s, params.freqs, num);
759                         if (num > 0) {
760                                 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
761                                         "current operating channels since "
762                                         "scan_cur_freq is enabled");
763                         } else {
764                                 os_free(params.freqs);
765                                 params.freqs = NULL;
766                         }
767                 }
768         }
769
770         params.filter_ssids = wpa_supplicant_build_filter_ssids(
771                 wpa_s->conf, &params.num_filter_ssids);
772         if (extra_ie) {
773                 params.extra_ies = wpabuf_head(extra_ie);
774                 params.extra_ies_len = wpabuf_len(extra_ie);
775         }
776
777 #ifdef CONFIG_P2P
778         if (wpa_s->p2p_in_provisioning ||
779             (wpa_s->show_group_started && wpa_s->go_params)) {
780                 /*
781                  * The interface may not yet be in P2P mode, so we have to
782                  * explicitly request P2P probe to disable CCK rates.
783                  */
784                 params.p2p_probe = 1;
785         }
786 #endif /* CONFIG_P2P */
787
788         scan_params = &params;
789
790 scan:
791 #ifdef CONFIG_P2P
792         /*
793          * If the driver does not support multi-channel concurrency and a
794          * virtual interface that shares the same radio with the wpa_s interface
795          * is operating there may not be need to scan other channels apart from
796          * the current operating channel on the other virtual interface. Filter
797          * out other channels in case we are trying to find a connection for a
798          * station interface when we are not configured to prefer station
799          * connection and a concurrent operation is already in process.
800          */
801         if (wpa_s->scan_for_connection &&
802             wpa_s->last_scan_req == NORMAL_SCAN_REQ &&
803             !scan_params->freqs && !params.freqs &&
804             wpas_is_p2p_prioritized(wpa_s) &&
805             wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
806             non_p2p_network_enabled(wpa_s)) {
807                 unsigned int num = wpa_s->num_multichan_concurrent;
808
809                 params.freqs = os_calloc(num + 1, sizeof(int));
810                 if (params.freqs) {
811                         num = get_shared_radio_freqs(wpa_s, params.freqs, num);
812                         if (num > 0 && num == wpa_s->num_multichan_concurrent) {
813                                 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
814                         } else {
815                                 os_free(params.freqs);
816                                 params.freqs = NULL;
817                         }
818                 }
819         }
820 #endif /* CONFIG_P2P */
821
822         ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
823
824         if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs &&
825             !wpa_s->manual_scan_freqs) {
826                 /* Restore manual_scan_freqs for the next attempt */
827                 wpa_s->manual_scan_freqs = params.freqs;
828                 params.freqs = NULL;
829         }
830
831         wpabuf_free(extra_ie);
832         os_free(params.freqs);
833         os_free(params.filter_ssids);
834
835         if (ret) {
836                 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
837                 if (prev_state != wpa_s->wpa_state)
838                         wpa_supplicant_set_state(wpa_s, prev_state);
839                 /* Restore scan_req since we will try to scan again */
840                 wpa_s->scan_req = wpa_s->last_scan_req;
841                 wpa_supplicant_req_scan(wpa_s, 1, 0);
842         } else {
843                 wpa_s->scan_for_connection = 0;
844         }
845 }
846
847
848 void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
849 {
850         struct os_reltime remaining, new_int;
851         int cancelled;
852
853         cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
854                                              &remaining);
855
856         new_int.sec = sec;
857         new_int.usec = 0;
858         if (cancelled && os_reltime_before(&remaining, &new_int)) {
859                 new_int.sec = remaining.sec;
860                 new_int.usec = remaining.usec;
861         }
862
863         if (cancelled) {
864                 eloop_register_timeout(new_int.sec, new_int.usec,
865                                        wpa_supplicant_scan, wpa_s, NULL);
866         }
867         wpa_s->scan_interval = sec;
868 }
869
870
871 /**
872  * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
873  * @wpa_s: Pointer to wpa_supplicant data
874  * @sec: Number of seconds after which to scan
875  * @usec: Number of microseconds after which to scan
876  *
877  * This function is used to schedule a scan for neighboring access points after
878  * the specified time.
879  */
880 void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
881 {
882         if (eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL))
883         {
884                 wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d sec %d usec",
885                         sec, usec);
886                 return;
887         }
888
889         wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
890                 sec, usec);
891         eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
892         eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
893 }
894
895
896 /**
897  * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
898  * @wpa_s: Pointer to wpa_supplicant data
899  * @sec: Number of seconds after which to scan
900  * @usec: Number of microseconds after which to scan
901  * Returns: 0 on success or -1 otherwise
902  *
903  * This function is used to schedule periodic scans for neighboring
904  * access points after the specified time.
905  */
906 int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
907                                       int sec, int usec)
908 {
909         if (!wpa_s->sched_scan_supported)
910                 return -1;
911
912         eloop_register_timeout(sec, usec,
913                                wpa_supplicant_delayed_sched_scan_timeout,
914                                wpa_s, NULL);
915
916         return 0;
917 }
918
919
920 /**
921  * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
922  * @wpa_s: Pointer to wpa_supplicant data
923  * Returns: 0 is sched_scan was started or -1 otherwise
924  *
925  * This function is used to schedule periodic scans for neighboring
926  * access points repeating the scan continuously.
927  */
928 int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
929 {
930         struct wpa_driver_scan_params params;
931         struct wpa_driver_scan_params *scan_params;
932         enum wpa_states prev_state;
933         struct wpa_ssid *ssid = NULL;
934         struct wpabuf *extra_ie = NULL;
935         int ret;
936         unsigned int max_sched_scan_ssids;
937         int wildcard = 0;
938         int need_ssids;
939
940         if (!wpa_s->sched_scan_supported)
941                 return -1;
942
943         if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
944                 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
945         else
946                 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
947         if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
948                 return -1;
949
950         if (wpa_s->sched_scanning) {
951                 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
952                 return 0;
953         }
954
955         need_ssids = 0;
956         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
957                 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
958                         /* Use wildcard SSID to find this network */
959                         wildcard = 1;
960                 } else if (!wpas_network_disabled(wpa_s, ssid) &&
961                            ssid->ssid_len)
962                         need_ssids++;
963
964 #ifdef CONFIG_WPS
965                 if (!wpas_network_disabled(wpa_s, ssid) &&
966                     ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
967                         /*
968                          * Normal scan is more reliable and faster for WPS
969                          * operations and since these are for short periods of
970                          * time, the benefit of trying to use sched_scan would
971                          * be limited.
972                          */
973                         wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
974                                 "sched_scan for WPS");
975                         return -1;
976                 }
977 #endif /* CONFIG_WPS */
978         }
979         if (wildcard)
980                 need_ssids++;
981
982         if (wpa_s->normal_scans < 3 &&
983             (need_ssids <= wpa_s->max_scan_ssids ||
984              wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
985                 /*
986                  * When normal scan can speed up operations, use that for the
987                  * first operations before starting the sched_scan to allow
988                  * user space sleep more. We do this only if the normal scan
989                  * has functionality that is suitable for this or if the
990                  * sched_scan does not have better support for multiple SSIDs.
991                  */
992                 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
993                         "sched_scan for initial scans (normal_scans=%d)",
994                         wpa_s->normal_scans);
995                 return -1;
996         }
997
998         os_memset(&params, 0, sizeof(params));
999
1000         /* If we can't allocate space for the filters, we just don't filter */
1001         params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
1002                                         sizeof(struct wpa_driver_scan_filter));
1003
1004         prev_state = wpa_s->wpa_state;
1005         if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1006             wpa_s->wpa_state == WPA_INACTIVE)
1007                 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1008
1009         if (wpa_s->autoscan_params != NULL) {
1010                 scan_params = wpa_s->autoscan_params;
1011                 goto scan;
1012         }
1013
1014         /* Find the starting point from which to continue scanning */
1015         ssid = wpa_s->conf->ssid;
1016         if (wpa_s->prev_sched_ssid) {
1017                 while (ssid) {
1018                         if (ssid == wpa_s->prev_sched_ssid) {
1019                                 ssid = ssid->next;
1020                                 break;
1021                         }
1022                         ssid = ssid->next;
1023                 }
1024         }
1025
1026         if (!ssid || !wpa_s->prev_sched_ssid) {
1027                 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
1028                 if (wpa_s->conf->sched_scan_interval)
1029                         wpa_s->sched_scan_interval =
1030                                 wpa_s->conf->sched_scan_interval;
1031                 if (wpa_s->sched_scan_interval == 0)
1032                         wpa_s->sched_scan_interval = 10;
1033                 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1034                 wpa_s->first_sched_scan = 1;
1035                 ssid = wpa_s->conf->ssid;
1036                 wpa_s->prev_sched_ssid = ssid;
1037         }
1038
1039         if (wildcard) {
1040                 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1041                 params.num_ssids++;
1042         }
1043
1044         while (ssid) {
1045                 if (wpas_network_disabled(wpa_s, ssid))
1046                         goto next;
1047
1048                 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1049                     params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1050                         wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1051                                 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1052                         os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1053                                   ssid->ssid, ssid->ssid_len);
1054                         params.filter_ssids[params.num_filter_ssids].ssid_len =
1055                                 ssid->ssid_len;
1056                         params.num_filter_ssids++;
1057                 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1058                 {
1059                         wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1060                                 "filter for sched_scan - drop filter");
1061                         os_free(params.filter_ssids);
1062                         params.filter_ssids = NULL;
1063                         params.num_filter_ssids = 0;
1064                 }
1065
1066                 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1067                         if (params.num_ssids == max_sched_scan_ssids)
1068                                 break; /* only room for broadcast SSID */
1069                         wpa_dbg(wpa_s, MSG_DEBUG,
1070                                 "add to active scan ssid: %s",
1071                                 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1072                         params.ssids[params.num_ssids].ssid =
1073                                 ssid->ssid;
1074                         params.ssids[params.num_ssids].ssid_len =
1075                                 ssid->ssid_len;
1076                         params.num_ssids++;
1077                         if (params.num_ssids >= max_sched_scan_ssids) {
1078                                 wpa_s->prev_sched_ssid = ssid;
1079                                 do {
1080                                         ssid = ssid->next;
1081                                 } while (ssid &&
1082                                          (wpas_network_disabled(wpa_s, ssid) ||
1083                                           !ssid->scan_ssid));
1084                                 break;
1085                         }
1086                 }
1087
1088         next:
1089                 wpa_s->prev_sched_ssid = ssid;
1090                 ssid = ssid->next;
1091         }
1092
1093         if (params.num_filter_ssids == 0) {
1094                 os_free(params.filter_ssids);
1095                 params.filter_ssids = NULL;
1096         }
1097
1098         extra_ie = wpa_supplicant_extra_ies(wpa_s);
1099         if (extra_ie) {
1100                 params.extra_ies = wpabuf_head(extra_ie);
1101                 params.extra_ies_len = wpabuf_len(extra_ie);
1102         }
1103
1104         scan_params = &params;
1105
1106 scan:
1107         if (ssid || !wpa_s->first_sched_scan) {
1108                 wpa_dbg(wpa_s, MSG_DEBUG,
1109                         "Starting sched scan: interval %d timeout %d",
1110                         wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
1111         } else {
1112                 wpa_dbg(wpa_s, MSG_DEBUG,
1113                         "Starting sched scan: interval %d (no timeout)",
1114                         wpa_s->sched_scan_interval);
1115         }
1116
1117         wpa_setband_scan_freqs(wpa_s, scan_params);
1118
1119         ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params,
1120                                               wpa_s->sched_scan_interval);
1121         wpabuf_free(extra_ie);
1122         os_free(params.filter_ssids);
1123         if (ret) {
1124                 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1125                 if (prev_state != wpa_s->wpa_state)
1126                         wpa_supplicant_set_state(wpa_s, prev_state);
1127                 return ret;
1128         }
1129
1130         /* If we have more SSIDs to scan, add a timeout so we scan them too */
1131         if (ssid || !wpa_s->first_sched_scan) {
1132                 wpa_s->sched_scan_timed_out = 0;
1133                 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1134                                        wpa_supplicant_sched_scan_timeout,
1135                                        wpa_s, NULL);
1136                 wpa_s->first_sched_scan = 0;
1137                 wpa_s->sched_scan_timeout /= 2;
1138                 wpa_s->sched_scan_interval *= 2;
1139                 if (wpa_s->sched_scan_timeout < wpa_s->sched_scan_interval) {
1140                         wpa_s->sched_scan_interval = 10;
1141                         wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1142                 }
1143         }
1144
1145         /* If there is no more ssids, start next time from the beginning */
1146         if (!ssid)
1147                 wpa_s->prev_sched_ssid = NULL;
1148
1149         return 0;
1150 }
1151
1152
1153 /**
1154  * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1155  * @wpa_s: Pointer to wpa_supplicant data
1156  *
1157  * This function is used to cancel a scan request scheduled with
1158  * wpa_supplicant_req_scan().
1159  */
1160 void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1161 {
1162         wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1163         eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
1164         wpas_p2p_continue_after_scan(wpa_s);
1165 }
1166
1167
1168 /**
1169  * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
1170  * @wpa_s: Pointer to wpa_supplicant data
1171  *
1172  * This function is used to stop a delayed scheduled scan.
1173  */
1174 void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
1175 {
1176         if (!wpa_s->sched_scan_supported)
1177                 return;
1178
1179         wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
1180         eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
1181                              wpa_s, NULL);
1182 }
1183
1184
1185 /**
1186  * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1187  * @wpa_s: Pointer to wpa_supplicant data
1188  *
1189  * This function is used to stop a periodic scheduled scan.
1190  */
1191 void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1192 {
1193         if (!wpa_s->sched_scanning)
1194                 return;
1195
1196         wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1197         eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1198         wpa_supplicant_stop_sched_scan(wpa_s);
1199 }
1200
1201
1202 /**
1203  * wpa_supplicant_notify_scanning - Indicate possible scan state change
1204  * @wpa_s: Pointer to wpa_supplicant data
1205  * @scanning: Whether scanning is currently in progress
1206  *
1207  * This function is to generate scanning notifycations. It is called whenever
1208  * there may have been a change in scanning (scan started, completed, stopped).
1209  * wpas_notify_scanning() is called whenever the scanning state changed from the
1210  * previously notified state.
1211  */
1212 void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1213                                     int scanning)
1214 {
1215         if (wpa_s->scanning != scanning) {
1216                 wpa_s->scanning = scanning;
1217                 wpas_notify_scanning(wpa_s);
1218         }
1219 }
1220
1221
1222 static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1223 {
1224         int rate = 0;
1225         const u8 *ie;
1226         int i;
1227
1228         ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1229         for (i = 0; ie && i < ie[1]; i++) {
1230                 if ((ie[i + 2] & 0x7f) > rate)
1231                         rate = ie[i + 2] & 0x7f;
1232         }
1233
1234         ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1235         for (i = 0; ie && i < ie[1]; i++) {
1236                 if ((ie[i + 2] & 0x7f) > rate)
1237                         rate = ie[i + 2] & 0x7f;
1238         }
1239
1240         return rate;
1241 }
1242
1243
1244 /**
1245  * wpa_scan_get_ie - Fetch a specified information element from a scan result
1246  * @res: Scan result entry
1247  * @ie: Information element identitifier (WLAN_EID_*)
1248  * Returns: Pointer to the information element (id field) or %NULL if not found
1249  *
1250  * This function returns the first matching information element in the scan
1251  * result.
1252  */
1253 const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1254 {
1255         const u8 *end, *pos;
1256
1257         pos = (const u8 *) (res + 1);
1258         end = pos + res->ie_len;
1259
1260         while (pos + 1 < end) {
1261                 if (pos + 2 + pos[1] > end)
1262                         break;
1263                 if (pos[0] == ie)
1264                         return pos;
1265                 pos += 2 + pos[1];
1266         }
1267
1268         return NULL;
1269 }
1270
1271
1272 /**
1273  * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1274  * @res: Scan result entry
1275  * @vendor_type: Vendor type (four octets starting the IE payload)
1276  * Returns: Pointer to the information element (id field) or %NULL if not found
1277  *
1278  * This function returns the first matching information element in the scan
1279  * result.
1280  */
1281 const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1282                                   u32 vendor_type)
1283 {
1284         const u8 *end, *pos;
1285
1286         pos = (const u8 *) (res + 1);
1287         end = pos + res->ie_len;
1288
1289         while (pos + 1 < end) {
1290                 if (pos + 2 + pos[1] > end)
1291                         break;
1292                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1293                     vendor_type == WPA_GET_BE32(&pos[2]))
1294                         return pos;
1295                 pos += 2 + pos[1];
1296         }
1297
1298         return NULL;
1299 }
1300
1301
1302 /**
1303  * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
1304  * @res: Scan result entry
1305  * @vendor_type: Vendor type (four octets starting the IE payload)
1306  * Returns: Pointer to the information element (id field) or %NULL if not found
1307  *
1308  * This function returns the first matching information element in the scan
1309  * result.
1310  *
1311  * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
1312  * from Beacon frames instead of either Beacon or Probe Response frames.
1313  */
1314 const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
1315                                          u32 vendor_type)
1316 {
1317         const u8 *end, *pos;
1318
1319         if (res->beacon_ie_len == 0)
1320                 return NULL;
1321
1322         pos = (const u8 *) (res + 1);
1323         pos += res->ie_len;
1324         end = pos + res->beacon_ie_len;
1325
1326         while (pos + 1 < end) {
1327                 if (pos + 2 + pos[1] > end)
1328                         break;
1329                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1330                     vendor_type == WPA_GET_BE32(&pos[2]))
1331                         return pos;
1332                 pos += 2 + pos[1];
1333         }
1334
1335         return NULL;
1336 }
1337
1338
1339 /**
1340  * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
1341  * @res: Scan result entry
1342  * @vendor_type: Vendor type (four octets starting the IE payload)
1343  * Returns: Pointer to the information element payload or %NULL if not found
1344  *
1345  * This function returns concatenated payload of possibly fragmented vendor
1346  * specific information elements in the scan result. The caller is responsible
1347  * for freeing the returned buffer.
1348  */
1349 struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1350                                              u32 vendor_type)
1351 {
1352         struct wpabuf *buf;
1353         const u8 *end, *pos;
1354
1355         buf = wpabuf_alloc(res->ie_len);
1356         if (buf == NULL)
1357                 return NULL;
1358
1359         pos = (const u8 *) (res + 1);
1360         end = pos + res->ie_len;
1361
1362         while (pos + 1 < end) {
1363                 if (pos + 2 + pos[1] > end)
1364                         break;
1365                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1366                     vendor_type == WPA_GET_BE32(&pos[2]))
1367                         wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1368                 pos += 2 + pos[1];
1369         }
1370
1371         if (wpabuf_len(buf) == 0) {
1372                 wpabuf_free(buf);
1373                 buf = NULL;
1374         }
1375
1376         return buf;
1377 }
1378
1379
1380 /*
1381  * Channels with a great SNR can operate at full rate. What is a great SNR?
1382  * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1383  * rule of thumb is that any SNR above 20 is good." This one
1384  * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1385  * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1386  * conservative value.
1387  */
1388 #define GREAT_SNR 30
1389
1390 /* Compare function for sorting scan results. Return >0 if @b is considered
1391  * better. */
1392 static int wpa_scan_result_compar(const void *a, const void *b)
1393 {
1394 #define IS_5GHZ(n) (n > 4000)
1395 #define MIN(a,b) a < b ? a : b
1396         struct wpa_scan_res **_wa = (void *) a;
1397         struct wpa_scan_res **_wb = (void *) b;
1398         struct wpa_scan_res *wa = *_wa;
1399         struct wpa_scan_res *wb = *_wb;
1400         int wpa_a, wpa_b, maxrate_a, maxrate_b;
1401         int snr_a, snr_b;
1402
1403         /* WPA/WPA2 support preferred */
1404         wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1405                 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1406         wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1407                 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1408
1409         if (wpa_b && !wpa_a)
1410                 return 1;
1411         if (!wpa_b && wpa_a)
1412                 return -1;
1413
1414         /* privacy support preferred */
1415         if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1416             (wb->caps & IEEE80211_CAP_PRIVACY))
1417                 return 1;
1418         if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1419             (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1420                 return -1;
1421
1422         if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
1423             !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
1424                 snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
1425                 snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
1426         } else {
1427                 /* Not suitable information to calculate SNR, so use level */
1428                 snr_a = wa->level;
1429                 snr_b = wb->level;
1430         }
1431
1432         /* best/max rate preferred if SNR close enough */
1433         if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
1434             (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
1435                 maxrate_a = wpa_scan_get_max_rate(wa);
1436                 maxrate_b = wpa_scan_get_max_rate(wb);
1437                 if (maxrate_a != maxrate_b)
1438                         return maxrate_b - maxrate_a;
1439                 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1440                         return IS_5GHZ(wa->freq) ? -1 : 1;
1441         }
1442
1443         /* use freq for channel preference */
1444
1445         /* all things being equal, use SNR; if SNRs are
1446          * identical, use quality values since some drivers may only report
1447          * that value and leave the signal level zero */
1448         if (snr_b == snr_a)
1449                 return wb->qual - wa->qual;
1450         return snr_b - snr_a;
1451 #undef MIN
1452 #undef IS_5GHZ
1453 }
1454
1455
1456 #ifdef CONFIG_WPS
1457 /* Compare function for sorting scan results when searching a WPS AP for
1458  * provisioning. Return >0 if @b is considered better. */
1459 static int wpa_scan_result_wps_compar(const void *a, const void *b)
1460 {
1461         struct wpa_scan_res **_wa = (void *) a;
1462         struct wpa_scan_res **_wb = (void *) b;
1463         struct wpa_scan_res *wa = *_wa;
1464         struct wpa_scan_res *wb = *_wb;
1465         int uses_wps_a, uses_wps_b;
1466         struct wpabuf *wps_a, *wps_b;
1467         int res;
1468
1469         /* Optimization - check WPS IE existence before allocated memory and
1470          * doing full reassembly. */
1471         uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1472         uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1473         if (uses_wps_a && !uses_wps_b)
1474                 return -1;
1475         if (!uses_wps_a && uses_wps_b)
1476                 return 1;
1477
1478         if (uses_wps_a && uses_wps_b) {
1479                 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1480                 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1481                 res = wps_ap_priority_compar(wps_a, wps_b);
1482                 wpabuf_free(wps_a);
1483                 wpabuf_free(wps_b);
1484                 if (res)
1485                         return res;
1486         }
1487
1488         /*
1489          * Do not use current AP security policy as a sorting criteria during
1490          * WPS provisioning step since the AP may get reconfigured at the
1491          * completion of provisioning.
1492          */
1493
1494         /* all things being equal, use signal level; if signal levels are
1495          * identical, use quality values since some drivers may only report
1496          * that value and leave the signal level zero */
1497         if (wb->level == wa->level)
1498                 return wb->qual - wa->qual;
1499         return wb->level - wa->level;
1500 }
1501 #endif /* CONFIG_WPS */
1502
1503
1504 static void dump_scan_res(struct wpa_scan_results *scan_res)
1505 {
1506 #ifndef CONFIG_NO_STDOUT_DEBUG
1507         size_t i;
1508
1509         if (scan_res->res == NULL || scan_res->num == 0)
1510                 return;
1511
1512         wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1513
1514         for (i = 0; i < scan_res->num; i++) {
1515                 struct wpa_scan_res *r = scan_res->res[i];
1516                 u8 *pos;
1517                 if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
1518                     == WPA_SCAN_LEVEL_DBM) {
1519                         int snr = r->level - r->noise;
1520                         wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1521                                    "noise=%d level=%d snr=%d%s flags=0x%x "
1522                                    "age=%u",
1523                                    MAC2STR(r->bssid), r->freq, r->qual,
1524                                    r->noise, r->level, snr,
1525                                    snr >= GREAT_SNR ? "*" : "", r->flags,
1526                                    r->age);
1527                 } else {
1528                         wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1529                                    "noise=%d level=%d flags=0x%x age=%u",
1530                                    MAC2STR(r->bssid), r->freq, r->qual,
1531                                    r->noise, r->level, r->flags, r->age);
1532                 }
1533                 pos = (u8 *) (r + 1);
1534                 if (r->ie_len)
1535                         wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
1536                 pos += r->ie_len;
1537                 if (r->beacon_ie_len)
1538                         wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
1539                                     pos, r->beacon_ie_len);
1540         }
1541 #endif /* CONFIG_NO_STDOUT_DEBUG */
1542 }
1543
1544
1545 /**
1546  * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
1547  * @wpa_s: Pointer to wpa_supplicant data
1548  * @bssid: BSSID to check
1549  * Returns: 0 if the BSSID is filtered or 1 if not
1550  *
1551  * This function is used to filter out specific BSSIDs from scan reslts mainly
1552  * for testing purposes (SET bssid_filter ctrl_iface command).
1553  */
1554 int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
1555                                       const u8 *bssid)
1556 {
1557         size_t i;
1558
1559         if (wpa_s->bssid_filter == NULL)
1560                 return 1;
1561
1562         for (i = 0; i < wpa_s->bssid_filter_count; i++) {
1563                 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
1564                               ETH_ALEN) == 0)
1565                         return 1;
1566         }
1567
1568         return 0;
1569 }
1570
1571
1572 static void filter_scan_res(struct wpa_supplicant *wpa_s,
1573                             struct wpa_scan_results *res)
1574 {
1575         size_t i, j;
1576
1577         if (wpa_s->bssid_filter == NULL)
1578                 return;
1579
1580         for (i = 0, j = 0; i < res->num; i++) {
1581                 if (wpa_supplicant_filter_bssid_match(wpa_s,
1582                                                       res->res[i]->bssid)) {
1583                         res->res[j++] = res->res[i];
1584                 } else {
1585                         os_free(res->res[i]);
1586                         res->res[i] = NULL;
1587                 }
1588         }
1589
1590         if (res->num != j) {
1591                 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
1592                            (int) (res->num - j));
1593                 res->num = j;
1594         }
1595 }
1596
1597
1598 /**
1599  * wpa_supplicant_get_scan_results - Get scan results
1600  * @wpa_s: Pointer to wpa_supplicant data
1601  * @info: Information about what was scanned or %NULL if not available
1602  * @new_scan: Whether a new scan was performed
1603  * Returns: Scan results, %NULL on failure
1604  *
1605  * This function request the current scan results from the driver and updates
1606  * the local BSS list wpa_s->bss. The caller is responsible for freeing the
1607  * results with wpa_scan_results_free().
1608  */
1609 struct wpa_scan_results *
1610 wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
1611                                 struct scan_info *info, int new_scan)
1612 {
1613         struct wpa_scan_results *scan_res;
1614         size_t i;
1615         int (*compar)(const void *, const void *) = wpa_scan_result_compar;
1616
1617         scan_res = wpa_drv_get_scan_results2(wpa_s);
1618         if (scan_res == NULL) {
1619                 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
1620                 return NULL;
1621         }
1622         if (scan_res->fetch_time.sec == 0) {
1623                 /*
1624                  * Make sure we have a valid timestamp if the driver wrapper
1625                  * does not set this.
1626                  */
1627                 os_get_reltime(&scan_res->fetch_time);
1628         }
1629         filter_scan_res(wpa_s, scan_res);
1630
1631 #ifdef CONFIG_WPS
1632         if (wpas_wps_in_progress(wpa_s)) {
1633                 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
1634                         "provisioning rules");
1635                 compar = wpa_scan_result_wps_compar;
1636         }
1637 #endif /* CONFIG_WPS */
1638
1639         qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
1640               compar);
1641         dump_scan_res(scan_res);
1642
1643         wpa_bss_update_start(wpa_s);
1644         for (i = 0; i < scan_res->num; i++)
1645                 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
1646                                         &scan_res->fetch_time);
1647         wpa_bss_update_end(wpa_s, info, new_scan);
1648
1649         return scan_res;
1650 }
1651
1652
1653 /**
1654  * wpa_supplicant_update_scan_results - Update scan results from the driver
1655  * @wpa_s: Pointer to wpa_supplicant data
1656  * Returns: 0 on success, -1 on failure
1657  *
1658  * This function updates the BSS table within wpa_supplicant based on the
1659  * currently available scan results from the driver without requesting a new
1660  * scan. This is used in cases where the driver indicates an association
1661  * (including roaming within ESS) and wpa_supplicant does not yet have the
1662  * needed information to complete the connection (e.g., to perform validation
1663  * steps in 4-way handshake).
1664  */
1665 int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
1666 {
1667         struct wpa_scan_results *scan_res;
1668         scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
1669         if (scan_res == NULL)
1670                 return -1;
1671         wpa_scan_results_free(scan_res);
1672
1673         return 0;
1674 }
1675
1676
1677 /**
1678  * scan_only_handler - Reports scan results
1679  */
1680 void scan_only_handler(struct wpa_supplicant *wpa_s,
1681                        struct wpa_scan_results *scan_res)
1682 {
1683         wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
1684         if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1685             wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
1686                 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
1687                              wpa_s->manual_scan_id);
1688                 wpa_s->manual_scan_use_id = 0;
1689         } else {
1690                 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1691         }
1692         wpas_notify_scan_results(wpa_s);
1693         wpas_notify_scan_done(wpa_s, 1);
1694 }
1695
1696
1697 int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
1698 {
1699         return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
1700 }