blob: 4ae559ef549e3161828d740f14dcb3fb6fa32fd6 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant - Scanning
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003 * Copyright (c) 2003-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
12#include "utils/eloop.h"
13#include "common/ieee802_11_defs.h"
Dmitry Shmidt3a787e62013-01-17 10:32:35 -080014#include "common/wpa_ctrl.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070015#include "config.h"
16#include "wpa_supplicant_i.h"
17#include "driver_i.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070018#include "wps_supplicant.h"
19#include "p2p_supplicant.h"
20#include "p2p/p2p.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070021#include "hs20_supplicant.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070022#include "notify.h"
23#include "bss.h"
24#include "scan.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080025#include "mesh.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070026
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
37 if (wpa_s->current_ssid == NULL) {
38 wpa_s->current_ssid = ssid;
Dmitry Shmidt849734c2016-05-27 09:59:01 -070039 wpas_notify_network_changed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070040 }
41 wpa_supplicant_initiate_eapol(wpa_s);
42 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with a configured "
43 "network - generating associated event");
44 os_memset(&data, 0, sizeof(data));
45 wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
46}
47
48
49#ifdef CONFIG_WPS
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080050static int wpas_wps_in_use(struct wpa_supplicant *wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070051 enum wps_request_type *req_type)
52{
53 struct wpa_ssid *ssid;
54 int wps = 0;
55
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080056 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070057 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
58 continue;
59
60 wps = 1;
61 *req_type = wpas_wps_get_req_type(ssid);
Dmitry Shmidt849734c2016-05-27 09:59:01 -070062 if (ssid->eap.phase1 && os_strstr(ssid->eap.phase1, "pbc=1"))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070063 return 2;
64 }
65
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080066#ifdef CONFIG_P2P
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080067 if (!wpa_s->global->p2p_disabled && wpa_s->global->p2p &&
68 !wpa_s->conf->p2p_disabled) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080069 wpa_s->wps->dev.p2p = 1;
70 if (!wps) {
71 wps = 1;
72 *req_type = WPS_REQ_ENROLLEE_INFO;
73 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080074 }
75#endif /* CONFIG_P2P */
76
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070077 return wps;
78}
79#endif /* CONFIG_WPS */
80
81
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080082/**
83 * wpa_supplicant_enabled_networks - Check whether there are enabled networks
84 * @wpa_s: Pointer to wpa_supplicant data
85 * Returns: 0 if no networks are enabled, >0 if networks are enabled
86 *
87 * This function is used to figure out whether any networks (or Interworking
88 * with enabled credentials and auto_interworking) are present in the current
89 * configuration.
90 */
Dmitry Shmidt04949592012-07-19 12:16:46 -070091int wpa_supplicant_enabled_networks(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070092{
Dmitry Shmidt04949592012-07-19 12:16:46 -070093 struct wpa_ssid *ssid = wpa_s->conf->ssid;
Dmitry Shmidtaa532512012-09-24 10:35:31 -070094 int count = 0, disabled = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -080095
96 if (wpa_s->p2p_mgmt)
97 return 0; /* no normal network profiles on p2p_mgmt interface */
98
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070099 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700100 if (!wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700101 count++;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700102 else
103 disabled++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700104 ssid = ssid->next;
105 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700106 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
107 wpa_s->conf->auto_interworking)
108 count++;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700109 if (count == 0 && disabled > 0) {
110 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks (%d disabled "
111 "networks)", disabled);
112 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700113 return count;
114}
115
116
117static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
118 struct wpa_ssid *ssid)
119{
120 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700121 if (!wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700122 break;
123 ssid = ssid->next;
124 }
125
126 /* ap_scan=2 mode - try to associate with each SSID. */
127 if (ssid == NULL) {
128 wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
129 "end of scan list - go back to beginning");
130 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
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. */
139 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
140 }
141 wpa_supplicant_associate(wpa_s, NULL, ssid);
142}
143
144
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800145static void wpas_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700146{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800147 struct wpa_supplicant *wpa_s = work->wpa_s;
148 struct wpa_driver_scan_params *params = work->ctx;
149 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700150
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800151 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800152 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;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700159 return;
160 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700161
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700162 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");
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700165 wpa_scan_free_params(params);
166 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=-1");
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700167 radio_work_done(work);
168 return;
169 }
170
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800171 wpa_supplicant_notify_scanning(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700172
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800173 if (wpa_s->clear_driver_scan_cache) {
174 wpa_printf(MSG_DEBUG,
175 "Request driver to clear scan cache due to local BSS flush");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800176 params->only_new_results = 1;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800177 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800178 ret = wpa_drv_scan(wpa_s, params);
179 wpa_scan_free_params(params);
180 work->ctx = NULL;
181 if (ret) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800182 int retry = wpa_s->last_scan_req != MANUAL_SCAN_REQ;
183
184 if (wpa_s->disconnected)
185 retry = 0;
186
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800187 wpa_supplicant_notify_scanning(wpa_s, 0);
188 wpas_notify_scan_done(wpa_s, 0);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800189 if (wpa_s->wpa_state == WPA_SCANNING)
190 wpa_supplicant_set_state(wpa_s,
191 wpa_s->scan_prev_wpa_state);
192 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=%d%s",
193 ret, retry ? " retry=1" : "");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800194 radio_work_done(work);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800195
196 if (retry) {
197 /* Restore scan_req since we will try to scan again */
198 wpa_s->scan_req = wpa_s->last_scan_req;
199 wpa_supplicant_req_scan(wpa_s, 1, 0);
200 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700201 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700202 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800203
204 os_get_reltime(&wpa_s->scan_trigger_time);
205 wpa_s->scan_runs++;
206 wpa_s->normal_scans++;
207 wpa_s->own_scan_requested = 1;
208 wpa_s->clear_driver_scan_cache = 0;
209 wpa_s->scan_work = work;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700210}
211
212
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800213/**
214 * wpa_supplicant_trigger_scan - Request driver to start a scan
215 * @wpa_s: Pointer to wpa_supplicant data
216 * @params: Scan parameters
217 * Returns: 0 on success, -1 on failure
218 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700219int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
220 struct wpa_driver_scan_params *params)
221{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800222 struct wpa_driver_scan_params *ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700223
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800224 if (wpa_s->scan_work) {
225 wpa_dbg(wpa_s, MSG_INFO, "Reject scan trigger since one is already pending");
226 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800227 }
228
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800229 ctx = wpa_scan_clone_params(params);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700230 if (!ctx ||
231 radio_add_work(wpa_s, 0, "scan", 0, wpas_trigger_scan_cb, ctx) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800232 {
233 wpa_scan_free_params(ctx);
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700234 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=-1");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800235 return -1;
236 }
237
238 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800239}
240
241
242static void
243wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
244{
245 struct wpa_supplicant *wpa_s = eloop_ctx;
246
247 wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
248
249 if (wpa_supplicant_req_sched_scan(wpa_s))
250 wpa_supplicant_req_scan(wpa_s, 0, 0);
251}
252
253
254static void
255wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
256{
257 struct wpa_supplicant *wpa_s = eloop_ctx;
258
259 wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
260
261 wpa_s->sched_scan_timed_out = 1;
262 wpa_supplicant_cancel_sched_scan(wpa_s);
263}
264
265
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700266static int
267wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
268 struct wpa_driver_scan_params *params)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800269{
270 int ret;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700271
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800272 wpa_supplicant_notify_scanning(wpa_s, 1);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800273 ret = wpa_drv_sched_scan(wpa_s, params);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800274 if (ret)
275 wpa_supplicant_notify_scanning(wpa_s, 0);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800276 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800277 wpa_s->sched_scanning = 1;
278
279 return ret;
280}
281
282
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700283static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800284{
285 int ret;
286
287 ret = wpa_drv_stop_sched_scan(wpa_s);
288 if (ret) {
289 wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
290 /* TODO: what to do if stopping fails? */
291 return -1;
292 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700293
294 return ret;
295}
296
297
298static struct wpa_driver_scan_filter *
299wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
300{
301 struct wpa_driver_scan_filter *ssids;
302 struct wpa_ssid *ssid;
303 size_t count;
304
305 *num_ssids = 0;
306 if (!conf->filter_ssids)
307 return NULL;
308
309 for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
310 if (ssid->ssid && ssid->ssid_len)
311 count++;
312 }
313 if (count == 0)
314 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800315 ssids = os_calloc(count, sizeof(struct wpa_driver_scan_filter));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700316 if (ssids == NULL)
317 return NULL;
318
319 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
320 if (!ssid->ssid || !ssid->ssid_len)
321 continue;
322 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
323 ssids[*num_ssids].ssid_len = ssid->ssid_len;
324 (*num_ssids)++;
325 }
326
327 return ssids;
328}
329
330
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800331static void wpa_supplicant_optimize_freqs(
332 struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
333{
334#ifdef CONFIG_P2P
335 if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
336 wpa_s->go_params) {
337 /* Optimize provisioning state scan based on GO information */
338 if (wpa_s->p2p_in_provisioning < 5 &&
339 wpa_s->go_params->freq > 0) {
340 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
341 "preferred frequency %d MHz",
342 wpa_s->go_params->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800343 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800344 if (params->freqs)
345 params->freqs[0] = wpa_s->go_params->freq;
346 } else if (wpa_s->p2p_in_provisioning < 8 &&
347 wpa_s->go_params->freq_list[0]) {
348 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
349 "channels");
350 int_array_concat(&params->freqs,
351 wpa_s->go_params->freq_list);
352 if (params->freqs)
353 int_array_sort_unique(params->freqs);
354 }
355 wpa_s->p2p_in_provisioning++;
356 }
Dmitry Shmidt15907092014-03-25 10:42:57 -0700357
358 if (params->freqs == NULL && wpa_s->p2p_in_invitation) {
359 /*
360 * Optimize scan based on GO information during persistent
361 * group reinvocation
362 */
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -0700363 if (wpa_s->p2p_in_invitation < 5 &&
Dmitry Shmidt15907092014-03-25 10:42:57 -0700364 wpa_s->p2p_invite_go_freq > 0) {
365 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO preferred frequency %d MHz during invitation",
366 wpa_s->p2p_invite_go_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800367 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidt15907092014-03-25 10:42:57 -0700368 if (params->freqs)
369 params->freqs[0] = wpa_s->p2p_invite_go_freq;
370 }
371 wpa_s->p2p_in_invitation++;
372 if (wpa_s->p2p_in_invitation > 20) {
373 /*
374 * This should not really happen since the variable is
375 * cleared on group removal, but if it does happen, make
376 * sure we do not get stuck in special invitation scan
377 * mode.
378 */
379 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Clear p2p_in_invitation");
380 wpa_s->p2p_in_invitation = 0;
381 }
382 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800383#endif /* CONFIG_P2P */
384
385#ifdef CONFIG_WPS
386 if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
387 /*
388 * Optimize post-provisioning scan based on channel used
389 * during provisioning.
390 */
391 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
392 "that was used during provisioning", wpa_s->wps_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800393 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800394 if (params->freqs)
395 params->freqs[0] = wpa_s->wps_freq;
396 wpa_s->after_wps--;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800397 } else if (wpa_s->after_wps)
398 wpa_s->after_wps--;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800399
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800400 if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
401 {
402 /* Optimize provisioning scan based on already known channel */
403 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
404 wpa_s->wps_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800405 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800406 if (params->freqs)
407 params->freqs[0] = wpa_s->wps_freq;
408 wpa_s->known_wps_freq = 0; /* only do this once */
409 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800410#endif /* CONFIG_WPS */
411}
412
413
414#ifdef CONFIG_INTERWORKING
415static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
416 struct wpabuf *buf)
417{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800418 wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
419 wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
420 1 + ETH_ALEN);
421 wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
422 /* No Venue Info */
423 if (!is_zero_ether_addr(wpa_s->conf->hessid))
424 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
425}
426#endif /* CONFIG_INTERWORKING */
427
428
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700429static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800430{
431 struct wpabuf *extra_ie = NULL;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700432 u8 ext_capab[18];
433 int ext_capab_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800434#ifdef CONFIG_WPS
435 int wps = 0;
436 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
437#endif /* CONFIG_WPS */
438
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700439#ifdef CONFIG_P2P
440 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT)
441 wpa_drv_get_ext_capa(wpa_s, WPA_IF_P2P_CLIENT);
442 else
443#endif /* CONFIG_P2P */
444 wpa_drv_get_ext_capa(wpa_s, WPA_IF_STATION);
445
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700446 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
447 sizeof(ext_capab));
448 if (ext_capab_len > 0 &&
449 wpabuf_resize(&extra_ie, ext_capab_len) == 0)
450 wpabuf_put_data(extra_ie, ext_capab, ext_capab_len);
451
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800452#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 */
457
458#ifdef CONFIG_WPS
459 wps = wpas_wps_in_use(wpa_s, &req_type);
460
461 if (wps) {
462 struct wpabuf *wps_ie;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700463 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
464 DEV_PW_DEFAULT,
465 &wpa_s->wps->dev,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800466 wpa_s->wps->uuid, req_type,
467 0, NULL);
468 if (wps_ie) {
469 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
470 wpabuf_put_buf(extra_ie, wps_ie);
471 wpabuf_free(wps_ie);
472 }
473 }
474
475#ifdef CONFIG_P2P
476 if (wps) {
477 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
478 if (wpabuf_resize(&extra_ie, ielen) == 0)
479 wpas_p2p_scan_ie(wpa_s, extra_ie);
480 }
481#endif /* CONFIG_P2P */
482
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800483 wpa_supplicant_mesh_add_scan_ie(wpa_s, &extra_ie);
484
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800485#endif /* CONFIG_WPS */
486
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700487#ifdef CONFIG_HS20
488 if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 7) == 0)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800489 wpas_hs20_add_indication(extra_ie, -1);
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700490#endif /* CONFIG_HS20 */
491
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800492#ifdef CONFIG_FST
493 if (wpa_s->fst_ies &&
494 wpabuf_resize(&extra_ie, wpabuf_len(wpa_s->fst_ies)) == 0)
495 wpabuf_put_buf(extra_ie, wpa_s->fst_ies);
496#endif /* CONFIG_FST */
497
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800498#ifdef CONFIG_MBO
499 /* Send cellular capabilities for potential MBO STAs */
500 if (wpabuf_resize(&extra_ie, 9) == 0)
501 wpas_mbo_scan_ie(wpa_s, extra_ie);
502#endif /* CONFIG_MBO */
503
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700504 if (wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ]) {
505 struct wpabuf *buf = wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ];
506
507 if (wpabuf_resize(&extra_ie, wpabuf_len(buf)) == 0)
508 wpabuf_put_buf(extra_ie, buf);
509 }
510
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800511 return extra_ie;
512}
513
514
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800515#ifdef CONFIG_P2P
516
517/*
518 * Check whether there are any enabled networks or credentials that could be
519 * used for a non-P2P connection.
520 */
521static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
522{
523 struct wpa_ssid *ssid;
524
525 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
526 if (wpas_network_disabled(wpa_s, ssid))
527 continue;
528 if (!ssid->p2p_group)
529 return 1;
530 }
531
532 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
533 wpa_s->conf->auto_interworking)
534 return 1;
535
536 return 0;
537}
538
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700539#endif /* CONFIG_P2P */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800540
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800541
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700542static void wpa_setband_scan_freqs_list(struct wpa_supplicant *wpa_s,
543 enum hostapd_hw_mode band,
544 struct wpa_driver_scan_params *params)
545{
546 /* Include only supported channels for the specified band */
547 struct hostapd_hw_modes *mode;
548 int count, i;
549
550 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band);
551 if (mode == NULL) {
552 /* No channels supported in this band - use empty list */
553 params->freqs = os_zalloc(sizeof(int));
554 return;
555 }
556
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800557 params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700558 if (params->freqs == NULL)
559 return;
560 for (count = 0, i = 0; i < mode->num_channels; i++) {
561 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
562 continue;
563 params->freqs[count++] = mode->channels[i].freq;
564 }
565}
566
567
568static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
569 struct wpa_driver_scan_params *params)
570{
571 if (wpa_s->hw.modes == NULL)
572 return; /* unknown what channels the driver supports */
573 if (params->freqs)
574 return; /* already using a limited channel set */
575 if (wpa_s->setband == WPA_SETBAND_5G)
576 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
577 params);
578 else if (wpa_s->setband == WPA_SETBAND_2G)
579 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G,
580 params);
581}
582
583
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700584static void wpa_set_scan_ssids(struct wpa_supplicant *wpa_s,
585 struct wpa_driver_scan_params *params,
586 size_t max_ssids)
587{
588 unsigned int i;
589 struct wpa_ssid *ssid;
Roshan Piuse382b882016-06-29 15:37:57 -0700590 /*
591 * For devices with |max_ssids| greater than 1, leave the last slot empty
592 * for adding the wildcard scan entry.
593 */
594 max_ssids = (max_ssids == 1) ? max_ssids : max_ssids - 1;
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700595
596 for (i = 0; i < wpa_s->scan_id_count; i++) {
597 unsigned int j;
598
599 ssid = wpa_config_get_network(wpa_s->conf, wpa_s->scan_id[i]);
600 if (!ssid || !ssid->scan_ssid)
601 continue;
602
603 for (j = 0; j < params->num_ssids; j++) {
604 if (params->ssids[j].ssid_len == ssid->ssid_len &&
605 params->ssids[j].ssid &&
606 os_memcmp(params->ssids[j].ssid, ssid->ssid,
607 ssid->ssid_len) == 0)
608 break;
609 }
610 if (j < params->num_ssids)
611 continue; /* already in the list */
612
613 if (params->num_ssids + 1 > max_ssids) {
614 wpa_printf(MSG_DEBUG,
615 "Over max scan SSIDs for manual request");
616 break;
617 }
618
619 wpa_printf(MSG_DEBUG, "Scan SSID (manual request): %s",
620 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
621 params->ssids[params->num_ssids].ssid = ssid->ssid;
622 params->ssids[params->num_ssids].ssid_len = ssid->ssid_len;
623 params->num_ssids++;
624 }
625
626 wpa_s->scan_id_count = 0;
627}
628
629
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700630static int wpa_set_ssids_from_scan_req(struct wpa_supplicant *wpa_s,
631 struct wpa_driver_scan_params *params,
632 size_t max_ssids)
633{
634 unsigned int i;
635
636 if (wpa_s->ssids_from_scan_req == NULL ||
637 wpa_s->num_ssids_from_scan_req == 0)
638 return 0;
639
640 if (wpa_s->num_ssids_from_scan_req > max_ssids) {
641 wpa_s->num_ssids_from_scan_req = max_ssids;
642 wpa_printf(MSG_DEBUG, "Over max scan SSIDs from scan req: %u",
643 (unsigned int) max_ssids);
644 }
645
646 for (i = 0; i < wpa_s->num_ssids_from_scan_req; i++) {
647 params->ssids[i].ssid = wpa_s->ssids_from_scan_req[i].ssid;
648 params->ssids[i].ssid_len =
649 wpa_s->ssids_from_scan_req[i].ssid_len;
650 wpa_hexdump_ascii(MSG_DEBUG, "specific SSID",
651 params->ssids[i].ssid,
652 params->ssids[i].ssid_len);
653 }
654
655 params->num_ssids = wpa_s->num_ssids_from_scan_req;
656 wpa_s->num_ssids_from_scan_req = 0;
657 return 1;
658}
659
660
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700661static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
662{
663 struct wpa_supplicant *wpa_s = eloop_ctx;
664 struct wpa_ssid *ssid;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800665 int ret, p2p_in_prog;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700666 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700667 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700668 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700669 size_t max_ssids;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800670 int connect_without_scan = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700671
Dmitry Shmidt18463232014-01-24 12:29:41 -0800672 if (wpa_s->pno || wpa_s->pno_sched_pending) {
673 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - PNO is in progress");
674 return;
675 }
676
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700677 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
678 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
679 return;
680 }
681
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800682 if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700683 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700684 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
685 return;
686 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800687
Dmitry Shmidt5887a9d2012-09-14 10:47:43 -0700688 if (wpa_s->scanning) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800689 /*
690 * If we are already in scanning state, we shall reschedule the
691 * the incoming scan request.
692 */
693 wpa_dbg(wpa_s, MSG_DEBUG, "Already scanning - Reschedule the incoming scan req");
694 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidt5887a9d2012-09-14 10:47:43 -0700695 return;
696 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800697
Dmitry Shmidt04949592012-07-19 12:16:46 -0700698 if (!wpa_supplicant_enabled_networks(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800699 wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700700 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
701 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
702 return;
703 }
704
705 if (wpa_s->conf->ap_scan != 0 &&
706 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
707 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
708 "overriding ap_scan configuration");
709 wpa_s->conf->ap_scan = 0;
710 wpas_notify_ap_scan_changed(wpa_s);
711 }
712
713 if (wpa_s->conf->ap_scan == 0) {
714 wpa_supplicant_gen_assoc_event(wpa_s);
715 return;
716 }
717
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800718 ssid = NULL;
719 if (wpa_s->scan_req != MANUAL_SCAN_REQ &&
720 wpa_s->connect_without_scan) {
721 connect_without_scan = 1;
722 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
723 if (ssid == wpa_s->connect_without_scan)
724 break;
725 }
726 }
727
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800728 p2p_in_prog = wpas_p2p_in_progress(wpa_s);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800729 if (p2p_in_prog && p2p_in_prog != 2 &&
730 (!ssid ||
731 (ssid->mode != WPAS_MODE_AP && ssid->mode != WPAS_MODE_P2P_GO))) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800732 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan while P2P operation is in progress");
733 wpa_supplicant_req_scan(wpa_s, 5, 0);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700734 return;
735 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700736
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800737 if (wpa_s->conf->ap_scan == 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700738 max_ssids = 1;
739 else {
740 max_ssids = wpa_s->max_scan_ssids;
741 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
742 max_ssids = WPAS_MAX_SCAN_SSIDS;
743 }
744
Dmitry Shmidt01904cf2013-12-05 11:08:35 -0800745 wpa_s->last_scan_req = wpa_s->scan_req;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800746 wpa_s->scan_req = NORMAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700747
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800748 if (connect_without_scan) {
749 wpa_s->connect_without_scan = NULL;
750 if (ssid) {
751 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
752 "without scan step");
753 wpa_supplicant_associate(wpa_s, NULL, ssid);
754 return;
755 }
756 }
757
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700758 os_memset(&params, 0, sizeof(params));
759
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800760 wpa_s->scan_prev_wpa_state = wpa_s->wpa_state;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700761 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
762 wpa_s->wpa_state == WPA_INACTIVE)
763 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
764
Dmitry Shmidt04949592012-07-19 12:16:46 -0700765 /*
766 * If autoscan has set its own scanning parameters
767 */
768 if (wpa_s->autoscan_params != NULL) {
769 scan_params = wpa_s->autoscan_params;
770 goto scan;
771 }
772
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700773 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
774 wpa_set_ssids_from_scan_req(wpa_s, &params, max_ssids)) {
775 wpa_printf(MSG_DEBUG, "Use specific SSIDs from SCAN command");
776 goto ssid_list_set;
777 }
778
Dmitry Shmidt04949592012-07-19 12:16:46 -0700779#ifdef CONFIG_P2P
780 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800781 wpa_s->go_params && !wpa_s->conf->passive_scan) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800782 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
783 wpa_s->p2p_in_provisioning,
784 wpa_s->show_group_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700785 params.ssids[0].ssid = wpa_s->go_params->ssid;
786 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
787 params.num_ssids = 1;
788 goto ssid_list_set;
789 }
Dmitry Shmidt15907092014-03-25 10:42:57 -0700790
791 if (wpa_s->p2p_in_invitation) {
792 if (wpa_s->current_ssid) {
793 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during invitation");
794 params.ssids[0].ssid = wpa_s->current_ssid->ssid;
795 params.ssids[0].ssid_len =
796 wpa_s->current_ssid->ssid_len;
797 params.num_ssids = 1;
798 } else {
799 wpa_printf(MSG_DEBUG, "P2P: No specific SSID known for scan during invitation");
800 }
801 goto ssid_list_set;
802 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700803#endif /* CONFIG_P2P */
804
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700805 /* Find the starting point from which to continue scanning */
806 ssid = wpa_s->conf->ssid;
807 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
808 while (ssid) {
809 if (ssid == wpa_s->prev_scan_ssid) {
810 ssid = ssid->next;
811 break;
812 }
813 ssid = ssid->next;
814 }
815 }
816
Dmitry Shmidt01904cf2013-12-05 11:08:35 -0800817 if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800818#ifdef CONFIG_AP
819 !wpa_s->ap_iface &&
820#endif /* CONFIG_AP */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -0800821 wpa_s->conf->ap_scan == 2) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700822 wpa_s->connect_without_scan = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800823 wpa_s->prev_scan_wildcard = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700824 wpa_supplicant_assoc_try(wpa_s, ssid);
825 return;
826 } else if (wpa_s->conf->ap_scan == 2) {
827 /*
828 * User-initiated scan request in ap_scan == 2; scan with
829 * wildcard SSID.
830 */
831 ssid = NULL;
Dmitry Shmidt98660862014-03-11 17:26:21 -0700832 } else if (wpa_s->reattach && wpa_s->current_ssid != NULL) {
833 /*
834 * Perform single-channel single-SSID scan for
835 * reassociate-to-same-BSS operation.
836 */
837 /* Setup SSID */
838 ssid = wpa_s->current_ssid;
839 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
840 ssid->ssid, ssid->ssid_len);
841 params.ssids[0].ssid = ssid->ssid;
842 params.ssids[0].ssid_len = ssid->ssid_len;
843 params.num_ssids = 1;
844
845 /*
846 * Allocate memory for frequency array, allocate one extra
847 * slot for the zero-terminator.
848 */
849 params.freqs = os_malloc(sizeof(int) * 2);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700850 if (params.freqs) {
851 params.freqs[0] = wpa_s->assoc_freq;
852 params.freqs[1] = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -0700853 }
Dmitry Shmidt98660862014-03-11 17:26:21 -0700854
855 /*
856 * Reset the reattach flag so that we fall back to full scan if
857 * this scan fails.
858 */
859 wpa_s->reattach = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700860 } else {
861 struct wpa_ssid *start = ssid, *tssid;
862 int freqs_set = 0;
863 if (ssid == NULL && max_ssids > 1)
864 ssid = wpa_s->conf->ssid;
865 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700866 if (!wpas_network_disabled(wpa_s, ssid) &&
867 ssid->scan_ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700868 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
869 ssid->ssid, ssid->ssid_len);
870 params.ssids[params.num_ssids].ssid =
871 ssid->ssid;
872 params.ssids[params.num_ssids].ssid_len =
873 ssid->ssid_len;
874 params.num_ssids++;
875 if (params.num_ssids + 1 >= max_ssids)
876 break;
877 }
878 ssid = ssid->next;
879 if (ssid == start)
880 break;
881 if (ssid == NULL && max_ssids > 1 &&
882 start != wpa_s->conf->ssid)
883 ssid = wpa_s->conf->ssid;
884 }
885
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700886 if (wpa_s->scan_id_count &&
887 wpa_s->last_scan_req == MANUAL_SCAN_REQ)
888 wpa_set_scan_ssids(wpa_s, &params, max_ssids);
889
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800890 for (tssid = wpa_s->conf->ssid;
891 wpa_s->last_scan_req != MANUAL_SCAN_REQ && tssid;
892 tssid = tssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700893 if (wpas_network_disabled(wpa_s, tssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700894 continue;
895 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
896 int_array_concat(&params.freqs,
897 tssid->scan_freq);
898 } else {
899 os_free(params.freqs);
900 params.freqs = NULL;
901 }
902 freqs_set = 1;
903 }
904 int_array_sort_unique(params.freqs);
905 }
906
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800907 if (ssid && max_ssids == 1) {
908 /*
909 * If the driver is limited to 1 SSID at a time interleave
910 * wildcard SSID scans with specific SSID scans to avoid
911 * waiting a long time for a wildcard scan.
912 */
913 if (!wpa_s->prev_scan_wildcard) {
914 params.ssids[0].ssid = NULL;
915 params.ssids[0].ssid_len = 0;
916 wpa_s->prev_scan_wildcard = 1;
917 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
918 "wildcard SSID (Interleave with specific)");
919 } else {
920 wpa_s->prev_scan_ssid = ssid;
921 wpa_s->prev_scan_wildcard = 0;
922 wpa_dbg(wpa_s, MSG_DEBUG,
923 "Starting AP scan for specific SSID: %s",
924 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700925 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800926 } else if (ssid) {
927 /* max_ssids > 1 */
928
929 wpa_s->prev_scan_ssid = ssid;
930 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
931 "the scan request");
932 params.num_ssids++;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800933 } else if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
934 wpa_s->manual_scan_passive && params.num_ssids == 0) {
935 wpa_dbg(wpa_s, MSG_DEBUG, "Use passive scan based on manual request");
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800936 } else if (wpa_s->conf->passive_scan) {
937 wpa_dbg(wpa_s, MSG_DEBUG,
938 "Use passive scan based on configuration");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700939 } else {
940 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
941 params.num_ssids++;
942 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
943 "SSID");
944 }
945
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700946ssid_list_set:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800947 wpa_supplicant_optimize_freqs(wpa_s, &params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700948 extra_ie = wpa_supplicant_extra_ies(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700949
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800950 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800951 wpa_s->manual_scan_only_new) {
952 wpa_printf(MSG_DEBUG,
953 "Request driver to clear scan cache due to manual only_new=1 scan");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800954 params.only_new_results = 1;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800955 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800956
957 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs == NULL &&
958 wpa_s->manual_scan_freqs) {
959 wpa_dbg(wpa_s, MSG_DEBUG, "Limit manual scan to specified channels");
960 params.freqs = wpa_s->manual_scan_freqs;
961 wpa_s->manual_scan_freqs = NULL;
962 }
963
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700964 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
965 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
966 "generated frequency list");
967 params.freqs = wpa_s->next_scan_freqs;
968 } else
969 os_free(wpa_s->next_scan_freqs);
970 wpa_s->next_scan_freqs = NULL;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700971 wpa_setband_scan_freqs(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700972
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700973 /* See if user specified frequencies. If so, scan only those. */
974 if (wpa_s->conf->freq_list && !params.freqs) {
975 wpa_dbg(wpa_s, MSG_DEBUG,
976 "Optimize scan based on conf->freq_list");
977 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
978 }
979
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700980 /* Use current associated channel? */
981 if (wpa_s->conf->scan_cur_freq && !params.freqs) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700982 unsigned int num = wpa_s->num_multichan_concurrent;
983
984 params.freqs = os_calloc(num + 1, sizeof(int));
985 if (params.freqs) {
986 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
987 if (num > 0) {
988 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
989 "current operating channels since "
990 "scan_cur_freq is enabled");
991 } else {
992 os_free(params.freqs);
993 params.freqs = NULL;
994 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700995 }
996 }
997
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700998 params.filter_ssids = wpa_supplicant_build_filter_ssids(
999 wpa_s->conf, &params.num_filter_ssids);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001000 if (extra_ie) {
1001 params.extra_ies = wpabuf_head(extra_ie);
1002 params.extra_ies_len = wpabuf_len(extra_ie);
1003 }
1004
1005#ifdef CONFIG_P2P
Dmitry Shmidt413dde72014-04-11 10:23:22 -07001006 if (wpa_s->p2p_in_provisioning || wpa_s->p2p_in_invitation ||
Dmitry Shmidt04949592012-07-19 12:16:46 -07001007 (wpa_s->show_group_started && wpa_s->go_params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001008 /*
1009 * The interface may not yet be in P2P mode, so we have to
1010 * explicitly request P2P probe to disable CCK rates.
1011 */
1012 params.p2p_probe = 1;
1013 }
1014#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001015
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001016 if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) {
1017 params.mac_addr_rand = 1;
1018 if (wpa_s->mac_addr_scan) {
1019 params.mac_addr = wpa_s->mac_addr_scan;
1020 params.mac_addr_mask = wpa_s->mac_addr_scan + ETH_ALEN;
1021 }
1022 }
1023
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001024 if (!is_zero_ether_addr(wpa_s->next_scan_bssid)) {
1025 struct wpa_bss *bss;
1026
1027 params.bssid = wpa_s->next_scan_bssid;
1028 bss = wpa_bss_get_bssid_latest(wpa_s, params.bssid);
1029 if (bss && bss->ssid_len && params.num_ssids == 1 &&
1030 params.ssids[0].ssid_len == 0) {
1031 params.ssids[0].ssid = bss->ssid;
1032 params.ssids[0].ssid_len = bss->ssid_len;
1033 wpa_dbg(wpa_s, MSG_DEBUG,
1034 "Scan a previously specified BSSID " MACSTR
1035 " and SSID %s",
1036 MAC2STR(params.bssid),
1037 wpa_ssid_txt(bss->ssid, bss->ssid_len));
1038 } else {
1039 wpa_dbg(wpa_s, MSG_DEBUG,
1040 "Scan a previously specified BSSID " MACSTR,
1041 MAC2STR(params.bssid));
1042 }
1043 }
1044
Dmitry Shmidt04949592012-07-19 12:16:46 -07001045 scan_params = &params;
1046
1047scan:
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001048#ifdef CONFIG_P2P
1049 /*
1050 * If the driver does not support multi-channel concurrency and a
1051 * virtual interface that shares the same radio with the wpa_s interface
1052 * is operating there may not be need to scan other channels apart from
1053 * the current operating channel on the other virtual interface. Filter
1054 * out other channels in case we are trying to find a connection for a
1055 * station interface when we are not configured to prefer station
1056 * connection and a concurrent operation is already in process.
1057 */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001058 if (wpa_s->scan_for_connection &&
1059 wpa_s->last_scan_req == NORMAL_SCAN_REQ &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001060 !scan_params->freqs && !params.freqs &&
1061 wpas_is_p2p_prioritized(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001062 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
1063 non_p2p_network_enabled(wpa_s)) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001064 unsigned int num = wpa_s->num_multichan_concurrent;
1065
1066 params.freqs = os_calloc(num + 1, sizeof(int));
1067 if (params.freqs) {
1068 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
1069 if (num > 0 && num == wpa_s->num_multichan_concurrent) {
1070 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
1071 } else {
1072 os_free(params.freqs);
1073 params.freqs = NULL;
1074 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001075 }
1076 }
1077#endif /* CONFIG_P2P */
1078
Dmitry Shmidt04949592012-07-19 12:16:46 -07001079 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001080
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001081 if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs &&
1082 !wpa_s->manual_scan_freqs) {
1083 /* Restore manual_scan_freqs for the next attempt */
1084 wpa_s->manual_scan_freqs = params.freqs;
1085 params.freqs = NULL;
1086 }
1087
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001088 wpabuf_free(extra_ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001089 os_free(params.freqs);
1090 os_free(params.filter_ssids);
1091
1092 if (ret) {
1093 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001094 if (wpa_s->scan_prev_wpa_state != wpa_s->wpa_state)
1095 wpa_supplicant_set_state(wpa_s,
1096 wpa_s->scan_prev_wpa_state);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001097 /* Restore scan_req since we will try to scan again */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001098 wpa_s->scan_req = wpa_s->last_scan_req;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001099 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001100 } else {
1101 wpa_s->scan_for_connection = 0;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001102#ifdef CONFIG_INTERWORKING
1103 wpa_s->interworking_fast_assoc_tried = 0;
1104#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001105 if (params.bssid)
1106 os_memset(wpa_s->next_scan_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001107 }
1108}
1109
1110
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001111void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
1112{
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001113 struct os_reltime remaining, new_int;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001114 int cancelled;
1115
1116 cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
1117 &remaining);
1118
1119 new_int.sec = sec;
1120 new_int.usec = 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001121 if (cancelled && os_reltime_before(&remaining, &new_int)) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001122 new_int.sec = remaining.sec;
1123 new_int.usec = remaining.usec;
1124 }
1125
Dmitry Shmidt051af732013-10-22 13:52:46 -07001126 if (cancelled) {
1127 eloop_register_timeout(new_int.sec, new_int.usec,
1128 wpa_supplicant_scan, wpa_s, NULL);
1129 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001130 wpa_s->scan_interval = sec;
1131}
1132
1133
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001134/**
1135 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
1136 * @wpa_s: Pointer to wpa_supplicant data
1137 * @sec: Number of seconds after which to scan
1138 * @usec: Number of microseconds after which to scan
1139 *
1140 * This function is used to schedule a scan for neighboring access points after
1141 * the specified time.
1142 */
1143void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
1144{
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001145 int res;
1146
1147 if (wpa_s->p2p_mgmt) {
1148 wpa_dbg(wpa_s, MSG_DEBUG,
1149 "Ignore scan request (%d.%06d sec) on p2p_mgmt interface",
1150 sec, usec);
1151 return;
1152 }
1153
1154 res = eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s,
1155 NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001156 if (res == 1) {
1157 wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d.%06d sec",
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001158 sec, usec);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001159 } else if (res == 0) {
1160 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore new scan request for %d.%06d sec since an earlier request is scheduled to trigger sooner",
1161 sec, usec);
1162 } else {
1163 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d.%06d sec",
1164 sec, usec);
1165 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001166 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001167}
1168
1169
1170/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001171 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
1172 * @wpa_s: Pointer to wpa_supplicant data
1173 * @sec: Number of seconds after which to scan
1174 * @usec: Number of microseconds after which to scan
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001175 * Returns: 0 on success or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001176 *
1177 * This function is used to schedule periodic scans for neighboring
1178 * access points after the specified time.
1179 */
1180int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
1181 int sec, int usec)
1182{
1183 if (!wpa_s->sched_scan_supported)
1184 return -1;
1185
1186 eloop_register_timeout(sec, usec,
1187 wpa_supplicant_delayed_sched_scan_timeout,
1188 wpa_s, NULL);
1189
1190 return 0;
1191}
1192
1193
1194/**
1195 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
1196 * @wpa_s: Pointer to wpa_supplicant data
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001197 * Returns: 0 is sched_scan was started or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001198 *
1199 * This function is used to schedule periodic scans for neighboring
1200 * access points repeating the scan continuously.
1201 */
1202int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
1203{
1204 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001205 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001206 enum wpa_states prev_state;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001207 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001208 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001209 int ret;
1210 unsigned int max_sched_scan_ssids;
1211 int wildcard = 0;
1212 int need_ssids;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001213 struct sched_scan_plan scan_plan;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001214
1215 if (!wpa_s->sched_scan_supported)
1216 return -1;
1217
1218 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
1219 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
1220 else
1221 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001222 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001223 return -1;
1224
1225 if (wpa_s->sched_scanning) {
1226 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
1227 return 0;
1228 }
1229
1230 need_ssids = 0;
1231 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001232 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001233 /* Use wildcard SSID to find this network */
1234 wildcard = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001235 } else if (!wpas_network_disabled(wpa_s, ssid) &&
1236 ssid->ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001237 need_ssids++;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001238
1239#ifdef CONFIG_WPS
1240 if (!wpas_network_disabled(wpa_s, ssid) &&
1241 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1242 /*
1243 * Normal scan is more reliable and faster for WPS
1244 * operations and since these are for short periods of
1245 * time, the benefit of trying to use sched_scan would
1246 * be limited.
1247 */
1248 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1249 "sched_scan for WPS");
1250 return -1;
1251 }
1252#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001253 }
1254 if (wildcard)
1255 need_ssids++;
1256
1257 if (wpa_s->normal_scans < 3 &&
1258 (need_ssids <= wpa_s->max_scan_ssids ||
1259 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
1260 /*
1261 * When normal scan can speed up operations, use that for the
1262 * first operations before starting the sched_scan to allow
1263 * user space sleep more. We do this only if the normal scan
1264 * has functionality that is suitable for this or if the
1265 * sched_scan does not have better support for multiple SSIDs.
1266 */
1267 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1268 "sched_scan for initial scans (normal_scans=%d)",
1269 wpa_s->normal_scans);
1270 return -1;
1271 }
1272
1273 os_memset(&params, 0, sizeof(params));
1274
1275 /* If we can't allocate space for the filters, we just don't filter */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001276 params.filter_ssids = os_calloc(wpa_s->max_match_sets,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001277 sizeof(struct wpa_driver_scan_filter));
1278
1279 prev_state = wpa_s->wpa_state;
1280 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1281 wpa_s->wpa_state == WPA_INACTIVE)
1282 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1283
Dmitry Shmidt04949592012-07-19 12:16:46 -07001284 if (wpa_s->autoscan_params != NULL) {
1285 scan_params = wpa_s->autoscan_params;
1286 goto scan;
1287 }
1288
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001289 /* Find the starting point from which to continue scanning */
1290 ssid = wpa_s->conf->ssid;
1291 if (wpa_s->prev_sched_ssid) {
1292 while (ssid) {
1293 if (ssid == wpa_s->prev_sched_ssid) {
1294 ssid = ssid->next;
1295 break;
1296 }
1297 ssid = ssid->next;
1298 }
1299 }
1300
1301 if (!ssid || !wpa_s->prev_sched_ssid) {
1302 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001303 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1304 wpa_s->first_sched_scan = 1;
1305 ssid = wpa_s->conf->ssid;
1306 wpa_s->prev_sched_ssid = ssid;
1307 }
1308
1309 if (wildcard) {
1310 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1311 params.num_ssids++;
1312 }
1313
1314 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001315 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001316 goto next;
1317
1318 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1319 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1320 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1321 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1322 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1323 ssid->ssid, ssid->ssid_len);
1324 params.filter_ssids[params.num_filter_ssids].ssid_len =
1325 ssid->ssid_len;
1326 params.num_filter_ssids++;
1327 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1328 {
1329 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1330 "filter for sched_scan - drop filter");
1331 os_free(params.filter_ssids);
1332 params.filter_ssids = NULL;
1333 params.num_filter_ssids = 0;
1334 }
1335
1336 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1337 if (params.num_ssids == max_sched_scan_ssids)
1338 break; /* only room for broadcast SSID */
1339 wpa_dbg(wpa_s, MSG_DEBUG,
1340 "add to active scan ssid: %s",
1341 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1342 params.ssids[params.num_ssids].ssid =
1343 ssid->ssid;
1344 params.ssids[params.num_ssids].ssid_len =
1345 ssid->ssid_len;
1346 params.num_ssids++;
1347 if (params.num_ssids >= max_sched_scan_ssids) {
1348 wpa_s->prev_sched_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001349 do {
1350 ssid = ssid->next;
1351 } while (ssid &&
1352 (wpas_network_disabled(wpa_s, ssid) ||
1353 !ssid->scan_ssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001354 break;
1355 }
1356 }
1357
1358 next:
1359 wpa_s->prev_sched_ssid = ssid;
1360 ssid = ssid->next;
1361 }
1362
1363 if (params.num_filter_ssids == 0) {
1364 os_free(params.filter_ssids);
1365 params.filter_ssids = NULL;
1366 }
1367
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001368 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1369 if (extra_ie) {
1370 params.extra_ies = wpabuf_head(extra_ie);
1371 params.extra_ies_len = wpabuf_len(extra_ie);
1372 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001373
Dmitry Shmidt18463232014-01-24 12:29:41 -08001374 if (wpa_s->conf->filter_rssi)
1375 params.filter_rssi = wpa_s->conf->filter_rssi;
1376
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001377 /* See if user specified frequencies. If so, scan only those. */
1378 if (wpa_s->conf->freq_list && !params.freqs) {
1379 wpa_dbg(wpa_s, MSG_DEBUG,
1380 "Optimize scan based on conf->freq_list");
1381 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1382 }
1383
Dmitry Shmidt04949592012-07-19 12:16:46 -07001384 scan_params = &params;
1385
1386scan:
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001387 wpa_s->sched_scan_timed_out = 0;
1388
1389 /*
1390 * We cannot support multiple scan plans if the scan request includes
1391 * too many SSID's, so in this case use only the last scan plan and make
1392 * it run infinitely. It will be stopped by the timeout.
1393 */
1394 if (wpa_s->sched_scan_plans_num == 1 ||
1395 (wpa_s->sched_scan_plans_num && !ssid && wpa_s->first_sched_scan)) {
1396 params.sched_scan_plans = wpa_s->sched_scan_plans;
1397 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
1398 } else if (wpa_s->sched_scan_plans_num > 1) {
1399 wpa_dbg(wpa_s, MSG_DEBUG,
1400 "Too many SSIDs. Default to using single scheduled_scan plan");
1401 params.sched_scan_plans =
1402 &wpa_s->sched_scan_plans[wpa_s->sched_scan_plans_num -
1403 1];
1404 params.sched_scan_plans_num = 1;
1405 } else {
1406 if (wpa_s->conf->sched_scan_interval)
1407 scan_plan.interval = wpa_s->conf->sched_scan_interval;
1408 else
1409 scan_plan.interval = 10;
1410
1411 if (scan_plan.interval > wpa_s->max_sched_scan_plan_interval) {
1412 wpa_printf(MSG_WARNING,
1413 "Scan interval too long(%u), use the maximum allowed(%u)",
1414 scan_plan.interval,
1415 wpa_s->max_sched_scan_plan_interval);
1416 scan_plan.interval =
1417 wpa_s->max_sched_scan_plan_interval;
1418 }
1419
1420 scan_plan.iterations = 0;
1421 params.sched_scan_plans = &scan_plan;
1422 params.sched_scan_plans_num = 1;
1423 }
1424
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001425 if (ssid || !wpa_s->first_sched_scan) {
1426 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001427 "Starting sched scan: interval %u timeout %d",
1428 params.sched_scan_plans[0].interval,
1429 wpa_s->sched_scan_timeout);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001430 } else {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001431 wpa_dbg(wpa_s, MSG_DEBUG, "Starting sched scan (no timeout)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001432 }
1433
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001434 wpa_setband_scan_freqs(wpa_s, scan_params);
1435
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001436 if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCHED_SCAN) {
1437 params.mac_addr_rand = 1;
1438 if (wpa_s->mac_addr_sched_scan) {
1439 params.mac_addr = wpa_s->mac_addr_sched_scan;
1440 params.mac_addr_mask = wpa_s->mac_addr_sched_scan +
1441 ETH_ALEN;
1442 }
1443 }
1444
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001445 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001446 wpabuf_free(extra_ie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001447 os_free(params.filter_ssids);
1448 if (ret) {
1449 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1450 if (prev_state != wpa_s->wpa_state)
1451 wpa_supplicant_set_state(wpa_s, prev_state);
1452 return ret;
1453 }
1454
1455 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1456 if (ssid || !wpa_s->first_sched_scan) {
1457 wpa_s->sched_scan_timed_out = 0;
1458 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1459 wpa_supplicant_sched_scan_timeout,
1460 wpa_s, NULL);
1461 wpa_s->first_sched_scan = 0;
1462 wpa_s->sched_scan_timeout /= 2;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001463 params.sched_scan_plans[0].interval *= 2;
1464 if ((unsigned int) wpa_s->sched_scan_timeout <
1465 params.sched_scan_plans[0].interval ||
1466 params.sched_scan_plans[0].interval >
1467 wpa_s->max_sched_scan_plan_interval) {
1468 params.sched_scan_plans[0].interval = 10;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001469 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1470 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001471 }
1472
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001473 /* If there is no more ssids, start next time from the beginning */
1474 if (!ssid)
1475 wpa_s->prev_sched_ssid = NULL;
1476
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001477 return 0;
1478}
1479
1480
1481/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001482 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1483 * @wpa_s: Pointer to wpa_supplicant data
1484 *
1485 * This function is used to cancel a scan request scheduled with
1486 * wpa_supplicant_req_scan().
1487 */
1488void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1489{
1490 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1491 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001492}
1493
1494
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001495/**
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001496 * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
1497 * @wpa_s: Pointer to wpa_supplicant data
1498 *
1499 * This function is used to stop a delayed scheduled scan.
1500 */
1501void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
1502{
1503 if (!wpa_s->sched_scan_supported)
1504 return;
1505
1506 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
1507 eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
1508 wpa_s, NULL);
1509}
1510
1511
1512/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001513 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1514 * @wpa_s: Pointer to wpa_supplicant data
1515 *
1516 * This function is used to stop a periodic scheduled scan.
1517 */
1518void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1519{
1520 if (!wpa_s->sched_scanning)
1521 return;
1522
1523 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1524 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1525 wpa_supplicant_stop_sched_scan(wpa_s);
1526}
1527
1528
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001529/**
1530 * wpa_supplicant_notify_scanning - Indicate possible scan state change
1531 * @wpa_s: Pointer to wpa_supplicant data
1532 * @scanning: Whether scanning is currently in progress
1533 *
1534 * This function is to generate scanning notifycations. It is called whenever
1535 * there may have been a change in scanning (scan started, completed, stopped).
1536 * wpas_notify_scanning() is called whenever the scanning state changed from the
1537 * previously notified state.
1538 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001539void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1540 int scanning)
1541{
1542 if (wpa_s->scanning != scanning) {
1543 wpa_s->scanning = scanning;
1544 wpas_notify_scanning(wpa_s);
1545 }
1546}
1547
1548
1549static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1550{
1551 int rate = 0;
1552 const u8 *ie;
1553 int i;
1554
1555 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1556 for (i = 0; ie && i < ie[1]; i++) {
1557 if ((ie[i + 2] & 0x7f) > rate)
1558 rate = ie[i + 2] & 0x7f;
1559 }
1560
1561 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1562 for (i = 0; ie && i < ie[1]; i++) {
1563 if ((ie[i + 2] & 0x7f) > rate)
1564 rate = ie[i + 2] & 0x7f;
1565 }
1566
1567 return rate;
1568}
1569
1570
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001571/**
1572 * wpa_scan_get_ie - Fetch a specified information element from a scan result
1573 * @res: Scan result entry
1574 * @ie: Information element identitifier (WLAN_EID_*)
1575 * Returns: Pointer to the information element (id field) or %NULL if not found
1576 *
1577 * This function returns the first matching information element in the scan
1578 * result.
1579 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001580const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1581{
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001582 return get_ie((const u8 *) (res + 1), res->ie_len, ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001583}
1584
1585
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001586/**
1587 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1588 * @res: Scan result entry
1589 * @vendor_type: Vendor type (four octets starting the IE payload)
1590 * Returns: Pointer to the information element (id field) or %NULL if not found
1591 *
1592 * This function returns the first matching information element in the scan
1593 * result.
1594 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001595const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1596 u32 vendor_type)
1597{
1598 const u8 *end, *pos;
1599
1600 pos = (const u8 *) (res + 1);
1601 end = pos + res->ie_len;
1602
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001603 while (end - pos > 1) {
1604 if (2 + pos[1] > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001605 break;
1606 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1607 vendor_type == WPA_GET_BE32(&pos[2]))
1608 return pos;
1609 pos += 2 + pos[1];
1610 }
1611
1612 return NULL;
1613}
1614
1615
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001616/**
Dmitry Shmidt96571392013-10-14 12:54:46 -07001617 * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
1618 * @res: Scan result entry
1619 * @vendor_type: Vendor type (four octets starting the IE payload)
1620 * Returns: Pointer to the information element (id field) or %NULL if not found
1621 *
1622 * This function returns the first matching information element in the scan
1623 * result.
1624 *
1625 * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
1626 * from Beacon frames instead of either Beacon or Probe Response frames.
1627 */
1628const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
1629 u32 vendor_type)
1630{
1631 const u8 *end, *pos;
1632
1633 if (res->beacon_ie_len == 0)
1634 return NULL;
1635
1636 pos = (const u8 *) (res + 1);
1637 pos += res->ie_len;
1638 end = pos + res->beacon_ie_len;
1639
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001640 while (end - pos > 1) {
1641 if (2 + pos[1] > end - pos)
Dmitry Shmidt96571392013-10-14 12:54:46 -07001642 break;
1643 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1644 vendor_type == WPA_GET_BE32(&pos[2]))
1645 return pos;
1646 pos += 2 + pos[1];
1647 }
1648
1649 return NULL;
1650}
1651
1652
1653/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001654 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
1655 * @res: Scan result entry
1656 * @vendor_type: Vendor type (four octets starting the IE payload)
1657 * Returns: Pointer to the information element payload or %NULL if not found
1658 *
1659 * This function returns concatenated payload of possibly fragmented vendor
1660 * specific information elements in the scan result. The caller is responsible
1661 * for freeing the returned buffer.
1662 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001663struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1664 u32 vendor_type)
1665{
1666 struct wpabuf *buf;
1667 const u8 *end, *pos;
1668
1669 buf = wpabuf_alloc(res->ie_len);
1670 if (buf == NULL)
1671 return NULL;
1672
1673 pos = (const u8 *) (res + 1);
1674 end = pos + res->ie_len;
1675
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001676 while (end - pos > 1) {
1677 if (2 + pos[1] > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001678 break;
1679 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1680 vendor_type == WPA_GET_BE32(&pos[2]))
1681 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1682 pos += 2 + pos[1];
1683 }
1684
1685 if (wpabuf_len(buf) == 0) {
1686 wpabuf_free(buf);
1687 buf = NULL;
1688 }
1689
1690 return buf;
1691}
1692
1693
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001694/*
1695 * Channels with a great SNR can operate at full rate. What is a great SNR?
1696 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1697 * rule of thumb is that any SNR above 20 is good." This one
1698 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1699 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1700 * conservative value.
1701 */
1702#define GREAT_SNR 30
1703
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001704#define IS_5GHZ(n) (n > 4000)
1705
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001706/* Compare function for sorting scan results. Return >0 if @b is considered
1707 * better. */
1708static int wpa_scan_result_compar(const void *a, const void *b)
1709{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001710#define MIN(a,b) a < b ? a : b
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001711 struct wpa_scan_res **_wa = (void *) a;
1712 struct wpa_scan_res **_wb = (void *) b;
1713 struct wpa_scan_res *wa = *_wa;
1714 struct wpa_scan_res *wb = *_wb;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001715 int wpa_a, wpa_b;
1716 int snr_a, snr_b, snr_a_full, snr_b_full;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001717
1718 /* WPA/WPA2 support preferred */
1719 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1720 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1721 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1722 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1723
1724 if (wpa_b && !wpa_a)
1725 return 1;
1726 if (!wpa_b && wpa_a)
1727 return -1;
1728
1729 /* privacy support preferred */
1730 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1731 (wb->caps & IEEE80211_CAP_PRIVACY))
1732 return 1;
1733 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1734 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1735 return -1;
1736
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001737 if (wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001738 snr_a_full = wa->snr;
1739 snr_a = MIN(wa->snr, GREAT_SNR);
1740 snr_b_full = wb->snr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001741 snr_b = MIN(wb->snr, GREAT_SNR);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001742 } else {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001743 /* Level is not in dBm, so we can't calculate
1744 * SNR. Just use raw level (units unknown). */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001745 snr_a = snr_a_full = wa->level;
1746 snr_b = snr_b_full = wb->level;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001747 }
1748
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001749 /* if SNR is close, decide by max rate or frequency band */
1750 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001751 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001752 if (wa->est_throughput != wb->est_throughput)
1753 return wb->est_throughput - wa->est_throughput;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001754 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1755 return IS_5GHZ(wa->freq) ? -1 : 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001756 }
1757
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001758 /* all things being equal, use SNR; if SNRs are
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001759 * identical, use quality values since some drivers may only report
1760 * that value and leave the signal level zero */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001761 if (snr_b_full == snr_a_full)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001762 return wb->qual - wa->qual;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001763 return snr_b_full - snr_a_full;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001764#undef MIN
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001765}
1766
1767
1768#ifdef CONFIG_WPS
1769/* Compare function for sorting scan results when searching a WPS AP for
1770 * provisioning. Return >0 if @b is considered better. */
1771static int wpa_scan_result_wps_compar(const void *a, const void *b)
1772{
1773 struct wpa_scan_res **_wa = (void *) a;
1774 struct wpa_scan_res **_wb = (void *) b;
1775 struct wpa_scan_res *wa = *_wa;
1776 struct wpa_scan_res *wb = *_wb;
1777 int uses_wps_a, uses_wps_b;
1778 struct wpabuf *wps_a, *wps_b;
1779 int res;
1780
1781 /* Optimization - check WPS IE existence before allocated memory and
1782 * doing full reassembly. */
1783 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1784 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1785 if (uses_wps_a && !uses_wps_b)
1786 return -1;
1787 if (!uses_wps_a && uses_wps_b)
1788 return 1;
1789
1790 if (uses_wps_a && uses_wps_b) {
1791 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1792 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1793 res = wps_ap_priority_compar(wps_a, wps_b);
1794 wpabuf_free(wps_a);
1795 wpabuf_free(wps_b);
1796 if (res)
1797 return res;
1798 }
1799
1800 /*
1801 * Do not use current AP security policy as a sorting criteria during
1802 * WPS provisioning step since the AP may get reconfigured at the
1803 * completion of provisioning.
1804 */
1805
1806 /* all things being equal, use signal level; if signal levels are
1807 * identical, use quality values since some drivers may only report
1808 * that value and leave the signal level zero */
1809 if (wb->level == wa->level)
1810 return wb->qual - wa->qual;
1811 return wb->level - wa->level;
1812}
1813#endif /* CONFIG_WPS */
1814
1815
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001816static void dump_scan_res(struct wpa_scan_results *scan_res)
1817{
1818#ifndef CONFIG_NO_STDOUT_DEBUG
1819 size_t i;
1820
1821 if (scan_res->res == NULL || scan_res->num == 0)
1822 return;
1823
1824 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1825
1826 for (i = 0; i < scan_res->num; i++) {
1827 struct wpa_scan_res *r = scan_res->res[i];
Dmitry Shmidt04949592012-07-19 12:16:46 -07001828 u8 *pos;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001829 if (r->flags & WPA_SCAN_LEVEL_DBM) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001830 int noise_valid = !(r->flags & WPA_SCAN_NOISE_INVALID);
1831
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001832 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001833 "noise=%d%s level=%d snr=%d%s flags=0x%x age=%u est=%u",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001834 MAC2STR(r->bssid), r->freq, r->qual,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001835 r->noise, noise_valid ? "" : "~", r->level,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001836 r->snr, r->snr >= GREAT_SNR ? "*" : "",
1837 r->flags,
1838 r->age, r->est_throughput);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001839 } else {
1840 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001841 "noise=%d level=%d flags=0x%x age=%u est=%u",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001842 MAC2STR(r->bssid), r->freq, r->qual,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001843 r->noise, r->level, r->flags, r->age,
1844 r->est_throughput);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001845 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001846 pos = (u8 *) (r + 1);
1847 if (r->ie_len)
1848 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
1849 pos += r->ie_len;
1850 if (r->beacon_ie_len)
1851 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
1852 pos, r->beacon_ie_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001853 }
1854#endif /* CONFIG_NO_STDOUT_DEBUG */
1855}
1856
1857
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001858/**
1859 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
1860 * @wpa_s: Pointer to wpa_supplicant data
1861 * @bssid: BSSID to check
1862 * Returns: 0 if the BSSID is filtered or 1 if not
1863 *
1864 * This function is used to filter out specific BSSIDs from scan reslts mainly
1865 * for testing purposes (SET bssid_filter ctrl_iface command).
1866 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001867int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
1868 const u8 *bssid)
1869{
1870 size_t i;
1871
1872 if (wpa_s->bssid_filter == NULL)
1873 return 1;
1874
1875 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
1876 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
1877 ETH_ALEN) == 0)
1878 return 1;
1879 }
1880
1881 return 0;
1882}
1883
1884
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001885void filter_scan_res(struct wpa_supplicant *wpa_s,
1886 struct wpa_scan_results *res)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001887{
1888 size_t i, j;
1889
1890 if (wpa_s->bssid_filter == NULL)
1891 return;
1892
1893 for (i = 0, j = 0; i < res->num; i++) {
1894 if (wpa_supplicant_filter_bssid_match(wpa_s,
1895 res->res[i]->bssid)) {
1896 res->res[j++] = res->res[i];
1897 } else {
1898 os_free(res->res[i]);
1899 res->res[i] = NULL;
1900 }
1901 }
1902
1903 if (res->num != j) {
1904 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
1905 (int) (res->num - j));
1906 res->num = j;
1907 }
1908}
1909
1910
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001911/*
1912 * Noise floor values to use when we have signal strength
Dmitry Shmidte4663042016-04-04 10:07:49 -07001913 * measurements, but no noise floor measurements. These values were
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001914 * measured in an office environment with many APs.
1915 */
1916#define DEFAULT_NOISE_FLOOR_2GHZ (-89)
1917#define DEFAULT_NOISE_FLOOR_5GHZ (-92)
1918
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001919void scan_snr(struct wpa_scan_res *res)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001920{
1921 if (res->flags & WPA_SCAN_NOISE_INVALID) {
1922 res->noise = IS_5GHZ(res->freq) ?
1923 DEFAULT_NOISE_FLOOR_5GHZ :
1924 DEFAULT_NOISE_FLOOR_2GHZ;
1925 }
1926
1927 if (res->flags & WPA_SCAN_LEVEL_DBM) {
1928 res->snr = res->level - res->noise;
1929 } else {
1930 /* Level is not in dBm, so we can't calculate
1931 * SNR. Just use raw level (units unknown). */
1932 res->snr = res->level;
1933 }
1934}
1935
1936
1937static unsigned int max_ht20_rate(int snr)
1938{
1939 if (snr < 6)
1940 return 6500; /* HT20 MCS0 */
1941 if (snr < 8)
1942 return 13000; /* HT20 MCS1 */
1943 if (snr < 13)
1944 return 19500; /* HT20 MCS2 */
1945 if (snr < 17)
1946 return 26000; /* HT20 MCS3 */
1947 if (snr < 20)
1948 return 39000; /* HT20 MCS4 */
1949 if (snr < 23)
1950 return 52000; /* HT20 MCS5 */
1951 if (snr < 24)
1952 return 58500; /* HT20 MCS6 */
1953 return 65000; /* HT20 MCS7 */
1954}
1955
1956
1957static unsigned int max_ht40_rate(int snr)
1958{
1959 if (snr < 3)
1960 return 13500; /* HT40 MCS0 */
1961 if (snr < 6)
1962 return 27000; /* HT40 MCS1 */
1963 if (snr < 10)
1964 return 40500; /* HT40 MCS2 */
1965 if (snr < 15)
1966 return 54000; /* HT40 MCS3 */
1967 if (snr < 17)
1968 return 81000; /* HT40 MCS4 */
1969 if (snr < 22)
1970 return 108000; /* HT40 MCS5 */
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001971 if (snr < 24)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001972 return 121500; /* HT40 MCS6 */
1973 return 135000; /* HT40 MCS7 */
1974}
1975
1976
1977static unsigned int max_vht80_rate(int snr)
1978{
1979 if (snr < 1)
1980 return 0;
1981 if (snr < 2)
1982 return 29300; /* VHT80 MCS0 */
1983 if (snr < 5)
1984 return 58500; /* VHT80 MCS1 */
1985 if (snr < 9)
1986 return 87800; /* VHT80 MCS2 */
1987 if (snr < 11)
1988 return 117000; /* VHT80 MCS3 */
1989 if (snr < 15)
1990 return 175500; /* VHT80 MCS4 */
1991 if (snr < 16)
1992 return 234000; /* VHT80 MCS5 */
1993 if (snr < 18)
1994 return 263300; /* VHT80 MCS6 */
1995 if (snr < 20)
1996 return 292500; /* VHT80 MCS7 */
1997 if (snr < 22)
1998 return 351000; /* VHT80 MCS8 */
1999 return 390000; /* VHT80 MCS9 */
2000}
2001
2002
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002003void scan_est_throughput(struct wpa_supplicant *wpa_s,
2004 struct wpa_scan_res *res)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002005{
2006 enum local_hw_capab capab = wpa_s->hw_capab;
2007 int rate; /* max legacy rate in 500 kb/s units */
2008 const u8 *ie;
2009 unsigned int est, tmp;
2010 int snr = res->snr;
2011
2012 if (res->est_throughput)
2013 return;
2014
2015 /* Get maximum legacy rate */
2016 rate = wpa_scan_get_max_rate(res);
2017
2018 /* Limit based on estimated SNR */
2019 if (rate > 1 * 2 && snr < 1)
2020 rate = 1 * 2;
2021 else if (rate > 2 * 2 && snr < 4)
2022 rate = 2 * 2;
2023 else if (rate > 6 * 2 && snr < 5)
2024 rate = 6 * 2;
2025 else if (rate > 9 * 2 && snr < 6)
2026 rate = 9 * 2;
2027 else if (rate > 12 * 2 && snr < 7)
2028 rate = 12 * 2;
2029 else if (rate > 18 * 2 && snr < 10)
2030 rate = 18 * 2;
2031 else if (rate > 24 * 2 && snr < 11)
2032 rate = 24 * 2;
2033 else if (rate > 36 * 2 && snr < 15)
2034 rate = 36 * 2;
2035 else if (rate > 48 * 2 && snr < 19)
2036 rate = 48 * 2;
2037 else if (rate > 54 * 2 && snr < 21)
2038 rate = 54 * 2;
2039 est = rate * 500;
2040
2041 if (capab == CAPAB_HT || capab == CAPAB_HT40 || capab == CAPAB_VHT) {
2042 ie = wpa_scan_get_ie(res, WLAN_EID_HT_CAP);
2043 if (ie) {
2044 tmp = max_ht20_rate(snr);
2045 if (tmp > est)
2046 est = tmp;
2047 }
2048 }
2049
2050 if (capab == CAPAB_HT40 || capab == CAPAB_VHT) {
2051 ie = wpa_scan_get_ie(res, WLAN_EID_HT_OPERATION);
2052 if (ie && ie[1] >= 2 &&
2053 (ie[3] & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
2054 tmp = max_ht40_rate(snr);
2055 if (tmp > est)
2056 est = tmp;
2057 }
2058 }
2059
2060 if (capab == CAPAB_VHT) {
2061 /* Use +1 to assume VHT is always faster than HT */
2062 ie = wpa_scan_get_ie(res, WLAN_EID_VHT_CAP);
2063 if (ie) {
2064 tmp = max_ht20_rate(snr) + 1;
2065 if (tmp > est)
2066 est = tmp;
2067
2068 ie = wpa_scan_get_ie(res, WLAN_EID_HT_OPERATION);
2069 if (ie && ie[1] >= 2 &&
2070 (ie[3] &
2071 HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
2072 tmp = max_ht40_rate(snr) + 1;
2073 if (tmp > est)
2074 est = tmp;
2075 }
2076
2077 ie = wpa_scan_get_ie(res, WLAN_EID_VHT_OPERATION);
2078 if (ie && ie[1] >= 1 &&
2079 (ie[2] & VHT_OPMODE_CHANNEL_WIDTH_MASK)) {
2080 tmp = max_vht80_rate(snr) + 1;
2081 if (tmp > est)
2082 est = tmp;
2083 }
2084 }
2085 }
2086
2087 /* TODO: channel utilization and AP load (e.g., from AP Beacon) */
2088
2089 res->est_throughput = est;
2090}
2091
2092
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002093/**
2094 * wpa_supplicant_get_scan_results - Get scan results
2095 * @wpa_s: Pointer to wpa_supplicant data
2096 * @info: Information about what was scanned or %NULL if not available
2097 * @new_scan: Whether a new scan was performed
2098 * Returns: Scan results, %NULL on failure
2099 *
2100 * This function request the current scan results from the driver and updates
2101 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
2102 * results with wpa_scan_results_free().
2103 */
2104struct wpa_scan_results *
2105wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
2106 struct scan_info *info, int new_scan)
2107{
2108 struct wpa_scan_results *scan_res;
2109 size_t i;
2110 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
2111
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002112 scan_res = wpa_drv_get_scan_results2(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002113 if (scan_res == NULL) {
2114 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
2115 return NULL;
2116 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002117 if (scan_res->fetch_time.sec == 0) {
2118 /*
2119 * Make sure we have a valid timestamp if the driver wrapper
2120 * does not set this.
2121 */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002122 os_get_reltime(&scan_res->fetch_time);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002123 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002124 filter_scan_res(wpa_s, scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002125
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002126 for (i = 0; i < scan_res->num; i++) {
2127 struct wpa_scan_res *scan_res_item = scan_res->res[i];
2128
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002129 scan_snr(scan_res_item);
2130 scan_est_throughput(wpa_s, scan_res_item);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002131 }
2132
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002133#ifdef CONFIG_WPS
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002134 if (wpas_wps_searching(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002135 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
2136 "provisioning rules");
2137 compar = wpa_scan_result_wps_compar;
2138 }
2139#endif /* CONFIG_WPS */
2140
2141 qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
2142 compar);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002143 dump_scan_res(scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002144
2145 wpa_bss_update_start(wpa_s);
2146 for (i = 0; i < scan_res->num; i++)
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002147 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
2148 &scan_res->fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002149 wpa_bss_update_end(wpa_s, info, new_scan);
2150
2151 return scan_res;
2152}
2153
2154
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002155/**
2156 * wpa_supplicant_update_scan_results - Update scan results from the driver
2157 * @wpa_s: Pointer to wpa_supplicant data
2158 * Returns: 0 on success, -1 on failure
2159 *
2160 * This function updates the BSS table within wpa_supplicant based on the
2161 * currently available scan results from the driver without requesting a new
2162 * scan. This is used in cases where the driver indicates an association
2163 * (including roaming within ESS) and wpa_supplicant does not yet have the
2164 * needed information to complete the connection (e.g., to perform validation
2165 * steps in 4-way handshake).
2166 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002167int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
2168{
2169 struct wpa_scan_results *scan_res;
2170 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
2171 if (scan_res == NULL)
2172 return -1;
2173 wpa_scan_results_free(scan_res);
2174
2175 return 0;
2176}
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002177
2178
2179/**
2180 * scan_only_handler - Reports scan results
2181 */
2182void scan_only_handler(struct wpa_supplicant *wpa_s,
2183 struct wpa_scan_results *scan_res)
2184{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002185 wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002186 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
2187 wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
2188 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
2189 wpa_s->manual_scan_id);
2190 wpa_s->manual_scan_use_id = 0;
2191 } else {
2192 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
2193 }
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002194 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002195 wpas_notify_scan_done(wpa_s, 1);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002196 if (wpa_s->scan_work) {
2197 struct wpa_radio_work *work = wpa_s->scan_work;
2198 wpa_s->scan_work = NULL;
2199 radio_work_done(work);
2200 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002201
2202 if (wpa_s->wpa_state == WPA_SCANNING)
2203 wpa_supplicant_set_state(wpa_s, wpa_s->scan_prev_wpa_state);
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002204}
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07002205
2206
2207int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
2208{
2209 return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
2210}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002211
2212
2213struct wpa_driver_scan_params *
2214wpa_scan_clone_params(const struct wpa_driver_scan_params *src)
2215{
2216 struct wpa_driver_scan_params *params;
2217 size_t i;
2218 u8 *n;
2219
2220 params = os_zalloc(sizeof(*params));
2221 if (params == NULL)
2222 return NULL;
2223
2224 for (i = 0; i < src->num_ssids; i++) {
2225 if (src->ssids[i].ssid) {
2226 n = os_malloc(src->ssids[i].ssid_len);
2227 if (n == NULL)
2228 goto failed;
2229 os_memcpy(n, src->ssids[i].ssid,
2230 src->ssids[i].ssid_len);
2231 params->ssids[i].ssid = n;
2232 params->ssids[i].ssid_len = src->ssids[i].ssid_len;
2233 }
2234 }
2235 params->num_ssids = src->num_ssids;
2236
2237 if (src->extra_ies) {
2238 n = os_malloc(src->extra_ies_len);
2239 if (n == NULL)
2240 goto failed;
2241 os_memcpy(n, src->extra_ies, src->extra_ies_len);
2242 params->extra_ies = n;
2243 params->extra_ies_len = src->extra_ies_len;
2244 }
2245
2246 if (src->freqs) {
2247 int len = int_array_len(src->freqs);
2248 params->freqs = os_malloc((len + 1) * sizeof(int));
2249 if (params->freqs == NULL)
2250 goto failed;
2251 os_memcpy(params->freqs, src->freqs, (len + 1) * sizeof(int));
2252 }
2253
2254 if (src->filter_ssids) {
Dmitry Shmidt97672262014-02-03 13:02:54 -08002255 params->filter_ssids = os_malloc(sizeof(*params->filter_ssids) *
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002256 src->num_filter_ssids);
2257 if (params->filter_ssids == NULL)
2258 goto failed;
2259 os_memcpy(params->filter_ssids, src->filter_ssids,
Dmitry Shmidt97672262014-02-03 13:02:54 -08002260 sizeof(*params->filter_ssids) *
2261 src->num_filter_ssids);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002262 params->num_filter_ssids = src->num_filter_ssids;
2263 }
2264
2265 params->filter_rssi = src->filter_rssi;
2266 params->p2p_probe = src->p2p_probe;
2267 params->only_new_results = src->only_new_results;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07002268 params->low_priority = src->low_priority;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002269
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002270 if (src->sched_scan_plans_num > 0) {
2271 params->sched_scan_plans =
2272 os_malloc(sizeof(*src->sched_scan_plans) *
2273 src->sched_scan_plans_num);
2274 if (!params->sched_scan_plans)
2275 goto failed;
2276
2277 os_memcpy(params->sched_scan_plans, src->sched_scan_plans,
2278 sizeof(*src->sched_scan_plans) *
2279 src->sched_scan_plans_num);
2280 params->sched_scan_plans_num = src->sched_scan_plans_num;
2281 }
2282
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002283 if (src->mac_addr_rand) {
2284 params->mac_addr_rand = src->mac_addr_rand;
2285
2286 if (src->mac_addr && src->mac_addr_mask) {
2287 u8 *mac_addr;
2288
2289 mac_addr = os_malloc(2 * ETH_ALEN);
2290 if (!mac_addr)
2291 goto failed;
2292
2293 os_memcpy(mac_addr, src->mac_addr, ETH_ALEN);
2294 os_memcpy(mac_addr + ETH_ALEN, src->mac_addr_mask,
2295 ETH_ALEN);
2296 params->mac_addr = mac_addr;
2297 params->mac_addr_mask = mac_addr + ETH_ALEN;
2298 }
2299 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002300
2301 if (src->bssid) {
2302 u8 *bssid;
2303
2304 bssid = os_malloc(ETH_ALEN);
2305 if (!bssid)
2306 goto failed;
2307 os_memcpy(bssid, src->bssid, ETH_ALEN);
2308 params->bssid = bssid;
2309 }
2310
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002311 return params;
2312
2313failed:
2314 wpa_scan_free_params(params);
2315 return NULL;
2316}
2317
2318
2319void wpa_scan_free_params(struct wpa_driver_scan_params *params)
2320{
2321 size_t i;
2322
2323 if (params == NULL)
2324 return;
2325
2326 for (i = 0; i < params->num_ssids; i++)
2327 os_free((u8 *) params->ssids[i].ssid);
2328 os_free((u8 *) params->extra_ies);
2329 os_free(params->freqs);
2330 os_free(params->filter_ssids);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002331 os_free(params->sched_scan_plans);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002332
2333 /*
2334 * Note: params->mac_addr_mask points to same memory allocation and
2335 * must not be freed separately.
2336 */
2337 os_free((u8 *) params->mac_addr);
2338
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002339 os_free((u8 *) params->bssid);
2340
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002341 os_free(params);
2342}
Dmitry Shmidt98660862014-03-11 17:26:21 -07002343
2344
2345int wpas_start_pno(struct wpa_supplicant *wpa_s)
2346{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002347 int ret, prio;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002348 size_t i, num_ssid, num_match_ssid;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002349 struct wpa_ssid *ssid;
2350 struct wpa_driver_scan_params params;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002351 struct sched_scan_plan scan_plan;
Dmitry Shmidte4663042016-04-04 10:07:49 -07002352 unsigned int max_sched_scan_ssids;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002353
2354 if (!wpa_s->sched_scan_supported)
2355 return -1;
2356
Dmitry Shmidte4663042016-04-04 10:07:49 -07002357 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
2358 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
2359 else
2360 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
2361 if (max_sched_scan_ssids < 1)
2362 return -1;
2363
Dmitry Shmidt98660862014-03-11 17:26:21 -07002364 if (wpa_s->pno || wpa_s->pno_sched_pending)
2365 return 0;
2366
2367 if ((wpa_s->wpa_state > WPA_SCANNING) &&
2368 (wpa_s->wpa_state <= WPA_COMPLETED)) {
2369 wpa_printf(MSG_ERROR, "PNO: In assoc process");
2370 return -EAGAIN;
2371 }
2372
2373 if (wpa_s->wpa_state == WPA_SCANNING) {
2374 wpa_supplicant_cancel_scan(wpa_s);
2375 if (wpa_s->sched_scanning) {
2376 wpa_printf(MSG_DEBUG, "Schedule PNO on completion of "
2377 "ongoing sched scan");
2378 wpa_supplicant_cancel_sched_scan(wpa_s);
2379 wpa_s->pno_sched_pending = 1;
2380 return 0;
2381 }
2382 }
2383
2384 os_memset(&params, 0, sizeof(params));
2385
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002386 num_ssid = num_match_ssid = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002387 ssid = wpa_s->conf->ssid;
2388 while (ssid) {
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002389 if (!wpas_network_disabled(wpa_s, ssid)) {
2390 num_match_ssid++;
2391 if (ssid->scan_ssid)
2392 num_ssid++;
2393 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07002394 ssid = ssid->next;
2395 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002396
2397 if (num_match_ssid == 0) {
2398 wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
2399 return -1;
2400 }
2401
2402 if (num_match_ssid > num_ssid) {
2403 params.num_ssids++; /* wildcard */
2404 num_ssid++;
2405 }
2406
Dmitry Shmidte4663042016-04-04 10:07:49 -07002407 if (num_ssid > max_sched_scan_ssids) {
Dmitry Shmidt98660862014-03-11 17:26:21 -07002408 wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
Dmitry Shmidte4663042016-04-04 10:07:49 -07002409 "%u", max_sched_scan_ssids, (unsigned int) num_ssid);
2410 num_ssid = max_sched_scan_ssids;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002411 }
2412
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002413 if (num_match_ssid > wpa_s->max_match_sets) {
2414 num_match_ssid = wpa_s->max_match_sets;
2415 wpa_dbg(wpa_s, MSG_DEBUG, "PNO: Too many SSIDs to match");
Dmitry Shmidt98660862014-03-11 17:26:21 -07002416 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002417 params.filter_ssids = os_calloc(num_match_ssid,
2418 sizeof(struct wpa_driver_scan_filter));
Dmitry Shmidt98660862014-03-11 17:26:21 -07002419 if (params.filter_ssids == NULL)
2420 return -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002421
Dmitry Shmidt98660862014-03-11 17:26:21 -07002422 i = 0;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002423 prio = 0;
2424 ssid = wpa_s->conf->pssid[prio];
Dmitry Shmidt98660862014-03-11 17:26:21 -07002425 while (ssid) {
2426 if (!wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002427 if (ssid->scan_ssid && params.num_ssids < num_ssid) {
2428 params.ssids[params.num_ssids].ssid =
2429 ssid->ssid;
2430 params.ssids[params.num_ssids].ssid_len =
2431 ssid->ssid_len;
2432 params.num_ssids++;
2433 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07002434 os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
2435 ssid->ssid_len);
2436 params.filter_ssids[i].ssid_len = ssid->ssid_len;
2437 params.num_filter_ssids++;
2438 i++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002439 if (i == num_match_ssid)
Dmitry Shmidt98660862014-03-11 17:26:21 -07002440 break;
2441 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002442 if (ssid->pnext)
2443 ssid = ssid->pnext;
2444 else if (prio + 1 == wpa_s->conf->num_prio)
2445 break;
2446 else
2447 ssid = wpa_s->conf->pssid[++prio];
Dmitry Shmidt98660862014-03-11 17:26:21 -07002448 }
2449
2450 if (wpa_s->conf->filter_rssi)
2451 params.filter_rssi = wpa_s->conf->filter_rssi;
2452
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002453 if (wpa_s->sched_scan_plans_num) {
2454 params.sched_scan_plans = wpa_s->sched_scan_plans;
2455 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
2456 } else {
2457 /* Set one scan plan that will run infinitely */
2458 if (wpa_s->conf->sched_scan_interval)
2459 scan_plan.interval = wpa_s->conf->sched_scan_interval;
2460 else
2461 scan_plan.interval = 10;
2462
2463 scan_plan.iterations = 0;
2464 params.sched_scan_plans = &scan_plan;
2465 params.sched_scan_plans_num = 1;
2466 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07002467
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002468 if (params.freqs == NULL && wpa_s->manual_sched_scan_freqs) {
2469 wpa_dbg(wpa_s, MSG_DEBUG, "Limit sched scan to specified channels");
2470 params.freqs = wpa_s->manual_sched_scan_freqs;
2471 }
2472
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002473 if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_PNO) {
2474 params.mac_addr_rand = 1;
2475 if (wpa_s->mac_addr_pno) {
2476 params.mac_addr = wpa_s->mac_addr_pno;
2477 params.mac_addr_mask = wpa_s->mac_addr_pno + ETH_ALEN;
2478 }
2479 }
2480
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002481 ret = wpa_supplicant_start_sched_scan(wpa_s, &params);
Dmitry Shmidt98660862014-03-11 17:26:21 -07002482 os_free(params.filter_ssids);
2483 if (ret == 0)
2484 wpa_s->pno = 1;
2485 else
2486 wpa_msg(wpa_s, MSG_ERROR, "Failed to schedule PNO");
2487 return ret;
2488}
2489
2490
2491int wpas_stop_pno(struct wpa_supplicant *wpa_s)
2492{
2493 int ret = 0;
2494
2495 if (!wpa_s->pno)
2496 return 0;
2497
2498 ret = wpa_supplicant_stop_sched_scan(wpa_s);
2499
2500 wpa_s->pno = 0;
2501 wpa_s->pno_sched_pending = 0;
2502
2503 if (wpa_s->wpa_state == WPA_SCANNING)
2504 wpa_supplicant_req_scan(wpa_s, 0, 0);
2505
2506 return ret;
2507}
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002508
2509
2510void wpas_mac_addr_rand_scan_clear(struct wpa_supplicant *wpa_s,
2511 unsigned int type)
2512{
2513 type &= MAC_ADDR_RAND_ALL;
2514 wpa_s->mac_addr_rand_enable &= ~type;
2515
2516 if (type & MAC_ADDR_RAND_SCAN) {
2517 os_free(wpa_s->mac_addr_scan);
2518 wpa_s->mac_addr_scan = NULL;
2519 }
2520
2521 if (type & MAC_ADDR_RAND_SCHED_SCAN) {
2522 os_free(wpa_s->mac_addr_sched_scan);
2523 wpa_s->mac_addr_sched_scan = NULL;
2524 }
2525
2526 if (type & MAC_ADDR_RAND_PNO) {
2527 os_free(wpa_s->mac_addr_pno);
2528 wpa_s->mac_addr_pno = NULL;
2529 }
2530}
2531
2532
2533int wpas_mac_addr_rand_scan_set(struct wpa_supplicant *wpa_s,
2534 unsigned int type, const u8 *addr,
2535 const u8 *mask)
2536{
2537 u8 *tmp = NULL;
2538
2539 wpas_mac_addr_rand_scan_clear(wpa_s, type);
2540
2541 if (addr) {
2542 tmp = os_malloc(2 * ETH_ALEN);
2543 if (!tmp)
2544 return -1;
2545 os_memcpy(tmp, addr, ETH_ALEN);
2546 os_memcpy(tmp + ETH_ALEN, mask, ETH_ALEN);
2547 }
2548
2549 if (type == MAC_ADDR_RAND_SCAN) {
2550 wpa_s->mac_addr_scan = tmp;
2551 } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
2552 wpa_s->mac_addr_sched_scan = tmp;
2553 } else if (type == MAC_ADDR_RAND_PNO) {
2554 wpa_s->mac_addr_pno = tmp;
2555 } else {
2556 wpa_printf(MSG_INFO,
2557 "scan: Invalid MAC randomization type=0x%x",
2558 type);
2559 os_free(tmp);
2560 return -1;
2561 }
2562
2563 wpa_s->mac_addr_rand_enable |= type;
2564 return 0;
2565}
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002566
2567
2568int wpas_abort_ongoing_scan(struct wpa_supplicant *wpa_s)
2569{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002570 int scan_work = !!wpa_s->scan_work;
2571
2572#ifdef CONFIG_P2P
2573 scan_work |= !!wpa_s->p2p_scan_work;
2574#endif /* CONFIG_P2P */
2575
2576 if (scan_work && wpa_s->own_scan_running) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002577 wpa_dbg(wpa_s, MSG_DEBUG, "Abort an ongoing scan");
2578 return wpa_drv_abort_scan(wpa_s);
2579 }
2580
2581 return 0;
2582}
2583
2584
2585int wpas_sched_scan_plans_set(struct wpa_supplicant *wpa_s, const char *cmd)
2586{
2587 struct sched_scan_plan *scan_plans = NULL;
2588 const char *token, *context = NULL;
2589 unsigned int num = 0;
2590
2591 if (!cmd)
2592 return -1;
2593
2594 if (!cmd[0]) {
2595 wpa_printf(MSG_DEBUG, "Clear sched scan plans");
2596 os_free(wpa_s->sched_scan_plans);
2597 wpa_s->sched_scan_plans = NULL;
2598 wpa_s->sched_scan_plans_num = 0;
2599 return 0;
2600 }
2601
2602 while ((token = cstr_token(cmd, " ", &context))) {
2603 int ret;
2604 struct sched_scan_plan *scan_plan, *n;
2605
2606 n = os_realloc_array(scan_plans, num + 1, sizeof(*scan_plans));
2607 if (!n)
2608 goto fail;
2609
2610 scan_plans = n;
2611 scan_plan = &scan_plans[num];
2612 num++;
2613
2614 ret = sscanf(token, "%u:%u", &scan_plan->interval,
2615 &scan_plan->iterations);
2616 if (ret <= 0 || ret > 2 || !scan_plan->interval) {
2617 wpa_printf(MSG_ERROR,
2618 "Invalid sched scan plan input: %s", token);
2619 goto fail;
2620 }
2621
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002622 if (scan_plan->interval > wpa_s->max_sched_scan_plan_interval) {
2623 wpa_printf(MSG_WARNING,
2624 "scan plan %u: Scan interval too long(%u), use the maximum allowed(%u)",
2625 num, scan_plan->interval,
2626 wpa_s->max_sched_scan_plan_interval);
2627 scan_plan->interval =
2628 wpa_s->max_sched_scan_plan_interval;
2629 }
2630
2631 if (ret == 1) {
2632 scan_plan->iterations = 0;
2633 break;
2634 }
2635
2636 if (!scan_plan->iterations) {
2637 wpa_printf(MSG_ERROR,
2638 "scan plan %u: Number of iterations cannot be zero",
2639 num);
2640 goto fail;
2641 }
2642
2643 if (scan_plan->iterations >
2644 wpa_s->max_sched_scan_plan_iterations) {
2645 wpa_printf(MSG_WARNING,
2646 "scan plan %u: Too many iterations(%u), use the maximum allowed(%u)",
2647 num, scan_plan->iterations,
2648 wpa_s->max_sched_scan_plan_iterations);
2649 scan_plan->iterations =
2650 wpa_s->max_sched_scan_plan_iterations;
2651 }
2652
2653 wpa_printf(MSG_DEBUG,
2654 "scan plan %u: interval=%u iterations=%u",
2655 num, scan_plan->interval, scan_plan->iterations);
2656 }
2657
2658 if (!scan_plans) {
2659 wpa_printf(MSG_ERROR, "Invalid scan plans entry");
2660 goto fail;
2661 }
2662
2663 if (cstr_token(cmd, " ", &context) || scan_plans[num - 1].iterations) {
2664 wpa_printf(MSG_ERROR,
2665 "All scan plans but the last must specify a number of iterations");
2666 goto fail;
2667 }
2668
2669 wpa_printf(MSG_DEBUG, "scan plan %u (last plan): interval=%u",
2670 num, scan_plans[num - 1].interval);
2671
2672 if (num > wpa_s->max_sched_scan_plans) {
2673 wpa_printf(MSG_WARNING,
2674 "Too many scheduled scan plans (only %u supported)",
2675 wpa_s->max_sched_scan_plans);
2676 wpa_printf(MSG_WARNING,
2677 "Use only the first %u scan plans, and the last one (in infinite loop)",
2678 wpa_s->max_sched_scan_plans - 1);
2679 os_memcpy(&scan_plans[wpa_s->max_sched_scan_plans - 1],
2680 &scan_plans[num - 1], sizeof(*scan_plans));
2681 num = wpa_s->max_sched_scan_plans;
2682 }
2683
2684 os_free(wpa_s->sched_scan_plans);
2685 wpa_s->sched_scan_plans = scan_plans;
2686 wpa_s->sched_scan_plans_num = num;
2687
2688 return 0;
2689
2690fail:
2691 os_free(scan_plans);
2692 wpa_printf(MSG_ERROR, "invalid scan plans list");
2693 return -1;
2694}