]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/scan.c
wpa_supplicant: Share a single get_mode() implementation
[thirdparty/hostap.git] / wpa_supplicant / scan.c
CommitLineData
6fc6879b
JM
1/*
2 * WPA Supplicant - Scanning
dd43aaa5 3 * Copyright (c) 2003-2014, 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
JM
23#include "bss.h"
24#include "scan.h"
fbca4c89 25#include "mesh.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;
44b9ea5b
JM
99
100 if (wpa_s->p2p_mgmt)
101 return 0; /* no normal network profiles on p2p_mgmt interface */
102
e76baaac 103 while (ssid) {
349493bd 104 if (!wpas_network_disabled(wpa_s, ssid))
5471c343 105 count++;
d4534bde
JM
106 else
107 disabled++;
e76baaac
JM
108 ssid = ssid->next;
109 }
4d5bda5f
JM
110 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
111 wpa_s->conf->auto_interworking)
112 count++;
d4534bde
JM
113 if (count == 0 && disabled > 0) {
114 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks (%d disabled "
115 "networks)", disabled);
116 }
5471c343 117 return count;
e76baaac
JM
118}
119
120
121static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
122 struct wpa_ssid *ssid)
123{
124 while (ssid) {
349493bd 125 if (!wpas_network_disabled(wpa_s, ssid))
e76baaac
JM
126 break;
127 ssid = ssid->next;
128 }
129
130 /* ap_scan=2 mode - try to associate with each SSID. */
131 if (ssid == NULL) {
f049052b
BG
132 wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
133 "end of scan list - go back to beginning");
ba2a573c 134 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
e76baaac
JM
135 wpa_supplicant_req_scan(wpa_s, 0, 0);
136 return;
137 }
138 if (ssid->next) {
139 /* Continue from the next SSID on the next attempt. */
140 wpa_s->prev_scan_ssid = ssid;
141 } else {
142 /* Start from the beginning of the SSID list. */
ba2a573c 143 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
e76baaac
JM
144 }
145 wpa_supplicant_associate(wpa_s, NULL, ssid);
146}
147
148
d12a51b5 149static void wpas_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
60b94c98 150{
d12a51b5
JM
151 struct wpa_supplicant *wpa_s = work->wpa_s;
152 struct wpa_driver_scan_params *params = work->ctx;
60b94c98
JM
153 int ret;
154
d12a51b5 155 if (deinit) {
b3253ebb
AO
156 if (!work->started) {
157 wpa_scan_free_params(params);
158 return;
159 }
160 wpa_supplicant_notify_scanning(wpa_s, 0);
161 wpas_notify_scan_done(wpa_s, 0);
162 wpa_s->scan_work = NULL;
d12a51b5
JM
163 return;
164 }
165
c267753b
JM
166 if (wpas_update_random_addr_disassoc(wpa_s) < 0) {
167 wpa_msg(wpa_s, MSG_INFO,
168 "Failed to assign random MAC address for a scan");
169 radio_work_done(work);
170 return;
171 }
172
60b94c98
JM
173 wpa_supplicant_notify_scanning(wpa_s, 1);
174
2dbe63ad
JM
175 if (wpa_s->clear_driver_scan_cache) {
176 wpa_printf(MSG_DEBUG,
177 "Request driver to clear scan cache due to local BSS flush");
949938aa 178 params->only_new_results = 1;
2dbe63ad 179 }
17fbb751 180 ret = wpa_drv_scan(wpa_s, params);
d12a51b5
JM
181 wpa_scan_free_params(params);
182 work->ctx = NULL;
60b94c98 183 if (ret) {
2d9c99e3
JM
184 int retry = wpa_s->last_scan_req != MANUAL_SCAN_REQ;
185
186 if (wpa_s->disconnected)
187 retry = 0;
188
60b94c98
JM
189 wpa_supplicant_notify_scanning(wpa_s, 0);
190 wpas_notify_scan_done(wpa_s, 0);
2d9c99e3
JM
191 if (wpa_s->wpa_state == WPA_SCANNING)
192 wpa_supplicant_set_state(wpa_s,
193 wpa_s->scan_prev_wpa_state);
194 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=%d%s",
195 ret, retry ? " retry=1" : "");
d12a51b5 196 radio_work_done(work);
2d9c99e3
JM
197
198 if (retry) {
199 /* Restore scan_req since we will try to scan again */
200 wpa_s->scan_req = wpa_s->last_scan_req;
201 wpa_supplicant_req_scan(wpa_s, 1, 0);
202 }
d12a51b5 203 return;
0b7a25c0 204 }
60b94c98 205
d12a51b5
JM
206 os_get_reltime(&wpa_s->scan_trigger_time);
207 wpa_s->scan_runs++;
208 wpa_s->normal_scans++;
209 wpa_s->own_scan_requested = 1;
210 wpa_s->clear_driver_scan_cache = 0;
211 wpa_s->scan_work = work;
212}
213
214
215/**
216 * wpa_supplicant_trigger_scan - Request driver to start a scan
217 * @wpa_s: Pointer to wpa_supplicant data
218 * @params: Scan parameters
219 * Returns: 0 on success, -1 on failure
220 */
221int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
222 struct wpa_driver_scan_params *params)
223{
224 struct wpa_driver_scan_params *ctx;
225
226 if (wpa_s->scan_work) {
227 wpa_dbg(wpa_s, MSG_INFO, "Reject scan trigger since one is already pending");
228 return -1;
229 }
230
231 ctx = wpa_scan_clone_params(params);
232 if (ctx == NULL)
233 return -1;
234
235 if (radio_add_work(wpa_s, 0, "scan", 0, wpas_trigger_scan_cb, ctx) < 0)
236 {
237 wpa_scan_free_params(ctx);
238 return -1;
239 }
240
241 return 0;
60b94c98
JM
242}
243
244
6a90053c
LC
245static void
246wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
247{
248 struct wpa_supplicant *wpa_s = eloop_ctx;
249
250 wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
251
252 if (wpa_supplicant_req_sched_scan(wpa_s))
253 wpa_supplicant_req_scan(wpa_s, 0, 0);
254}
255
256
cbdf3507
LC
257static void
258wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
259{
260 struct wpa_supplicant *wpa_s = eloop_ctx;
261
262 wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
263
264 wpa_s->sched_scan_timed_out = 1;
265 wpa_supplicant_cancel_sched_scan(wpa_s);
266}
267
268
f86d282f 269int wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
32c02261 270 struct wpa_driver_scan_params *params)
cbdf3507
LC
271{
272 int ret;
273
cbdf3507 274 wpa_supplicant_notify_scanning(wpa_s, 1);
09ea4309 275 ret = wpa_drv_sched_scan(wpa_s, params);
cbdf3507
LC
276 if (ret)
277 wpa_supplicant_notify_scanning(wpa_s, 0);
278 else
279 wpa_s->sched_scanning = 1;
280
281 return ret;
282}
283
284
f86d282f 285int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
cbdf3507
LC
286{
287 int ret;
288
289 ret = wpa_drv_stop_sched_scan(wpa_s);
290 if (ret) {
291 wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
292 /* TODO: what to do if stopping fails? */
293 return -1;
294 }
295
296 return ret;
297}
298
299
3812464c
JM
300static struct wpa_driver_scan_filter *
301wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
302{
303 struct wpa_driver_scan_filter *ssids;
304 struct wpa_ssid *ssid;
305 size_t count;
306
307 *num_ssids = 0;
308 if (!conf->filter_ssids)
309 return NULL;
310
311 for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
312 if (ssid->ssid && ssid->ssid_len)
313 count++;
314 }
315 if (count == 0)
316 return NULL;
faebdeaa 317 ssids = os_calloc(count, sizeof(struct wpa_driver_scan_filter));
3812464c
JM
318 if (ssids == NULL)
319 return NULL;
320
321 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
322 if (!ssid->ssid || !ssid->ssid_len)
323 continue;
324 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
325 ssids[*num_ssids].ssid_len = ssid->ssid_len;
326 (*num_ssids)++;
327 }
328
329 return ssids;
330}
331
332
5f738a21
LC
333static void wpa_supplicant_optimize_freqs(
334 struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
335{
336#ifdef CONFIG_P2P
337 if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
338 wpa_s->go_params) {
339 /* Optimize provisioning state scan based on GO information */
340 if (wpa_s->p2p_in_provisioning < 5 &&
341 wpa_s->go_params->freq > 0) {
342 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
343 "preferred frequency %d MHz",
344 wpa_s->go_params->freq);
faebdeaa 345 params->freqs = os_calloc(2, sizeof(int));
5f738a21
LC
346 if (params->freqs)
347 params->freqs[0] = wpa_s->go_params->freq;
348 } else if (wpa_s->p2p_in_provisioning < 8 &&
349 wpa_s->go_params->freq_list[0]) {
350 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
351 "channels");
352 int_array_concat(&params->freqs,
353 wpa_s->go_params->freq_list);
354 if (params->freqs)
355 int_array_sort_unique(params->freqs);
356 }
357 wpa_s->p2p_in_provisioning++;
358 }
41d5ce9e
RR
359
360 if (params->freqs == NULL && wpa_s->p2p_in_invitation) {
361 /*
362 * Optimize scan based on GO information during persistent
363 * group reinvocation
364 */
28fa4eb2 365 if (wpa_s->p2p_in_invitation < 5 &&
41d5ce9e
RR
366 wpa_s->p2p_invite_go_freq > 0) {
367 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO preferred frequency %d MHz during invitation",
368 wpa_s->p2p_invite_go_freq);
faebdeaa 369 params->freqs = os_calloc(2, sizeof(int));
41d5ce9e
RR
370 if (params->freqs)
371 params->freqs[0] = wpa_s->p2p_invite_go_freq;
372 }
373 wpa_s->p2p_in_invitation++;
374 if (wpa_s->p2p_in_invitation > 20) {
375 /*
376 * This should not really happen since the variable is
377 * cleared on group removal, but if it does happen, make
378 * sure we do not get stuck in special invitation scan
379 * mode.
380 */
381 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Clear p2p_in_invitation");
382 wpa_s->p2p_in_invitation = 0;
383 }
384 }
5f738a21
LC
385#endif /* CONFIG_P2P */
386
387#ifdef CONFIG_WPS
388 if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
389 /*
390 * Optimize post-provisioning scan based on channel used
391 * during provisioning.
392 */
393 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
394 "that was used during provisioning", wpa_s->wps_freq);
faebdeaa 395 params->freqs = os_calloc(2, sizeof(int));
5f738a21
LC
396 if (params->freqs)
397 params->freqs[0] = wpa_s->wps_freq;
398 wpa_s->after_wps--;
662b40b1
JM
399 } else if (wpa_s->after_wps)
400 wpa_s->after_wps--;
5f738a21 401
1e7fb4f1
JM
402 if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
403 {
404 /* Optimize provisioning scan based on already known channel */
405 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
406 wpa_s->wps_freq);
faebdeaa 407 params->freqs = os_calloc(2, sizeof(int));
1e7fb4f1
JM
408 if (params->freqs)
409 params->freqs[0] = wpa_s->wps_freq;
410 wpa_s->known_wps_freq = 0; /* only do this once */
411 }
5f738a21
LC
412#endif /* CONFIG_WPS */
413}
414
415
46ee0427
JM
416#ifdef CONFIG_INTERWORKING
417static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
418 struct wpabuf *buf)
419{
46ee0427
JM
420 wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
421 wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
422 1 + ETH_ALEN);
11540c0b 423 wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
46ee0427
JM
424 /* No Venue Info */
425 if (!is_zero_ether_addr(wpa_s->conf->hessid))
426 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
427}
428#endif /* CONFIG_INTERWORKING */
429
430
a13e07ec 431static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
6fc6879b 432{
46ee0427 433 struct wpabuf *extra_ie = NULL;
ad443097
IP
434 u8 ext_capab[18];
435 int ext_capab_len;
b01c18a8 436#ifdef CONFIG_WPS
509a3972 437 int wps = 0;
f90c86d4 438 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
46ee0427
JM
439#endif /* CONFIG_WPS */
440
ad443097
IP
441 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
442 sizeof(ext_capab));
443 if (ext_capab_len > 0 &&
444 wpabuf_resize(&extra_ie, ext_capab_len) == 0)
445 wpabuf_put_data(extra_ie, ext_capab, ext_capab_len);
446
46ee0427
JM
447#ifdef CONFIG_INTERWORKING
448 if (wpa_s->conf->interworking &&
449 wpabuf_resize(&extra_ie, 100) == 0)
450 wpas_add_interworking_elements(wpa_s, extra_ie);
451#endif /* CONFIG_INTERWORKING */
5f738a21 452
46ee0427 453#ifdef CONFIG_WPS
5f738a21
LC
454 wps = wpas_wps_in_use(wpa_s, &req_type);
455
456 if (wps) {
46ee0427 457 struct wpabuf *wps_ie;
360182ed
JM
458 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
459 DEV_PW_DEFAULT,
460 &wpa_s->wps->dev,
5f738a21
LC
461 wpa_s->wps->uuid, req_type,
462 0, NULL);
463 if (wps_ie) {
46ee0427
JM
464 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
465 wpabuf_put_buf(extra_ie, wps_ie);
466 wpabuf_free(wps_ie);
5f738a21
LC
467 }
468 }
469
470#ifdef CONFIG_P2P
46ee0427 471 if (wps) {
5f738a21 472 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
46ee0427
JM
473 if (wpabuf_resize(&extra_ie, ielen) == 0)
474 wpas_p2p_scan_ie(wpa_s, extra_ie);
5f738a21
LC
475 }
476#endif /* CONFIG_P2P */
477
fbca4c89
JA
478 wpa_supplicant_mesh_add_scan_ie(wpa_s, &extra_ie);
479
b01c18a8 480#endif /* CONFIG_WPS */
5f738a21 481
0f105f9e
JM
482#ifdef CONFIG_HS20
483 if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 7) == 0)
f9cd147d 484 wpas_hs20_add_indication(extra_ie, -1);
0f105f9e
JM
485#endif /* CONFIG_HS20 */
486
b36a3a65
AN
487#ifdef CONFIG_FST
488 if (wpa_s->fst_ies &&
489 wpabuf_resize(&extra_ie, wpabuf_len(wpa_s->fst_ies)) == 0)
490 wpabuf_put_buf(extra_ie, wpa_s->fst_ies);
491#endif /* CONFIG_FST */
492
46ee0427 493 return extra_ie;
5f738a21
LC
494}
495
496
5cc70322
JM
497#ifdef CONFIG_P2P
498
499/*
500 * Check whether there are any enabled networks or credentials that could be
501 * used for a non-P2P connection.
502 */
503static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
504{
505 struct wpa_ssid *ssid;
506
507 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
508 if (wpas_network_disabled(wpa_s, ssid))
509 continue;
510 if (!ssid->p2p_group)
511 return 1;
512 }
513
514 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
515 wpa_s->conf->auto_interworking)
516 return 1;
517
518 return 0;
519}
520
6124e858 521#endif /* CONFIG_P2P */
5cc70322 522
5cc70322 523
faf9a858
JM
524static void wpa_setband_scan_freqs_list(struct wpa_supplicant *wpa_s,
525 enum hostapd_hw_mode band,
526 struct wpa_driver_scan_params *params)
527{
528 /* Include only supported channels for the specified band */
529 struct hostapd_hw_modes *mode;
530 int count, i;
531
532 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band);
533 if (mode == NULL) {
534 /* No channels supported in this band - use empty list */
535 params->freqs = os_zalloc(sizeof(int));
536 return;
537 }
538
faebdeaa 539 params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
faf9a858
JM
540 if (params->freqs == NULL)
541 return;
542 for (count = 0, i = 0; i < mode->num_channels; i++) {
543 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
544 continue;
545 params->freqs[count++] = mode->channels[i].freq;
546 }
547}
548
549
550static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
551 struct wpa_driver_scan_params *params)
552{
553 if (wpa_s->hw.modes == NULL)
554 return; /* unknown what channels the driver supports */
555 if (params->freqs)
556 return; /* already using a limited channel set */
557 if (wpa_s->setband == WPA_SETBAND_5G)
558 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
559 params);
560 else if (wpa_s->setband == WPA_SETBAND_2G)
561 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G,
562 params);
563}
564
565
6891f0e6
LJ
566static void wpa_set_scan_ssids(struct wpa_supplicant *wpa_s,
567 struct wpa_driver_scan_params *params,
568 size_t max_ssids)
569{
570 unsigned int i;
571 struct wpa_ssid *ssid;
572
573 for (i = 0; i < wpa_s->scan_id_count; i++) {
574 unsigned int j;
575
576 ssid = wpa_config_get_network(wpa_s->conf, wpa_s->scan_id[i]);
577 if (!ssid || !ssid->scan_ssid)
578 continue;
579
580 for (j = 0; j < params->num_ssids; j++) {
581 if (params->ssids[j].ssid_len == ssid->ssid_len &&
582 params->ssids[j].ssid &&
583 os_memcmp(params->ssids[j].ssid, ssid->ssid,
584 ssid->ssid_len) == 0)
585 break;
586 }
587 if (j < params->num_ssids)
588 continue; /* already in the list */
589
590 if (params->num_ssids + 1 > max_ssids) {
591 wpa_printf(MSG_DEBUG,
592 "Over max scan SSIDs for manual request");
593 break;
594 }
595
596 wpa_printf(MSG_DEBUG, "Scan SSID (manual request): %s",
597 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
598 params->ssids[params->num_ssids].ssid = ssid->ssid;
599 params->ssids[params->num_ssids].ssid_len = ssid->ssid_len;
600 params->num_ssids++;
601 }
602
603 wpa_s->scan_id_count = 0;
604}
605
606
a80651d0
KV
607static int wpa_set_ssids_from_scan_req(struct wpa_supplicant *wpa_s,
608 struct wpa_driver_scan_params *params,
609 size_t max_ssids)
610{
611 unsigned int i;
612
613 if (wpa_s->ssids_from_scan_req == NULL ||
614 wpa_s->num_ssids_from_scan_req == 0)
615 return 0;
616
617 if (wpa_s->num_ssids_from_scan_req > max_ssids) {
618 wpa_s->num_ssids_from_scan_req = max_ssids;
619 wpa_printf(MSG_DEBUG, "Over max scan SSIDs from scan req: %u",
620 (unsigned int) max_ssids);
621 }
622
623 for (i = 0; i < wpa_s->num_ssids_from_scan_req; i++) {
624 params->ssids[i].ssid = wpa_s->ssids_from_scan_req[i].ssid;
625 params->ssids[i].ssid_len =
626 wpa_s->ssids_from_scan_req[i].ssid_len;
627 wpa_hexdump_ascii(MSG_DEBUG, "specific SSID",
628 params->ssids[i].ssid,
629 params->ssids[i].ssid_len);
630 }
631
632 params->num_ssids = wpa_s->num_ssids_from_scan_req;
633 wpa_s->num_ssids_from_scan_req = 0;
634 return 1;
635}
636
637
5f738a21
LC
638static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
639{
640 struct wpa_supplicant *wpa_s = eloop_ctx;
641 struct wpa_ssid *ssid;
49e3eea8 642 int ret, p2p_in_prog;
b9b12d02 643 struct wpabuf *extra_ie = NULL;
e76baaac 644 struct wpa_driver_scan_params params;
7c865c68 645 struct wpa_driver_scan_params *scan_params;
e76baaac 646 size_t max_ssids;
3f9ebc43 647 int connect_without_scan = 0;
6fc6879b 648
cf70d298 649 if (wpa_s->pno || wpa_s->pno_sched_pending) {
dd271857
RM
650 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - PNO is in progress");
651 return;
652 }
653
8401a6b0 654 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
f049052b 655 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
8401a6b0
JM
656 return;
657 }
658
4115303b 659 if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
ac06fb12 660 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
3180d7a2 661 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
6fc6879b 662 return;
3180d7a2 663 }
6fc6879b 664
f86d282f
JJ
665 if (wpa_s->scanning) {
666 /*
667 * If we are already in scanning state, we shall reschedule the
668 * the incoming scan request.
669 */
670 wpa_dbg(wpa_s, MSG_DEBUG, "Already scanning - Reschedule the incoming scan req");
671 wpa_supplicant_req_scan(wpa_s, 1, 0);
672 return;
673 }
674
349493bd 675 if (!wpa_supplicant_enabled_networks(wpa_s) &&
4115303b 676 wpa_s->scan_req == NORMAL_SCAN_REQ) {
f049052b 677 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
6fc6879b
JM
678 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
679 return;
680 }
6fc6879b 681
c2a04078
JM
682 if (wpa_s->conf->ap_scan != 0 &&
683 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
f049052b
BG
684 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
685 "overriding ap_scan configuration");
6fc6879b 686 wpa_s->conf->ap_scan = 0;
8bac466b 687 wpas_notify_ap_scan_changed(wpa_s);
6fc6879b
JM
688 }
689
690 if (wpa_s->conf->ap_scan == 0) {
691 wpa_supplicant_gen_assoc_event(wpa_s);
692 return;
693 }
694
3f9ebc43 695 ssid = NULL;
a51c40aa 696 if (wpa_s->scan_req != MANUAL_SCAN_REQ &&
3f9ebc43
JM
697 wpa_s->connect_without_scan) {
698 connect_without_scan = 1;
699 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
700 if (ssid == wpa_s->connect_without_scan)
701 break;
702 }
703 }
704
49e3eea8 705 p2p_in_prog = wpas_p2p_in_progress(wpa_s);
3f9ebc43
JM
706 if (p2p_in_prog && p2p_in_prog != 2 &&
707 (!ssid ||
708 (ssid->mode != WPAS_MODE_AP && ssid->mode != WPAS_MODE_P2P_GO))) {
1b5d4714
JM
709 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan while P2P operation is in progress");
710 wpa_supplicant_req_scan(wpa_s, 5, 0);
711 return;
303f60d3 712 }
303f60d3 713
17fbb751 714 if (wpa_s->conf->ap_scan == 2)
e76baaac
JM
715 max_ssids = 1;
716 else {
717 max_ssids = wpa_s->max_scan_ssids;
718 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
719 max_ssids = WPAS_MAX_SCAN_SSIDS;
720 }
721
12455031 722 wpa_s->last_scan_req = wpa_s->scan_req;
4115303b 723 wpa_s->scan_req = NORMAL_SCAN_REQ;
e76baaac 724
3f9ebc43
JM
725 if (connect_without_scan) {
726 wpa_s->connect_without_scan = NULL;
727 if (ssid) {
728 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
729 "without scan step");
730 wpa_supplicant_associate(wpa_s, NULL, ssid);
731 return;
732 }
733 }
734
e76baaac
JM
735 os_memset(&params, 0, sizeof(params));
736
2d9c99e3 737 wpa_s->scan_prev_wpa_state = wpa_s->wpa_state;
6fc6879b
JM
738 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
739 wpa_s->wpa_state == WPA_INACTIVE)
740 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
741
7c865c68
TB
742 /*
743 * If autoscan has set its own scanning parameters
744 */
745 if (wpa_s->autoscan_params != NULL) {
746 scan_params = wpa_s->autoscan_params;
747 goto scan;
748 }
749
a80651d0
KV
750 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
751 wpa_set_ssids_from_scan_req(wpa_s, &params, max_ssids)) {
752 wpa_printf(MSG_DEBUG, "Use specific SSIDs from SCAN command");
753 goto ssid_list_set;
754 }
755
a21c05db 756#ifdef CONFIG_P2P
85ea132a 757 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
c35e35ed 758 wpa_s->go_params && !wpa_s->conf->passive_scan) {
9100b660
JM
759 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
760 wpa_s->p2p_in_provisioning,
761 wpa_s->show_group_started);
a21c05db
JM
762 params.ssids[0].ssid = wpa_s->go_params->ssid;
763 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
764 params.num_ssids = 1;
765 goto ssid_list_set;
766 }
41d5ce9e
RR
767
768 if (wpa_s->p2p_in_invitation) {
769 if (wpa_s->current_ssid) {
770 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during invitation");
771 params.ssids[0].ssid = wpa_s->current_ssid->ssid;
772 params.ssids[0].ssid_len =
773 wpa_s->current_ssid->ssid_len;
774 params.num_ssids = 1;
775 } else {
776 wpa_printf(MSG_DEBUG, "P2P: No specific SSID known for scan during invitation");
777 }
778 goto ssid_list_set;
779 }
a21c05db
JM
780#endif /* CONFIG_P2P */
781
e76baaac 782 /* Find the starting point from which to continue scanning */
6fc6879b 783 ssid = wpa_s->conf->ssid;
ba2a573c 784 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
6fc6879b
JM
785 while (ssid) {
786 if (ssid == wpa_s->prev_scan_ssid) {
787 ssid = ssid->next;
788 break;
789 }
790 ssid = ssid->next;
791 }
792 }
6fc6879b 793
12455031 794 if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
0a34b62b
JM
795#ifdef CONFIG_AP
796 !wpa_s->ap_iface &&
797#endif /* CONFIG_AP */
12455031 798 wpa_s->conf->ap_scan == 2) {
7dcdcfd6 799 wpa_s->connect_without_scan = NULL;
b3aa456b 800 wpa_s->prev_scan_wildcard = 0;
e76baaac
JM
801 wpa_supplicant_assoc_try(wpa_s, ssid);
802 return;
803 } else if (wpa_s->conf->ap_scan == 2) {
6fc6879b 804 /*
ba2a573c
JM
805 * User-initiated scan request in ap_scan == 2; scan with
806 * wildcard SSID.
6fc6879b 807 */
e76baaac 808 ssid = NULL;
0f44ec8e
PQ
809 } else if (wpa_s->reattach && wpa_s->current_ssid != NULL) {
810 /*
811 * Perform single-channel single-SSID scan for
812 * reassociate-to-same-BSS operation.
813 */
814 /* Setup SSID */
815 ssid = wpa_s->current_ssid;
816 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
817 ssid->ssid, ssid->ssid_len);
818 params.ssids[0].ssid = ssid->ssid;
819 params.ssids[0].ssid_len = ssid->ssid_len;
820 params.num_ssids = 1;
821
822 /*
823 * Allocate memory for frequency array, allocate one extra
824 * slot for the zero-terminator.
825 */
826 params.freqs = os_malloc(sizeof(int) * 2);
827 if (params.freqs == NULL) {
828 wpa_dbg(wpa_s, MSG_ERROR, "Memory allocation failed");
829 return;
830 }
831 params.freqs[0] = wpa_s->assoc_freq;
832 params.freqs[1] = 0;
833
834 /*
835 * Reset the reattach flag so that we fall back to full scan if
836 * this scan fails.
837 */
838 wpa_s->reattach = 0;
e76baaac 839 } else {
5be45e2e 840 struct wpa_ssid *start = ssid, *tssid;
d3a98225 841 int freqs_set = 0;
e76baaac
JM
842 if (ssid == NULL && max_ssids > 1)
843 ssid = wpa_s->conf->ssid;
844 while (ssid) {
349493bd
JM
845 if (!wpas_network_disabled(wpa_s, ssid) &&
846 ssid->scan_ssid) {
e76baaac
JM
847 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
848 ssid->ssid, ssid->ssid_len);
849 params.ssids[params.num_ssids].ssid =
850 ssid->ssid;
851 params.ssids[params.num_ssids].ssid_len =
852 ssid->ssid_len;
853 params.num_ssids++;
854 if (params.num_ssids + 1 >= max_ssids)
855 break;
856 }
857 ssid = ssid->next;
858 if (ssid == start)
859 break;
860 if (ssid == NULL && max_ssids > 1 &&
861 start != wpa_s->conf->ssid)
862 ssid = wpa_s->conf->ssid;
6fc6879b 863 }
d3a98225 864
6891f0e6
LJ
865 if (wpa_s->scan_id_count &&
866 wpa_s->last_scan_req == MANUAL_SCAN_REQ)
867 wpa_set_scan_ssids(wpa_s, &params, max_ssids);
868
0855e2e1
JM
869 for (tssid = wpa_s->conf->ssid;
870 wpa_s->last_scan_req != MANUAL_SCAN_REQ && tssid;
871 tssid = tssid->next) {
349493bd 872 if (wpas_network_disabled(wpa_s, tssid))
d3a98225 873 continue;
5be45e2e 874 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
d3a98225 875 int_array_concat(&params.freqs,
5be45e2e 876 tssid->scan_freq);
d3a98225
JM
877 } else {
878 os_free(params.freqs);
879 params.freqs = NULL;
880 }
881 freqs_set = 1;
882 }
883 int_array_sort_unique(params.freqs);
6fc6879b
JM
884 }
885
b3aa456b
ES
886 if (ssid && max_ssids == 1) {
887 /*
888 * If the driver is limited to 1 SSID at a time interleave
889 * wildcard SSID scans with specific SSID scans to avoid
890 * waiting a long time for a wildcard scan.
891 */
892 if (!wpa_s->prev_scan_wildcard) {
893 params.ssids[0].ssid = NULL;
894 params.ssids[0].ssid_len = 0;
895 wpa_s->prev_scan_wildcard = 1;
896 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
897 "wildcard SSID (Interleave with specific)");
898 } else {
899 wpa_s->prev_scan_ssid = ssid;
900 wpa_s->prev_scan_wildcard = 0;
901 wpa_dbg(wpa_s, MSG_DEBUG,
902 "Starting AP scan for specific SSID: %s",
903 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
e76baaac 904 }
b3aa456b
ES
905 } else if (ssid) {
906 /* max_ssids > 1 */
907
908 wpa_s->prev_scan_ssid = ssid;
909 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
910 "the scan request");
911 params.num_ssids++;
88c2d488
JM
912 } else if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
913 wpa_s->manual_scan_passive && params.num_ssids == 0) {
914 wpa_dbg(wpa_s, MSG_DEBUG, "Use passive scan based on manual request");
c35e35ed
JM
915 } else if (wpa_s->conf->passive_scan) {
916 wpa_dbg(wpa_s, MSG_DEBUG,
917 "Use passive scan based on configuration");
e76baaac 918 } else {
ba2a573c 919 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
e76baaac 920 params.num_ssids++;
f049052b
BG
921 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
922 "SSID");
6fc6879b
JM
923 }
924
a80651d0 925ssid_list_set:
5f738a21 926 wpa_supplicant_optimize_freqs(wpa_s, &params);
a13e07ec 927 extra_ie = wpa_supplicant_extra_ies(wpa_s);
0e65037c 928
949938aa 929 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
2dbe63ad
JM
930 wpa_s->manual_scan_only_new) {
931 wpa_printf(MSG_DEBUG,
932 "Request driver to clear scan cache due to manual only_new=1 scan");
949938aa 933 params.only_new_results = 1;
2dbe63ad 934 }
949938aa 935
fee52342
JM
936 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs == NULL &&
937 wpa_s->manual_scan_freqs) {
938 wpa_dbg(wpa_s, MSG_DEBUG, "Limit manual scan to specified channels");
939 params.freqs = wpa_s->manual_scan_freqs;
940 wpa_s->manual_scan_freqs = NULL;
941 }
942
f47d639d 943 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
f049052b
BG
944 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
945 "generated frequency list");
f47d639d
JM
946 params.freqs = wpa_s->next_scan_freqs;
947 } else
948 os_free(wpa_s->next_scan_freqs);
949 wpa_s->next_scan_freqs = NULL;
faf9a858 950 wpa_setband_scan_freqs(wpa_s, &params);
f47d639d 951
f5ffc348
BG
952 /* See if user specified frequencies. If so, scan only those. */
953 if (wpa_s->conf->freq_list && !params.freqs) {
954 wpa_dbg(wpa_s, MSG_DEBUG,
955 "Optimize scan based on conf->freq_list");
956 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
957 }
958
6124e858
BG
959 /* Use current associated channel? */
960 if (wpa_s->conf->scan_cur_freq && !params.freqs) {
53c5dfc2
IP
961 unsigned int num = wpa_s->num_multichan_concurrent;
962
963 params.freqs = os_calloc(num + 1, sizeof(int));
964 if (params.freqs) {
965 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
966 if (num > 0) {
967 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
968 "current operating channels since "
969 "scan_cur_freq is enabled");
970 } else {
971 os_free(params.freqs);
972 params.freqs = NULL;
973 }
6124e858
BG
974 }
975 }
976
3812464c
JM
977 params.filter_ssids = wpa_supplicant_build_filter_ssids(
978 wpa_s->conf, &params.num_filter_ssids);
46ee0427
JM
979 if (extra_ie) {
980 params.extra_ies = wpabuf_head(extra_ie);
981 params.extra_ies_len = wpabuf_len(extra_ie);
982 }
3812464c 983
d1dd48e3 984#ifdef CONFIG_P2P
8235f89f 985 if (wpa_s->p2p_in_provisioning || wpa_s->p2p_in_invitation ||
85ea132a 986 (wpa_s->show_group_started && wpa_s->go_params)) {
d1dd48e3
JM
987 /*
988 * The interface may not yet be in P2P mode, so we have to
989 * explicitly request P2P probe to disable CCK rates.
990 */
991 params.p2p_probe = 1;
992 }
993#endif /* CONFIG_P2P */
994
56c76fa5
IP
995 if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) {
996 params.mac_addr_rand = 1;
997 if (wpa_s->mac_addr_scan) {
998 params.mac_addr = wpa_s->mac_addr_scan;
999 params.mac_addr_mask = wpa_s->mac_addr_scan + ETH_ALEN;
1000 }
1001 }
1002
7c865c68
TB
1003 scan_params = &params;
1004
1005scan:
5cc70322
JM
1006#ifdef CONFIG_P2P
1007 /*
1008 * If the driver does not support multi-channel concurrency and a
1009 * virtual interface that shares the same radio with the wpa_s interface
1010 * is operating there may not be need to scan other channels apart from
1011 * the current operating channel on the other virtual interface. Filter
1012 * out other channels in case we are trying to find a connection for a
1013 * station interface when we are not configured to prefer station
1014 * connection and a concurrent operation is already in process.
1015 */
12455031
LP
1016 if (wpa_s->scan_for_connection &&
1017 wpa_s->last_scan_req == NORMAL_SCAN_REQ &&
5cc70322
JM
1018 !scan_params->freqs && !params.freqs &&
1019 wpas_is_p2p_prioritized(wpa_s) &&
5cc70322
JM
1020 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
1021 non_p2p_network_enabled(wpa_s)) {
53c5dfc2
IP
1022 unsigned int num = wpa_s->num_multichan_concurrent;
1023
1024 params.freqs = os_calloc(num + 1, sizeof(int));
1025 if (params.freqs) {
1026 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
1027 if (num > 0 && num == wpa_s->num_multichan_concurrent) {
1028 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
1029 } else {
1030 os_free(params.freqs);
1031 params.freqs = NULL;
1032 }
5cc70322
JM
1033 }
1034 }
1035#endif /* CONFIG_P2P */
1036
7c865c68 1037 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
6fc6879b 1038
fee52342
JM
1039 if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs &&
1040 !wpa_s->manual_scan_freqs) {
1041 /* Restore manual_scan_freqs for the next attempt */
1042 wpa_s->manual_scan_freqs = params.freqs;
1043 params.freqs = NULL;
1044 }
1045
46ee0427 1046 wpabuf_free(extra_ie);
d3a98225 1047 os_free(params.freqs);
3812464c 1048 os_free(params.filter_ssids);
ad08c363 1049
6fc6879b 1050 if (ret) {
f049052b 1051 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
2d9c99e3
JM
1052 if (wpa_s->scan_prev_wpa_state != wpa_s->wpa_state)
1053 wpa_supplicant_set_state(wpa_s,
1054 wpa_s->scan_prev_wpa_state);
23270cd8 1055 /* Restore scan_req since we will try to scan again */
12455031 1056 wpa_s->scan_req = wpa_s->last_scan_req;
1c4c9c50 1057 wpa_supplicant_req_scan(wpa_s, 1, 0);
5cc70322
JM
1058 } else {
1059 wpa_s->scan_for_connection = 0;
a8826b18
JM
1060#ifdef CONFIG_INTERWORKING
1061 wpa_s->interworking_fast_assoc_tried = 0;
1062#endif /* CONFIG_INTERWORKING */
d902a9c1 1063 }
6fc6879b
JM
1064}
1065
1066
9e737f08
PF
1067void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
1068{
594516b4 1069 struct os_reltime remaining, new_int;
9e737f08
PF
1070 int cancelled;
1071
1072 cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
1073 &remaining);
1074
1075 new_int.sec = sec;
1076 new_int.usec = 0;
594516b4 1077 if (cancelled && os_reltime_before(&remaining, &new_int)) {
9e737f08
PF
1078 new_int.sec = remaining.sec;
1079 new_int.usec = remaining.usec;
1080 }
1081
c6f5dec9
PF
1082 if (cancelled) {
1083 eloop_register_timeout(new_int.sec, new_int.usec,
1084 wpa_supplicant_scan, wpa_s, NULL);
1085 }
9e737f08
PF
1086 wpa_s->scan_interval = sec;
1087}
1088
1089
6fc6879b
JM
1090/**
1091 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
1092 * @wpa_s: Pointer to wpa_supplicant data
1093 * @sec: Number of seconds after which to scan
1094 * @usec: Number of microseconds after which to scan
1095 *
1096 * This function is used to schedule a scan for neighboring access points after
1097 * the specified time.
1098 */
1099void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
1100{
44b9ea5b
JM
1101 int res;
1102
1103 if (wpa_s->p2p_mgmt) {
1104 wpa_dbg(wpa_s, MSG_DEBUG,
1105 "Ignore scan request (%d.%06d sec) on p2p_mgmt interface",
1106 sec, usec);
1107 return;
1108 }
1109
1110 res = eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s,
1111 NULL);
a09ffd5f
JM
1112 if (res == 1) {
1113 wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d.%06d sec",
e2f5a988 1114 sec, usec);
a09ffd5f
JM
1115 } else if (res == 0) {
1116 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore new scan request for %d.%06d sec since an earlier request is scheduled to trigger sooner",
1117 sec, usec);
1118 } else {
1119 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d.%06d sec",
1120 sec, usec);
1121 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
e2f5a988 1122 }
6fc6879b
JM
1123}
1124
1125
6a90053c
LC
1126/**
1127 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
1128 * @wpa_s: Pointer to wpa_supplicant data
1129 * @sec: Number of seconds after which to scan
1130 * @usec: Number of microseconds after which to scan
2c09af30 1131 * Returns: 0 on success or -1 otherwise
6a90053c
LC
1132 *
1133 * This function is used to schedule periodic scans for neighboring
1134 * access points after the specified time.
1135 */
1136int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
1137 int sec, int usec)
1138{
1139 if (!wpa_s->sched_scan_supported)
1140 return -1;
1141
1142 eloop_register_timeout(sec, usec,
1143 wpa_supplicant_delayed_sched_scan_timeout,
1144 wpa_s, NULL);
1145
1146 return 0;
1147}
1148
1149
cbdf3507
LC
1150/**
1151 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
1152 * @wpa_s: Pointer to wpa_supplicant data
2c09af30 1153 * Returns: 0 is sched_scan was started or -1 otherwise
cbdf3507
LC
1154 *
1155 * This function is used to schedule periodic scans for neighboring
1156 * access points repeating the scan continuously.
1157 */
1158int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
1159{
1160 struct wpa_driver_scan_params params;
7c865c68 1161 struct wpa_driver_scan_params *scan_params;
cbdf3507 1162 enum wpa_states prev_state;
7c865c68 1163 struct wpa_ssid *ssid = NULL;
a13e07ec 1164 struct wpabuf *extra_ie = NULL;
cbdf3507 1165 int ret;
cbdf3507 1166 unsigned int max_sched_scan_ssids;
76a5249e 1167 int wildcard = 0;
0b7a25c0 1168 int need_ssids;
32c02261 1169 struct sched_scan_plan scan_plan;
cbdf3507
LC
1170
1171 if (!wpa_s->sched_scan_supported)
1172 return -1;
1173
1174 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
1175 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
1176 else
1177 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
54ddd743 1178 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
76a5249e 1179 return -1;
cbdf3507 1180
1966e3d1
ES
1181 if (wpa_s->sched_scanning) {
1182 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
cbdf3507 1183 return 0;
1966e3d1 1184 }
cbdf3507 1185
0b7a25c0
JM
1186 need_ssids = 0;
1187 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
349493bd 1188 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
0b7a25c0
JM
1189 /* Use wildcard SSID to find this network */
1190 wildcard = 1;
349493bd
JM
1191 } else if (!wpas_network_disabled(wpa_s, ssid) &&
1192 ssid->ssid_len)
0b7a25c0 1193 need_ssids++;
aa283ddd
JM
1194
1195#ifdef CONFIG_WPS
349493bd 1196 if (!wpas_network_disabled(wpa_s, ssid) &&
fea7c3a0 1197 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
aa283ddd
JM
1198 /*
1199 * Normal scan is more reliable and faster for WPS
1200 * operations and since these are for short periods of
1201 * time, the benefit of trying to use sched_scan would
1202 * be limited.
1203 */
1204 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1205 "sched_scan for WPS");
1206 return -1;
1207 }
1208#endif /* CONFIG_WPS */
0b7a25c0
JM
1209 }
1210 if (wildcard)
1211 need_ssids++;
1212
1213 if (wpa_s->normal_scans < 3 &&
1214 (need_ssids <= wpa_s->max_scan_ssids ||
1215 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
1216 /*
1217 * When normal scan can speed up operations, use that for the
1218 * first operations before starting the sched_scan to allow
1219 * user space sleep more. We do this only if the normal scan
1220 * has functionality that is suitable for this or if the
1221 * sched_scan does not have better support for multiple SSIDs.
1222 */
1223 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1224 "sched_scan for initial scans (normal_scans=%d)",
1225 wpa_s->normal_scans);
1226 return -1;
1227 }
1228
cbdf3507
LC
1229 os_memset(&params, 0, sizeof(params));
1230
b59e6f26 1231 /* If we can't allocate space for the filters, we just don't filter */
faebdeaa 1232 params.filter_ssids = os_calloc(wpa_s->max_match_sets,
b59e6f26
LC
1233 sizeof(struct wpa_driver_scan_filter));
1234
cbdf3507
LC
1235 prev_state = wpa_s->wpa_state;
1236 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1237 wpa_s->wpa_state == WPA_INACTIVE)
1238 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1239
7c865c68
TB
1240 if (wpa_s->autoscan_params != NULL) {
1241 scan_params = wpa_s->autoscan_params;
1242 goto scan;
1243 }
1244
cbdf3507
LC
1245 /* Find the starting point from which to continue scanning */
1246 ssid = wpa_s->conf->ssid;
1247 if (wpa_s->prev_sched_ssid) {
1248 while (ssid) {
1249 if (ssid == wpa_s->prev_sched_ssid) {
1250 ssid = ssid->next;
1251 break;
1252 }
1253 ssid = ssid->next;
1254 }
1255 }
1256
1257 if (!ssid || !wpa_s->prev_sched_ssid) {
1258 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
cbdf3507
LC
1259 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1260 wpa_s->first_sched_scan = 1;
1261 ssid = wpa_s->conf->ssid;
1262 wpa_s->prev_sched_ssid = ssid;
1263 }
1264
76a5249e
JM
1265 if (wildcard) {
1266 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1267 params.num_ssids++;
1268 }
1269
cbdf3507 1270 while (ssid) {
349493bd 1271 if (wpas_network_disabled(wpa_s, ssid))
5edddf41 1272 goto next;
cbdf3507 1273
fcd16847
JM
1274 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1275 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1966e3d1
ES
1276 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1277 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
b59e6f26
LC
1278 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1279 ssid->ssid, ssid->ssid_len);
1280 params.filter_ssids[params.num_filter_ssids].ssid_len =
1281 ssid->ssid_len;
1282 params.num_filter_ssids++;
86b47aaf
JM
1283 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1284 {
1285 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1286 "filter for sched_scan - drop filter");
1287 os_free(params.filter_ssids);
1288 params.filter_ssids = NULL;
1289 params.num_filter_ssids = 0;
b59e6f26
LC
1290 }
1291
3f56f3a4 1292 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
76a5249e
JM
1293 if (params.num_ssids == max_sched_scan_ssids)
1294 break; /* only room for broadcast SSID */
3f56f3a4
JM
1295 wpa_dbg(wpa_s, MSG_DEBUG,
1296 "add to active scan ssid: %s",
1966e3d1 1297 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
cbdf3507
LC
1298 params.ssids[params.num_ssids].ssid =
1299 ssid->ssid;
1300 params.ssids[params.num_ssids].ssid_len =
1301 ssid->ssid_len;
1302 params.num_ssids++;
b59e6f26 1303 if (params.num_ssids >= max_sched_scan_ssids) {
cbdf3507 1304 wpa_s->prev_sched_ssid = ssid;
b55aca46
JM
1305 do {
1306 ssid = ssid->next;
1307 } while (ssid &&
349493bd 1308 (wpas_network_disabled(wpa_s, ssid) ||
fea7c3a0 1309 !ssid->scan_ssid));
cbdf3507
LC
1310 break;
1311 }
1312 }
b59e6f26 1313
5edddf41 1314 next:
cbdf3507
LC
1315 wpa_s->prev_sched_ssid = ssid;
1316 ssid = ssid->next;
1317 }
1318
7c6a266c
JM
1319 if (params.num_filter_ssids == 0) {
1320 os_free(params.filter_ssids);
1321 params.filter_ssids = NULL;
1322 }
1323
a13e07ec
ES
1324 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1325 if (extra_ie) {
1326 params.extra_ies = wpabuf_head(extra_ie);
1327 params.extra_ies_len = wpabuf_len(extra_ie);
1328 }
cbdf3507 1329
cf70d298
RM
1330 if (wpa_s->conf->filter_rssi)
1331 params.filter_rssi = wpa_s->conf->filter_rssi;
1332
1cc0d6a0
BP
1333 /* See if user specified frequencies. If so, scan only those. */
1334 if (wpa_s->conf->freq_list && !params.freqs) {
1335 wpa_dbg(wpa_s, MSG_DEBUG,
1336 "Optimize scan based on conf->freq_list");
1337 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1338 }
1339
7c865c68
TB
1340 scan_params = &params;
1341
1342scan:
32c02261
AS
1343 wpa_s->sched_scan_timed_out = 0;
1344
1345 /*
1346 * We cannot support multiple scan plans if the scan request includes
1347 * too many SSID's, so in this case use only the last scan plan and make
1348 * it run infinitely. It will be stopped by the timeout.
1349 */
1350 if (wpa_s->sched_scan_plans_num == 1 ||
1351 (wpa_s->sched_scan_plans_num && !ssid && wpa_s->first_sched_scan)) {
1352 params.sched_scan_plans = wpa_s->sched_scan_plans;
1353 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
1354 } else if (wpa_s->sched_scan_plans_num > 1) {
a8cb5a88 1355 wpa_dbg(wpa_s, MSG_DEBUG,
32c02261
AS
1356 "Too many SSIDs. Default to using single scheduled_scan plan");
1357 params.sched_scan_plans =
1358 &wpa_s->sched_scan_plans[wpa_s->sched_scan_plans_num -
1359 1];
1360 params.sched_scan_plans_num = 1;
7d21a223 1361 } else {
32c02261
AS
1362 if (wpa_s->conf->sched_scan_interval)
1363 scan_plan.interval = wpa_s->conf->sched_scan_interval;
1364 else
1365 scan_plan.interval = 10;
1366
1367 if (scan_plan.interval > wpa_s->max_sched_scan_plan_interval) {
1368 wpa_printf(MSG_WARNING,
1369 "Scan interval too long(%u), use the maximum allowed(%u)",
1370 scan_plan.interval,
1371 wpa_s->max_sched_scan_plan_interval);
1372 scan_plan.interval =
1373 wpa_s->max_sched_scan_plan_interval;
1374 }
1375
1376 scan_plan.iterations = 0;
1377 params.sched_scan_plans = &scan_plan;
1378 params.sched_scan_plans_num = 1;
1379 }
1380
1381 if (ssid || !wpa_s->first_sched_scan) {
7d21a223 1382 wpa_dbg(wpa_s, MSG_DEBUG,
32c02261
AS
1383 "Starting sched scan: interval %u timeout %d",
1384 params.sched_scan_plans[0].interval,
1385 wpa_s->sched_scan_timeout);
1386 } else {
1387 wpa_dbg(wpa_s, MSG_DEBUG, "Starting sched scan (no timeout)");
a8cb5a88 1388 }
cbdf3507 1389
faf9a858
JM
1390 wpa_setband_scan_freqs(wpa_s, scan_params);
1391
56c76fa5
IP
1392 if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCHED_SCAN) {
1393 params.mac_addr_rand = 1;
1394 if (wpa_s->mac_addr_sched_scan) {
1395 params.mac_addr = wpa_s->mac_addr_sched_scan;
1396 params.mac_addr_mask = wpa_s->mac_addr_sched_scan +
1397 ETH_ALEN;
1398 }
1399 }
1400
32c02261 1401 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params);
a13e07ec 1402 wpabuf_free(extra_ie);
b59e6f26 1403 os_free(params.filter_ssids);
cbdf3507
LC
1404 if (ret) {
1405 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1406 if (prev_state != wpa_s->wpa_state)
1407 wpa_supplicant_set_state(wpa_s, prev_state);
1408 return ret;
1409 }
1410
1411 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1412 if (ssid || !wpa_s->first_sched_scan) {
1413 wpa_s->sched_scan_timed_out = 0;
1414 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1415 wpa_supplicant_sched_scan_timeout,
1416 wpa_s, NULL);
1417 wpa_s->first_sched_scan = 0;
1418 wpa_s->sched_scan_timeout /= 2;
32c02261
AS
1419 params.sched_scan_plans[0].interval *= 2;
1420 if ((unsigned int) wpa_s->sched_scan_timeout <
1421 params.sched_scan_plans[0].interval ||
1422 params.sched_scan_plans[0].interval >
1423 wpa_s->max_sched_scan_plan_interval) {
1424 params.sched_scan_plans[0].interval = 10;
a09fc1cc
DS
1425 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1426 }
cbdf3507
LC
1427 }
1428
7ed52f67
DS
1429 /* If there is no more ssids, start next time from the beginning */
1430 if (!ssid)
1431 wpa_s->prev_sched_ssid = NULL;
1432
cbdf3507
LC
1433 return 0;
1434}
1435
1436
6fc6879b
JM
1437/**
1438 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1439 * @wpa_s: Pointer to wpa_supplicant data
1440 *
1441 * This function is used to cancel a scan request scheduled with
1442 * wpa_supplicant_req_scan().
1443 */
1444void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1445{
f049052b 1446 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
6fc6879b
JM
1447 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
1448}
cb8564b1
DW
1449
1450
831770bf
CZ
1451/**
1452 * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
1453 * @wpa_s: Pointer to wpa_supplicant data
1454 *
1455 * This function is used to stop a delayed scheduled scan.
1456 */
1457void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
1458{
1459 if (!wpa_s->sched_scan_supported)
1460 return;
1461
1462 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
1463 eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
1464 wpa_s, NULL);
1465}
1466
1467
cbdf3507
LC
1468/**
1469 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1470 * @wpa_s: Pointer to wpa_supplicant data
1471 *
1472 * This function is used to stop a periodic scheduled scan.
1473 */
1474void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1475{
1476 if (!wpa_s->sched_scanning)
1477 return;
1478
1479 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1480 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1481 wpa_supplicant_stop_sched_scan(wpa_s);
1482}
1483
1484
2c09af30
JM
1485/**
1486 * wpa_supplicant_notify_scanning - Indicate possible scan state change
1487 * @wpa_s: Pointer to wpa_supplicant data
1488 * @scanning: Whether scanning is currently in progress
1489 *
1490 * This function is to generate scanning notifycations. It is called whenever
1491 * there may have been a change in scanning (scan started, completed, stopped).
1492 * wpas_notify_scanning() is called whenever the scanning state changed from the
1493 * previously notified state.
1494 */
cb8564b1
DW
1495void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1496 int scanning)
1497{
1498 if (wpa_s->scanning != scanning) {
1499 wpa_s->scanning = scanning;
8bac466b 1500 wpas_notify_scanning(wpa_s);
cb8564b1
DW
1501 }
1502}
1503
9ba9fa07
JM
1504
1505static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1506{
1507 int rate = 0;
1508 const u8 *ie;
1509 int i;
1510
1511 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1512 for (i = 0; ie && i < ie[1]; i++) {
1513 if ((ie[i + 2] & 0x7f) > rate)
1514 rate = ie[i + 2] & 0x7f;
1515 }
1516
1517 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1518 for (i = 0; ie && i < ie[1]; i++) {
1519 if ((ie[i + 2] & 0x7f) > rate)
1520 rate = ie[i + 2] & 0x7f;
1521 }
1522
1523 return rate;
1524}
1525
1526
2c09af30
JM
1527/**
1528 * wpa_scan_get_ie - Fetch a specified information element from a scan result
1529 * @res: Scan result entry
1530 * @ie: Information element identitifier (WLAN_EID_*)
1531 * Returns: Pointer to the information element (id field) or %NULL if not found
1532 *
1533 * This function returns the first matching information element in the scan
1534 * result.
1535 */
d1f9c410
JM
1536const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1537{
1538 const u8 *end, *pos;
1539
1540 pos = (const u8 *) (res + 1);
1541 end = pos + res->ie_len;
1542
904e977b
JM
1543 while (end - pos > 1) {
1544 if (2 + pos[1] > end - pos)
d1f9c410
JM
1545 break;
1546 if (pos[0] == ie)
1547 return pos;
1548 pos += 2 + pos[1];
1549 }
1550
1551 return NULL;
1552}
1553
1554
2c09af30
JM
1555/**
1556 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1557 * @res: Scan result entry
1558 * @vendor_type: Vendor type (four octets starting the IE payload)
1559 * Returns: Pointer to the information element (id field) or %NULL if not found
1560 *
1561 * This function returns the first matching information element in the scan
1562 * result.
1563 */
9ba9fa07
JM
1564const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1565 u32 vendor_type)
1566{
1567 const u8 *end, *pos;
1568
1569 pos = (const u8 *) (res + 1);
1570 end = pos + res->ie_len;
1571
904e977b
JM
1572 while (end - pos > 1) {
1573 if (2 + pos[1] > end - pos)
9ba9fa07
JM
1574 break;
1575 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1576 vendor_type == WPA_GET_BE32(&pos[2]))
1577 return pos;
1578 pos += 2 + pos[1];
1579 }
1580
1581 return NULL;
1582}
1583
1584
aaeb9c98
JM
1585/**
1586 * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
1587 * @res: Scan result entry
1588 * @vendor_type: Vendor type (four octets starting the IE payload)
1589 * Returns: Pointer to the information element (id field) or %NULL if not found
1590 *
1591 * This function returns the first matching information element in the scan
1592 * result.
1593 *
1594 * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
1595 * from Beacon frames instead of either Beacon or Probe Response frames.
1596 */
1597const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
1598 u32 vendor_type)
1599{
1600 const u8 *end, *pos;
1601
1602 if (res->beacon_ie_len == 0)
1603 return NULL;
1604
1605 pos = (const u8 *) (res + 1);
1606 pos += res->ie_len;
1607 end = pos + res->beacon_ie_len;
1608
904e977b
JM
1609 while (end - pos > 1) {
1610 if (2 + pos[1] > end - pos)
aaeb9c98
JM
1611 break;
1612 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1613 vendor_type == WPA_GET_BE32(&pos[2]))
1614 return pos;
1615 pos += 2 + pos[1];
1616 }
1617
1618 return NULL;
1619}
1620
1621
2c09af30
JM
1622/**
1623 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
1624 * @res: Scan result entry
1625 * @vendor_type: Vendor type (four octets starting the IE payload)
1626 * Returns: Pointer to the information element payload or %NULL if not found
1627 *
1628 * This function returns concatenated payload of possibly fragmented vendor
1629 * specific information elements in the scan result. The caller is responsible
1630 * for freeing the returned buffer.
1631 */
9ba9fa07
JM
1632struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1633 u32 vendor_type)
1634{
1635 struct wpabuf *buf;
1636 const u8 *end, *pos;
1637
1638 buf = wpabuf_alloc(res->ie_len);
1639 if (buf == NULL)
1640 return NULL;
1641
1642 pos = (const u8 *) (res + 1);
1643 end = pos + res->ie_len;
1644
904e977b
JM
1645 while (end - pos > 1) {
1646 if (2 + pos[1] > end - pos)
54f489be
JM
1647 break;
1648 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1649 vendor_type == WPA_GET_BE32(&pos[2]))
1650 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1651 pos += 2 + pos[1];
1652 }
1653
1654 if (wpabuf_len(buf) == 0) {
1655 wpabuf_free(buf);
1656 buf = NULL;
1657 }
1658
1659 return buf;
1660}
1661
1662
577db0ae
GM
1663/*
1664 * Channels with a great SNR can operate at full rate. What is a great SNR?
1665 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1666 * rule of thumb is that any SNR above 20 is good." This one
1667 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1668 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1669 * conservative value.
1670 */
1671#define GREAT_SNR 30
1672
f0d0a5d2
MA
1673#define IS_5GHZ(n) (n > 4000)
1674
9ba9fa07
JM
1675/* Compare function for sorting scan results. Return >0 if @b is considered
1676 * better. */
1677static int wpa_scan_result_compar(const void *a, const void *b)
1678{
577db0ae 1679#define MIN(a,b) a < b ? a : b
9ba9fa07
JM
1680 struct wpa_scan_res **_wa = (void *) a;
1681 struct wpa_scan_res **_wb = (void *) b;
1682 struct wpa_scan_res *wa = *_wa;
1683 struct wpa_scan_res *wb = *_wb;
a1b790eb
JM
1684 int wpa_a, wpa_b;
1685 int snr_a, snr_b, snr_a_full, snr_b_full;
9ba9fa07
JM
1686
1687 /* WPA/WPA2 support preferred */
1688 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1689 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1690 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1691 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1692
1693 if (wpa_b && !wpa_a)
1694 return 1;
1695 if (!wpa_b && wpa_a)
1696 return -1;
1697
1698 /* privacy support preferred */
1699 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1700 (wb->caps & IEEE80211_CAP_PRIVACY))
1701 return 1;
1702 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1703 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1704 return -1;
1705
f0d0a5d2 1706 if (wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) {
a1b790eb
JM
1707 snr_a_full = wa->snr;
1708 snr_a = MIN(wa->snr, GREAT_SNR);
1709 snr_b_full = wb->snr;
aa517ae2 1710 snr_b = MIN(wb->snr, GREAT_SNR);
577db0ae 1711 } else {
f0d0a5d2
MA
1712 /* Level is not in dBm, so we can't calculate
1713 * SNR. Just use raw level (units unknown). */
a1b790eb
JM
1714 snr_a = snr_a_full = wa->level;
1715 snr_b = snr_b_full = wb->level;
577db0ae
GM
1716 }
1717
f0d0a5d2
MA
1718 /* if SNR is close, decide by max rate or frequency band */
1719 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
9ba9fa07 1720 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
a1b790eb
JM
1721 if (wa->est_throughput != wb->est_throughput)
1722 return wb->est_throughput - wa->est_throughput;
577db0ae
GM
1723 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1724 return IS_5GHZ(wa->freq) ? -1 : 1;
9ba9fa07
JM
1725 }
1726
577db0ae 1727 /* all things being equal, use SNR; if SNRs are
9ba9fa07
JM
1728 * identical, use quality values since some drivers may only report
1729 * that value and leave the signal level zero */
a1b790eb 1730 if (snr_b_full == snr_a_full)
9ba9fa07 1731 return wb->qual - wa->qual;
a1b790eb 1732 return snr_b_full - snr_a_full;
577db0ae 1733#undef MIN
9ba9fa07
JM
1734}
1735
1736
41e650ae
JM
1737#ifdef CONFIG_WPS
1738/* Compare function for sorting scan results when searching a WPS AP for
1739 * provisioning. Return >0 if @b is considered better. */
1740static int wpa_scan_result_wps_compar(const void *a, const void *b)
1741{
1742 struct wpa_scan_res **_wa = (void *) a;
1743 struct wpa_scan_res **_wb = (void *) b;
1744 struct wpa_scan_res *wa = *_wa;
1745 struct wpa_scan_res *wb = *_wb;
1746 int uses_wps_a, uses_wps_b;
1747 struct wpabuf *wps_a, *wps_b;
1748 int res;
1749
1750 /* Optimization - check WPS IE existence before allocated memory and
1751 * doing full reassembly. */
1752 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1753 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1754 if (uses_wps_a && !uses_wps_b)
1755 return -1;
1756 if (!uses_wps_a && uses_wps_b)
1757 return 1;
1758
1759 if (uses_wps_a && uses_wps_b) {
1760 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1761 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1762 res = wps_ap_priority_compar(wps_a, wps_b);
1763 wpabuf_free(wps_a);
1764 wpabuf_free(wps_b);
1765 if (res)
1766 return res;
1767 }
1768
1769 /*
1770 * Do not use current AP security policy as a sorting criteria during
1771 * WPS provisioning step since the AP may get reconfigured at the
1772 * completion of provisioning.
1773 */
1774
1775 /* all things being equal, use signal level; if signal levels are
1776 * identical, use quality values since some drivers may only report
1777 * that value and leave the signal level zero */
1778 if (wb->level == wa->level)
1779 return wb->qual - wa->qual;
1780 return wb->level - wa->level;
1781}
1782#endif /* CONFIG_WPS */
1783
1784
aa820e02
JM
1785static void dump_scan_res(struct wpa_scan_results *scan_res)
1786{
76202aed 1787#ifndef CONFIG_NO_STDOUT_DEBUG
aa820e02
JM
1788 size_t i;
1789
1790 if (scan_res->res == NULL || scan_res->num == 0)
1791 return;
1792
1793 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1794
1795 for (i = 0; i < scan_res->num; i++) {
1796 struct wpa_scan_res *r = scan_res->res[i];
1cbe86e2 1797 u8 *pos;
f0d0a5d2 1798 if (r->flags & WPA_SCAN_LEVEL_DBM) {
f0d0a5d2
MA
1799 int noise_valid = !(r->flags & WPA_SCAN_NOISE_INVALID);
1800
aa820e02 1801 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
a1b790eb 1802 "noise=%d%s level=%d snr=%d%s flags=0x%x age=%u est=%u",
aa820e02 1803 MAC2STR(r->bssid), r->freq, r->qual,
f0d0a5d2 1804 r->noise, noise_valid ? "" : "~", r->level,
a1b790eb
JM
1805 r->snr, r->snr >= GREAT_SNR ? "*" : "",
1806 r->flags,
1807 r->age, r->est_throughput);
aa820e02
JM
1808 } else {
1809 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
a1b790eb 1810 "noise=%d level=%d flags=0x%x age=%u est=%u",
aa820e02 1811 MAC2STR(r->bssid), r->freq, r->qual,
a1b790eb
JM
1812 r->noise, r->level, r->flags, r->age,
1813 r->est_throughput);
aa820e02 1814 }
1cbe86e2
JM
1815 pos = (u8 *) (r + 1);
1816 if (r->ie_len)
1817 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
1818 pos += r->ie_len;
1819 if (r->beacon_ie_len)
1820 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
1821 pos, r->beacon_ie_len);
aa820e02 1822 }
76202aed 1823#endif /* CONFIG_NO_STDOUT_DEBUG */
aa820e02
JM
1824}
1825
1826
2c09af30
JM
1827/**
1828 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
1829 * @wpa_s: Pointer to wpa_supplicant data
1830 * @bssid: BSSID to check
1831 * Returns: 0 if the BSSID is filtered or 1 if not
1832 *
1833 * This function is used to filter out specific BSSIDs from scan reslts mainly
1834 * for testing purposes (SET bssid_filter ctrl_iface command).
1835 */
d445a5cd
JM
1836int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
1837 const u8 *bssid)
1838{
1839 size_t i;
1840
1841 if (wpa_s->bssid_filter == NULL)
1842 return 1;
1843
1844 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
1845 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
1846 ETH_ALEN) == 0)
1847 return 1;
1848 }
1849
1850 return 0;
1851}
1852
1853
1854static void filter_scan_res(struct wpa_supplicant *wpa_s,
1855 struct wpa_scan_results *res)
1856{
1857 size_t i, j;
1858
1859 if (wpa_s->bssid_filter == NULL)
1860 return;
1861
1862 for (i = 0, j = 0; i < res->num; i++) {
1863 if (wpa_supplicant_filter_bssid_match(wpa_s,
1864 res->res[i]->bssid)) {
1865 res->res[j++] = res->res[i];
1866 } else {
1867 os_free(res->res[i]);
1868 res->res[i] = NULL;
1869 }
1870 }
1871
1872 if (res->num != j) {
1873 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
1874 (int) (res->num - j));
1875 res->num = j;
1876 }
1877}
1878
1879
f0d0a5d2
MA
1880/*
1881 * Noise floor values to use when we have signal strength
1882 * measurements, but no noise floor measurments. These values were
1883 * measured in an office environment with many APs.
1884 */
1885#define DEFAULT_NOISE_FLOOR_2GHZ (-89)
1886#define DEFAULT_NOISE_FLOOR_5GHZ (-92)
1887
a1b790eb
JM
1888static void scan_snr(struct wpa_scan_res *res)
1889{
1890 if (res->flags & WPA_SCAN_NOISE_INVALID) {
1891 res->noise = IS_5GHZ(res->freq) ?
1892 DEFAULT_NOISE_FLOOR_5GHZ :
1893 DEFAULT_NOISE_FLOOR_2GHZ;
1894 }
1895
1896 if (res->flags & WPA_SCAN_LEVEL_DBM) {
1897 res->snr = res->level - res->noise;
1898 } else {
1899 /* Level is not in dBm, so we can't calculate
1900 * SNR. Just use raw level (units unknown). */
1901 res->snr = res->level;
1902 }
1903}
1904
1905
1906static unsigned int max_ht20_rate(int snr)
1907{
1908 if (snr < 6)
1909 return 6500; /* HT20 MCS0 */
1910 if (snr < 8)
1911 return 13000; /* HT20 MCS1 */
1912 if (snr < 13)
1913 return 19500; /* HT20 MCS2 */
1914 if (snr < 17)
1915 return 26000; /* HT20 MCS3 */
1916 if (snr < 20)
1917 return 39000; /* HT20 MCS4 */
1918 if (snr < 23)
1919 return 52000; /* HT20 MCS5 */
1920 if (snr < 24)
1921 return 58500; /* HT20 MCS6 */
1922 return 65000; /* HT20 MCS7 */
1923}
1924
1925
1926static unsigned int max_ht40_rate(int snr)
1927{
1928 if (snr < 3)
1929 return 13500; /* HT40 MCS0 */
1930 if (snr < 6)
1931 return 27000; /* HT40 MCS1 */
1932 if (snr < 10)
1933 return 40500; /* HT40 MCS2 */
1934 if (snr < 15)
1935 return 54000; /* HT40 MCS3 */
1936 if (snr < 17)
1937 return 81000; /* HT40 MCS4 */
1938 if (snr < 22)
1939 return 108000; /* HT40 MCS5 */
8b2b718d 1940 if (snr < 24)
a1b790eb
JM
1941 return 121500; /* HT40 MCS6 */
1942 return 135000; /* HT40 MCS7 */
1943}
1944
1945
1946static unsigned int max_vht80_rate(int snr)
1947{
1948 if (snr < 1)
1949 return 0;
1950 if (snr < 2)
1951 return 29300; /* VHT80 MCS0 */
1952 if (snr < 5)
1953 return 58500; /* VHT80 MCS1 */
1954 if (snr < 9)
1955 return 87800; /* VHT80 MCS2 */
1956 if (snr < 11)
1957 return 117000; /* VHT80 MCS3 */
1958 if (snr < 15)
1959 return 175500; /* VHT80 MCS4 */
1960 if (snr < 16)
1961 return 234000; /* VHT80 MCS5 */
1962 if (snr < 18)
1963 return 263300; /* VHT80 MCS6 */
1964 if (snr < 20)
1965 return 292500; /* VHT80 MCS7 */
1966 if (snr < 22)
1967 return 351000; /* VHT80 MCS8 */
1968 return 390000; /* VHT80 MCS9 */
1969}
1970
1971
1972static void scan_est_throughput(struct wpa_supplicant *wpa_s,
1973 struct wpa_scan_res *res)
1974{
1975 enum local_hw_capab capab = wpa_s->hw_capab;
1976 int rate; /* max legacy rate in 500 kb/s units */
1977 const u8 *ie;
1978 unsigned int est, tmp;
1979 int snr = res->snr;
1980
1981 if (res->est_throughput)
1982 return;
1983
1984 /* Get maximum legacy rate */
1985 rate = wpa_scan_get_max_rate(res);
1986
1987 /* Limit based on estimated SNR */
1988 if (rate > 1 * 2 && snr < 1)
1989 rate = 1 * 2;
1990 else if (rate > 2 * 2 && snr < 4)
1991 rate = 2 * 2;
1992 else if (rate > 6 * 2 && snr < 5)
1993 rate = 6 * 2;
1994 else if (rate > 9 * 2 && snr < 6)
1995 rate = 9 * 2;
1996 else if (rate > 12 * 2 && snr < 7)
1997 rate = 12 * 2;
1998 else if (rate > 18 * 2 && snr < 10)
1999 rate = 18 * 2;
2000 else if (rate > 24 * 2 && snr < 11)
2001 rate = 24 * 2;
2002 else if (rate > 36 * 2 && snr < 15)
2003 rate = 36 * 2;
2004 else if (rate > 48 * 2 && snr < 19)
2005 rate = 48 * 2;
2006 else if (rate > 54 * 2 && snr < 21)
2007 rate = 54 * 2;
2008 est = rate * 500;
2009
2010 if (capab == CAPAB_HT || capab == CAPAB_HT40 || capab == CAPAB_VHT) {
2011 ie = wpa_scan_get_ie(res, WLAN_EID_HT_CAP);
2012 if (ie) {
2013 tmp = max_ht20_rate(snr);
2014 if (tmp > est)
2015 est = tmp;
2016 }
2017 }
2018
2019 if (capab == CAPAB_HT40 || capab == CAPAB_VHT) {
2020 ie = wpa_scan_get_ie(res, WLAN_EID_HT_OPERATION);
2021 if (ie && ie[1] >= 2 &&
2022 (ie[3] & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
2023 tmp = max_ht40_rate(snr);
2024 if (tmp > est)
2025 est = tmp;
2026 }
2027 }
2028
2029 if (capab == CAPAB_VHT) {
2030 /* Use +1 to assume VHT is always faster than HT */
2031 ie = wpa_scan_get_ie(res, WLAN_EID_VHT_CAP);
2032 if (ie) {
2033 tmp = max_ht20_rate(snr) + 1;
2034 if (tmp > est)
2035 est = tmp;
2036
2037 ie = wpa_scan_get_ie(res, WLAN_EID_HT_OPERATION);
2038 if (ie && ie[1] >= 2 &&
2039 (ie[3] &
2040 HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
2041 tmp = max_ht40_rate(snr) + 1;
2042 if (tmp > est)
2043 est = tmp;
2044 }
2045
2046 ie = wpa_scan_get_ie(res, WLAN_EID_VHT_OPERATION);
2047 if (ie && ie[1] >= 1 &&
2048 (ie[2] & VHT_OPMODE_CHANNEL_WIDTH_MASK)) {
2049 tmp = max_vht80_rate(snr) + 1;
2050 if (tmp > est)
2051 est = tmp;
2052 }
2053 }
2054 }
2055
2056 /* TODO: channel utilization and AP load (e.g., from AP Beacon) */
2057
2058 res->est_throughput = est;
2059}
2060
2061
9ba9fa07
JM
2062/**
2063 * wpa_supplicant_get_scan_results - Get scan results
2064 * @wpa_s: Pointer to wpa_supplicant data
2065 * @info: Information about what was scanned or %NULL if not available
2066 * @new_scan: Whether a new scan was performed
2067 * Returns: Scan results, %NULL on failure
2068 *
2069 * This function request the current scan results from the driver and updates
2070 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
2071 * results with wpa_scan_results_free().
2072 */
2073struct wpa_scan_results *
2074wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
2075 struct scan_info *info, int new_scan)
2076{
2077 struct wpa_scan_results *scan_res;
2078 size_t i;
41e650ae 2079 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
9ba9fa07 2080
17fbb751 2081 scan_res = wpa_drv_get_scan_results2(wpa_s);
9ba9fa07 2082 if (scan_res == NULL) {
f049052b 2083 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
9ba9fa07
JM
2084 return NULL;
2085 }
c5f10e80
JM
2086 if (scan_res->fetch_time.sec == 0) {
2087 /*
2088 * Make sure we have a valid timestamp if the driver wrapper
2089 * does not set this.
2090 */
acb69cec 2091 os_get_reltime(&scan_res->fetch_time);
c5f10e80 2092 }
d445a5cd 2093 filter_scan_res(wpa_s, scan_res);
9ba9fa07 2094
f0d0a5d2
MA
2095 for (i = 0; i < scan_res->num; i++) {
2096 struct wpa_scan_res *scan_res_item = scan_res->res[i];
2097
a1b790eb
JM
2098 scan_snr(scan_res_item);
2099 scan_est_throughput(wpa_s, scan_res_item);
f0d0a5d2
MA
2100 }
2101
41e650ae 2102#ifdef CONFIG_WPS
3187fd90 2103 if (wpas_wps_searching(wpa_s)) {
f049052b
BG
2104 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
2105 "provisioning rules");
41e650ae
JM
2106 compar = wpa_scan_result_wps_compar;
2107 }
2108#endif /* CONFIG_WPS */
2109
9ba9fa07 2110 qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
41e650ae 2111 compar);
aa820e02 2112 dump_scan_res(scan_res);
9ba9fa07
JM
2113
2114 wpa_bss_update_start(wpa_s);
2115 for (i = 0; i < scan_res->num; i++)
c5f10e80
JM
2116 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
2117 &scan_res->fetch_time);
9ba9fa07
JM
2118 wpa_bss_update_end(wpa_s, info, new_scan);
2119
2120 return scan_res;
2121}
2122
2123
2c09af30
JM
2124/**
2125 * wpa_supplicant_update_scan_results - Update scan results from the driver
2126 * @wpa_s: Pointer to wpa_supplicant data
2127 * Returns: 0 on success, -1 on failure
2128 *
2129 * This function updates the BSS table within wpa_supplicant based on the
2130 * currently available scan results from the driver without requesting a new
2131 * scan. This is used in cases where the driver indicates an association
2132 * (including roaming within ESS) and wpa_supplicant does not yet have the
2133 * needed information to complete the connection (e.g., to perform validation
2134 * steps in 4-way handshake).
2135 */
9ba9fa07
JM
2136int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
2137{
2138 struct wpa_scan_results *scan_res;
2139 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
2140 if (scan_res == NULL)
2141 return -1;
2142 wpa_scan_results_free(scan_res);
2143
2144 return 0;
2145}
66fe0f70
DS
2146
2147
2148/**
2149 * scan_only_handler - Reports scan results
2150 */
2151void scan_only_handler(struct wpa_supplicant *wpa_s,
2152 struct wpa_scan_results *scan_res)
2153{
2154 wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
d81c73be
JM
2155 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
2156 wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
2157 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
2158 wpa_s->manual_scan_id);
2159 wpa_s->manual_scan_use_id = 0;
2160 } else {
2161 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
2162 }
66fe0f70
DS
2163 wpas_notify_scan_results(wpa_s);
2164 wpas_notify_scan_done(wpa_s, 1);
d12a51b5
JM
2165 if (wpa_s->scan_work) {
2166 struct wpa_radio_work *work = wpa_s->scan_work;
2167 wpa_s->scan_work = NULL;
2168 radio_work_done(work);
2169 }
ea6030c7
JM
2170
2171 if (wpa_s->wpa_state == WPA_SCANNING)
2172 wpa_supplicant_set_state(wpa_s, wpa_s->scan_prev_wpa_state);
66fe0f70 2173}
407be00b
JM
2174
2175
2176int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
2177{
2178 return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
2179}
dd43aaa5
JM
2180
2181
2182struct wpa_driver_scan_params *
2183wpa_scan_clone_params(const struct wpa_driver_scan_params *src)
2184{
2185 struct wpa_driver_scan_params *params;
2186 size_t i;
2187 u8 *n;
2188
2189 params = os_zalloc(sizeof(*params));
2190 if (params == NULL)
2191 return NULL;
2192
2193 for (i = 0; i < src->num_ssids; i++) {
2194 if (src->ssids[i].ssid) {
2195 n = os_malloc(src->ssids[i].ssid_len);
2196 if (n == NULL)
2197 goto failed;
2198 os_memcpy(n, src->ssids[i].ssid,
2199 src->ssids[i].ssid_len);
2200 params->ssids[i].ssid = n;
2201 params->ssids[i].ssid_len = src->ssids[i].ssid_len;
2202 }
2203 }
2204 params->num_ssids = src->num_ssids;
2205
2206 if (src->extra_ies) {
2207 n = os_malloc(src->extra_ies_len);
2208 if (n == NULL)
2209 goto failed;
2210 os_memcpy(n, src->extra_ies, src->extra_ies_len);
2211 params->extra_ies = n;
2212 params->extra_ies_len = src->extra_ies_len;
2213 }
2214
2215 if (src->freqs) {
2216 int len = int_array_len(src->freqs);
2217 params->freqs = os_malloc((len + 1) * sizeof(int));
2218 if (params->freqs == NULL)
2219 goto failed;
2220 os_memcpy(params->freqs, src->freqs, (len + 1) * sizeof(int));
2221 }
2222
2223 if (src->filter_ssids) {
fd67275b 2224 params->filter_ssids = os_malloc(sizeof(*params->filter_ssids) *
dd43aaa5
JM
2225 src->num_filter_ssids);
2226 if (params->filter_ssids == NULL)
2227 goto failed;
2228 os_memcpy(params->filter_ssids, src->filter_ssids,
fd67275b
EL
2229 sizeof(*params->filter_ssids) *
2230 src->num_filter_ssids);
dd43aaa5
JM
2231 params->num_filter_ssids = src->num_filter_ssids;
2232 }
2233
2234 params->filter_rssi = src->filter_rssi;
2235 params->p2p_probe = src->p2p_probe;
2236 params->only_new_results = src->only_new_results;
57a8f8af 2237 params->low_priority = src->low_priority;
dd43aaa5 2238
09ea4309
AS
2239 if (src->sched_scan_plans_num > 0) {
2240 params->sched_scan_plans =
2241 os_malloc(sizeof(*src->sched_scan_plans) *
2242 src->sched_scan_plans_num);
2243 if (!params->sched_scan_plans)
2244 goto failed;
2245
2246 os_memcpy(params->sched_scan_plans, src->sched_scan_plans,
2247 sizeof(*src->sched_scan_plans) *
2248 src->sched_scan_plans_num);
2249 params->sched_scan_plans_num = src->sched_scan_plans_num;
2250 }
2251
56c76fa5
IP
2252 if (src->mac_addr_rand) {
2253 params->mac_addr_rand = src->mac_addr_rand;
2254
2255 if (src->mac_addr && src->mac_addr_mask) {
2256 u8 *mac_addr;
2257
2258 mac_addr = os_malloc(2 * ETH_ALEN);
2259 if (!mac_addr)
2260 goto failed;
2261
2262 os_memcpy(mac_addr, src->mac_addr, ETH_ALEN);
2263 os_memcpy(mac_addr + ETH_ALEN, src->mac_addr_mask,
2264 ETH_ALEN);
2265 params->mac_addr = mac_addr;
2266 params->mac_addr_mask = mac_addr + ETH_ALEN;
2267 }
2268 }
dd43aaa5
JM
2269 return params;
2270
2271failed:
2272 wpa_scan_free_params(params);
2273 return NULL;
2274}
2275
2276
2277void wpa_scan_free_params(struct wpa_driver_scan_params *params)
2278{
2279 size_t i;
2280
2281 if (params == NULL)
2282 return;
2283
2284 for (i = 0; i < params->num_ssids; i++)
2285 os_free((u8 *) params->ssids[i].ssid);
2286 os_free((u8 *) params->extra_ies);
2287 os_free(params->freqs);
2288 os_free(params->filter_ssids);
09ea4309 2289 os_free(params->sched_scan_plans);
56c76fa5
IP
2290
2291 /*
2292 * Note: params->mac_addr_mask points to same memory allocation and
2293 * must not be freed separately.
2294 */
2295 os_free((u8 *) params->mac_addr);
2296
dd43aaa5
JM
2297 os_free(params);
2298}
737e7a08
AB
2299
2300
2301int wpas_start_pno(struct wpa_supplicant *wpa_s)
2302{
32c02261 2303 int ret, prio;
4ed34922 2304 size_t i, num_ssid, num_match_ssid;
737e7a08
AB
2305 struct wpa_ssid *ssid;
2306 struct wpa_driver_scan_params params;
32c02261 2307 struct sched_scan_plan scan_plan;
737e7a08 2308
5e3ddf4d
AB
2309 if (!wpa_s->sched_scan_supported)
2310 return -1;
2311
737e7a08
AB
2312 if (wpa_s->pno || wpa_s->pno_sched_pending)
2313 return 0;
2314
2315 if ((wpa_s->wpa_state > WPA_SCANNING) &&
2316 (wpa_s->wpa_state <= WPA_COMPLETED)) {
2317 wpa_printf(MSG_ERROR, "PNO: In assoc process");
2318 return -EAGAIN;
2319 }
2320
2321 if (wpa_s->wpa_state == WPA_SCANNING) {
2322 wpa_supplicant_cancel_scan(wpa_s);
2323 if (wpa_s->sched_scanning) {
2324 wpa_printf(MSG_DEBUG, "Schedule PNO on completion of "
2325 "ongoing sched scan");
2326 wpa_supplicant_cancel_sched_scan(wpa_s);
2327 wpa_s->pno_sched_pending = 1;
2328 return 0;
2329 }
2330 }
2331
2332 os_memset(&params, 0, sizeof(params));
2333
4ed34922 2334 num_ssid = num_match_ssid = 0;
737e7a08
AB
2335 ssid = wpa_s->conf->ssid;
2336 while (ssid) {
4ed34922
DS
2337 if (!wpas_network_disabled(wpa_s, ssid)) {
2338 num_match_ssid++;
2339 if (ssid->scan_ssid)
2340 num_ssid++;
2341 }
737e7a08
AB
2342 ssid = ssid->next;
2343 }
4ed34922
DS
2344
2345 if (num_match_ssid == 0) {
2346 wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
2347 return -1;
2348 }
2349
2350 if (num_match_ssid > num_ssid) {
2351 params.num_ssids++; /* wildcard */
2352 num_ssid++;
2353 }
2354
737e7a08
AB
2355 if (num_ssid > WPAS_MAX_SCAN_SSIDS) {
2356 wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
2357 "%u", WPAS_MAX_SCAN_SSIDS, (unsigned int) num_ssid);
2358 num_ssid = WPAS_MAX_SCAN_SSIDS;
2359 }
2360
4ed34922
DS
2361 if (num_match_ssid > wpa_s->max_match_sets) {
2362 num_match_ssid = wpa_s->max_match_sets;
2363 wpa_dbg(wpa_s, MSG_DEBUG, "PNO: Too many SSIDs to match");
737e7a08 2364 }
4ed34922
DS
2365 params.filter_ssids = os_calloc(num_match_ssid,
2366 sizeof(struct wpa_driver_scan_filter));
737e7a08
AB
2367 if (params.filter_ssids == NULL)
2368 return -1;
6f5e1b0b 2369
737e7a08 2370 i = 0;
6f5e1b0b
DS
2371 prio = 0;
2372 ssid = wpa_s->conf->pssid[prio];
737e7a08
AB
2373 while (ssid) {
2374 if (!wpas_network_disabled(wpa_s, ssid)) {
4ed34922
DS
2375 if (ssid->scan_ssid && params.num_ssids < num_ssid) {
2376 params.ssids[params.num_ssids].ssid =
2377 ssid->ssid;
2378 params.ssids[params.num_ssids].ssid_len =
2379 ssid->ssid_len;
2380 params.num_ssids++;
2381 }
737e7a08
AB
2382 os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
2383 ssid->ssid_len);
2384 params.filter_ssids[i].ssid_len = ssid->ssid_len;
2385 params.num_filter_ssids++;
2386 i++;
4ed34922 2387 if (i == num_match_ssid)
737e7a08
AB
2388 break;
2389 }
6f5e1b0b
DS
2390 if (ssid->pnext)
2391 ssid = ssid->pnext;
2392 else if (prio + 1 == wpa_s->conf->num_prio)
2393 break;
2394 else
2395 ssid = wpa_s->conf->pssid[++prio];
737e7a08
AB
2396 }
2397
2398 if (wpa_s->conf->filter_rssi)
2399 params.filter_rssi = wpa_s->conf->filter_rssi;
2400
32c02261
AS
2401 if (wpa_s->sched_scan_plans_num) {
2402 params.sched_scan_plans = wpa_s->sched_scan_plans;
2403 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
2404 } else {
2405 /* Set one scan plan that will run infinitely */
2406 if (wpa_s->conf->sched_scan_interval)
2407 scan_plan.interval = wpa_s->conf->sched_scan_interval;
2408 else
2409 scan_plan.interval = 10;
2410
2411 scan_plan.iterations = 0;
2412 params.sched_scan_plans = &scan_plan;
2413 params.sched_scan_plans_num = 1;
2414 }
737e7a08 2415
d3c9c35f
DS
2416 if (params.freqs == NULL && wpa_s->manual_sched_scan_freqs) {
2417 wpa_dbg(wpa_s, MSG_DEBUG, "Limit sched scan to specified channels");
2418 params.freqs = wpa_s->manual_sched_scan_freqs;
2419 }
2420
56c76fa5
IP
2421 if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_PNO) {
2422 params.mac_addr_rand = 1;
2423 if (wpa_s->mac_addr_pno) {
2424 params.mac_addr = wpa_s->mac_addr_pno;
2425 params.mac_addr_mask = wpa_s->mac_addr_pno + ETH_ALEN;
2426 }
2427 }
2428
32c02261 2429 ret = wpa_supplicant_start_sched_scan(wpa_s, &params);
737e7a08
AB
2430 os_free(params.filter_ssids);
2431 if (ret == 0)
2432 wpa_s->pno = 1;
5e3ddf4d
AB
2433 else
2434 wpa_msg(wpa_s, MSG_ERROR, "Failed to schedule PNO");
737e7a08
AB
2435 return ret;
2436}
2437
2438
2439int wpas_stop_pno(struct wpa_supplicant *wpa_s)
2440{
2441 int ret = 0;
2442
5e3ddf4d
AB
2443 if (!wpa_s->pno)
2444 return 0;
2445
2446 ret = wpa_supplicant_stop_sched_scan(wpa_s);
737e7a08 2447
5e3ddf4d 2448 wpa_s->pno = 0;
737e7a08
AB
2449 wpa_s->pno_sched_pending = 0;
2450
2451 if (wpa_s->wpa_state == WPA_SCANNING)
2452 wpa_supplicant_req_scan(wpa_s, 0, 0);
2453
2454 return ret;
2455}
56c76fa5
IP
2456
2457
2458void wpas_mac_addr_rand_scan_clear(struct wpa_supplicant *wpa_s,
2459 unsigned int type)
2460{
2461 type &= MAC_ADDR_RAND_ALL;
2462 wpa_s->mac_addr_rand_enable &= ~type;
2463
2464 if (type & MAC_ADDR_RAND_SCAN) {
2465 os_free(wpa_s->mac_addr_scan);
2466 wpa_s->mac_addr_scan = NULL;
2467 }
2468
2469 if (type & MAC_ADDR_RAND_SCHED_SCAN) {
2470 os_free(wpa_s->mac_addr_sched_scan);
2471 wpa_s->mac_addr_sched_scan = NULL;
2472 }
2473
2474 if (type & MAC_ADDR_RAND_PNO) {
2475 os_free(wpa_s->mac_addr_pno);
2476 wpa_s->mac_addr_pno = NULL;
2477 }
2478}
2479
2480
2481int wpas_mac_addr_rand_scan_set(struct wpa_supplicant *wpa_s,
2482 unsigned int type, const u8 *addr,
2483 const u8 *mask)
2484{
2485 u8 *tmp = NULL;
2486
2487 wpas_mac_addr_rand_scan_clear(wpa_s, type);
2488
2489 if (addr) {
2490 tmp = os_malloc(2 * ETH_ALEN);
2491 if (!tmp)
2492 return -1;
2493 os_memcpy(tmp, addr, ETH_ALEN);
2494 os_memcpy(tmp + ETH_ALEN, mask, ETH_ALEN);
2495 }
2496
2497 if (type == MAC_ADDR_RAND_SCAN) {
2498 wpa_s->mac_addr_scan = tmp;
2499 } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
2500 wpa_s->mac_addr_sched_scan = tmp;
2501 } else if (type == MAC_ADDR_RAND_PNO) {
2502 wpa_s->mac_addr_pno = tmp;
2503 } else {
2504 wpa_printf(MSG_INFO,
2505 "scan: Invalid MAC randomization type=0x%x",
2506 type);
2507 os_free(tmp);
2508 return -1;
2509 }
2510
2511 wpa_s->mac_addr_rand_enable |= type;
2512 return 0;
2513}
4ead7cfd
KV
2514
2515
2516int wpas_abort_ongoing_scan(struct wpa_supplicant *wpa_s)
2517{
2518 if (wpa_s->scan_work && wpa_s->own_scan_running) {
2519 wpa_dbg(wpa_s, MSG_DEBUG, "Abort an ongoing scan");
2520 return wpa_drv_abort_scan(wpa_s);
2521 }
2522
2523 return 0;
2524}
32c02261
AS
2525
2526
2527int wpas_sched_scan_plans_set(struct wpa_supplicant *wpa_s, const char *cmd)
2528{
2529 struct sched_scan_plan *scan_plans = NULL;
2530 const char *token, *context = NULL;
2531 unsigned int num = 0;
2532
2533 if (!cmd)
2534 return -1;
2535
bea48f77
JM
2536 if (!cmd[0]) {
2537 wpa_printf(MSG_DEBUG, "Clear sched scan plans");
2538 os_free(wpa_s->sched_scan_plans);
2539 wpa_s->sched_scan_plans = NULL;
2540 wpa_s->sched_scan_plans_num = 0;
2541 return 0;
2542 }
2543
32c02261
AS
2544 while ((token = cstr_token(cmd, " ", &context))) {
2545 int ret;
2546 struct sched_scan_plan *scan_plan, *n;
2547
2548 n = os_realloc_array(scan_plans, num + 1, sizeof(*scan_plans));
2549 if (!n)
2550 goto fail;
2551
2552 scan_plans = n;
2553 scan_plan = &scan_plans[num];
2554 num++;
2555
2556 ret = sscanf(token, "%u:%u", &scan_plan->interval,
2557 &scan_plan->iterations);
2558 if (ret <= 0 || ret > 2 || !scan_plan->interval) {
2559 wpa_printf(MSG_ERROR,
2560 "Invalid sched scan plan input: %s", token);
2561 goto fail;
2562 }
2563
2564 if (!scan_plan->interval) {
2565 wpa_printf(MSG_ERROR,
2566 "scan plan %u: Interval cannot be zero",
2567 num);
2568 goto fail;
2569 }
2570
2571 if (scan_plan->interval > wpa_s->max_sched_scan_plan_interval) {
2572 wpa_printf(MSG_WARNING,
2573 "scan plan %u: Scan interval too long(%u), use the maximum allowed(%u)",
2574 num, scan_plan->interval,
2575 wpa_s->max_sched_scan_plan_interval);
2576 scan_plan->interval =
2577 wpa_s->max_sched_scan_plan_interval;
2578 }
2579
2580 if (ret == 1) {
2581 scan_plan->iterations = 0;
2582 break;
2583 }
2584
2585 if (!scan_plan->iterations) {
2586 wpa_printf(MSG_ERROR,
2587 "scan plan %u: Number of iterations cannot be zero",
2588 num);
2589 goto fail;
2590 }
2591
2592 if (scan_plan->iterations >
2593 wpa_s->max_sched_scan_plan_iterations) {
2594 wpa_printf(MSG_WARNING,
2595 "scan plan %u: Too many iterations(%u), use the maximum allowed(%u)",
2596 num, scan_plan->iterations,
2597 wpa_s->max_sched_scan_plan_iterations);
2598 scan_plan->iterations =
2599 wpa_s->max_sched_scan_plan_iterations;
2600 }
2601
2602 wpa_printf(MSG_DEBUG,
2603 "scan plan %u: interval=%u iterations=%u",
2604 num, scan_plan->interval, scan_plan->iterations);
2605 }
2606
2607 if (!scan_plans) {
2608 wpa_printf(MSG_ERROR, "Invalid scan plans entry");
2609 goto fail;
2610 }
2611
2612 if (cstr_token(cmd, " ", &context) || scan_plans[num - 1].iterations) {
2613 wpa_printf(MSG_ERROR,
2614 "All scan plans but the last must specify a number of iterations");
2615 goto fail;
2616 }
2617
2618 wpa_printf(MSG_DEBUG, "scan plan %u (last plan): interval=%u",
2619 num, scan_plans[num - 1].interval);
2620
2621 if (num > wpa_s->max_sched_scan_plans) {
2622 wpa_printf(MSG_WARNING,
2623 "Too many scheduled scan plans (only %u supported)",
2624 wpa_s->max_sched_scan_plans);
2625 wpa_printf(MSG_WARNING,
2626 "Use only the first %u scan plans, and the last one (in infinite loop)",
2627 wpa_s->max_sched_scan_plans - 1);
2628 os_memcpy(&scan_plans[wpa_s->max_sched_scan_plans - 1],
2629 &scan_plans[num - 1], sizeof(*scan_plans));
2630 num = wpa_s->max_sched_scan_plans;
2631 }
2632
2633 os_free(wpa_s->sched_scan_plans);
2634 wpa_s->sched_scan_plans = scan_plans;
2635 wpa_s->sched_scan_plans_num = num;
2636
2637 return 0;
2638
2639fail:
2640 os_free(scan_plans);
2641 wpa_printf(MSG_ERROR, "invalid scan plans list");
2642 return -1;
2643}