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