OSDN Git Service

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