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