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