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