blob: 119da28d00b4cd3469718e4ad45128603290251c [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;
590
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700591 /*
592 * For devices with max_ssids greater than 1, leave the last slot empty
593 * for adding the wildcard scan entry.
594 */
595 max_ssids = max_ssids > 1 ? max_ssids - 1 : max_ssids;
596
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700597 for (i = 0; i < wpa_s->scan_id_count; i++) {
598 unsigned int j;
599
600 ssid = wpa_config_get_network(wpa_s->conf, wpa_s->scan_id[i]);
601 if (!ssid || !ssid->scan_ssid)
602 continue;
603
604 for (j = 0; j < params->num_ssids; j++) {
605 if (params->ssids[j].ssid_len == ssid->ssid_len &&
606 params->ssids[j].ssid &&
607 os_memcmp(params->ssids[j].ssid, ssid->ssid,
608 ssid->ssid_len) == 0)
609 break;
610 }
611 if (j < params->num_ssids)
612 continue; /* already in the list */
613
614 if (params->num_ssids + 1 > max_ssids) {
615 wpa_printf(MSG_DEBUG,
616 "Over max scan SSIDs for manual request");
617 break;
618 }
619
620 wpa_printf(MSG_DEBUG, "Scan SSID (manual request): %s",
621 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
622 params->ssids[params->num_ssids].ssid = ssid->ssid;
623 params->ssids[params->num_ssids].ssid_len = ssid->ssid_len;
624 params->num_ssids++;
625 }
626
627 wpa_s->scan_id_count = 0;
628}
629
630
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700631static int wpa_set_ssids_from_scan_req(struct wpa_supplicant *wpa_s,
632 struct wpa_driver_scan_params *params,
633 size_t max_ssids)
634{
635 unsigned int i;
636
637 if (wpa_s->ssids_from_scan_req == NULL ||
638 wpa_s->num_ssids_from_scan_req == 0)
639 return 0;
640
641 if (wpa_s->num_ssids_from_scan_req > max_ssids) {
642 wpa_s->num_ssids_from_scan_req = max_ssids;
643 wpa_printf(MSG_DEBUG, "Over max scan SSIDs from scan req: %u",
644 (unsigned int) max_ssids);
645 }
646
647 for (i = 0; i < wpa_s->num_ssids_from_scan_req; i++) {
648 params->ssids[i].ssid = wpa_s->ssids_from_scan_req[i].ssid;
649 params->ssids[i].ssid_len =
650 wpa_s->ssids_from_scan_req[i].ssid_len;
651 wpa_hexdump_ascii(MSG_DEBUG, "specific SSID",
652 params->ssids[i].ssid,
653 params->ssids[i].ssid_len);
654 }
655
656 params->num_ssids = wpa_s->num_ssids_from_scan_req;
657 wpa_s->num_ssids_from_scan_req = 0;
658 return 1;
659}
660
661
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700662static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
663{
664 struct wpa_supplicant *wpa_s = eloop_ctx;
665 struct wpa_ssid *ssid;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800666 int ret, p2p_in_prog;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700667 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700668 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700669 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700670 size_t max_ssids;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800671 int connect_without_scan = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700672
Dmitry Shmidt18463232014-01-24 12:29:41 -0800673 if (wpa_s->pno || wpa_s->pno_sched_pending) {
674 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - PNO is in progress");
675 return;
676 }
677
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700678 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
679 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
680 return;
681 }
682
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800683 if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700684 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700685 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
686 return;
687 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800688
Dmitry Shmidt5887a9d2012-09-14 10:47:43 -0700689 if (wpa_s->scanning) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800690 /*
691 * If we are already in scanning state, we shall reschedule the
692 * the incoming scan request.
693 */
694 wpa_dbg(wpa_s, MSG_DEBUG, "Already scanning - Reschedule the incoming scan req");
695 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidt5887a9d2012-09-14 10:47:43 -0700696 return;
697 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800698
Dmitry Shmidt04949592012-07-19 12:16:46 -0700699 if (!wpa_supplicant_enabled_networks(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800700 wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700701 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
702 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
703 return;
704 }
705
706 if (wpa_s->conf->ap_scan != 0 &&
707 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
708 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
709 "overriding ap_scan configuration");
710 wpa_s->conf->ap_scan = 0;
711 wpas_notify_ap_scan_changed(wpa_s);
712 }
713
714 if (wpa_s->conf->ap_scan == 0) {
715 wpa_supplicant_gen_assoc_event(wpa_s);
716 return;
717 }
718
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800719 ssid = NULL;
720 if (wpa_s->scan_req != MANUAL_SCAN_REQ &&
721 wpa_s->connect_without_scan) {
722 connect_without_scan = 1;
723 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
724 if (ssid == wpa_s->connect_without_scan)
725 break;
726 }
727 }
728
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800729 p2p_in_prog = wpas_p2p_in_progress(wpa_s);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800730 if (p2p_in_prog && p2p_in_prog != 2 &&
731 (!ssid ||
732 (ssid->mode != WPAS_MODE_AP && ssid->mode != WPAS_MODE_P2P_GO))) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800733 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan while P2P operation is in progress");
734 wpa_supplicant_req_scan(wpa_s, 5, 0);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700735 return;
736 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700737
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800738 if (wpa_s->conf->ap_scan == 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700739 max_ssids = 1;
740 else {
741 max_ssids = wpa_s->max_scan_ssids;
742 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
743 max_ssids = WPAS_MAX_SCAN_SSIDS;
744 }
745
Dmitry Shmidt01904cf2013-12-05 11:08:35 -0800746 wpa_s->last_scan_req = wpa_s->scan_req;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800747 wpa_s->scan_req = NORMAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700748
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800749 if (connect_without_scan) {
750 wpa_s->connect_without_scan = NULL;
751 if (ssid) {
752 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
753 "without scan step");
754 wpa_supplicant_associate(wpa_s, NULL, ssid);
755 return;
756 }
757 }
758
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700759 os_memset(&params, 0, sizeof(params));
760
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800761 wpa_s->scan_prev_wpa_state = wpa_s->wpa_state;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700762 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
763 wpa_s->wpa_state == WPA_INACTIVE)
764 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
765
Dmitry Shmidt04949592012-07-19 12:16:46 -0700766 /*
767 * If autoscan has set its own scanning parameters
768 */
769 if (wpa_s->autoscan_params != NULL) {
770 scan_params = wpa_s->autoscan_params;
771 goto scan;
772 }
773
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700774 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
775 wpa_set_ssids_from_scan_req(wpa_s, &params, max_ssids)) {
776 wpa_printf(MSG_DEBUG, "Use specific SSIDs from SCAN command");
777 goto ssid_list_set;
778 }
779
Dmitry Shmidt04949592012-07-19 12:16:46 -0700780#ifdef CONFIG_P2P
781 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800782 wpa_s->go_params && !wpa_s->conf->passive_scan) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800783 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
784 wpa_s->p2p_in_provisioning,
785 wpa_s->show_group_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700786 params.ssids[0].ssid = wpa_s->go_params->ssid;
787 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
788 params.num_ssids = 1;
789 goto ssid_list_set;
790 }
Dmitry Shmidt15907092014-03-25 10:42:57 -0700791
792 if (wpa_s->p2p_in_invitation) {
793 if (wpa_s->current_ssid) {
794 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during invitation");
795 params.ssids[0].ssid = wpa_s->current_ssid->ssid;
796 params.ssids[0].ssid_len =
797 wpa_s->current_ssid->ssid_len;
798 params.num_ssids = 1;
799 } else {
800 wpa_printf(MSG_DEBUG, "P2P: No specific SSID known for scan during invitation");
801 }
802 goto ssid_list_set;
803 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700804#endif /* CONFIG_P2P */
805
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700806 /* Find the starting point from which to continue scanning */
807 ssid = wpa_s->conf->ssid;
808 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
809 while (ssid) {
810 if (ssid == wpa_s->prev_scan_ssid) {
811 ssid = ssid->next;
812 break;
813 }
814 ssid = ssid->next;
815 }
816 }
817
Dmitry Shmidt01904cf2013-12-05 11:08:35 -0800818 if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800819#ifdef CONFIG_AP
820 !wpa_s->ap_iface &&
821#endif /* CONFIG_AP */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -0800822 wpa_s->conf->ap_scan == 2) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700823 wpa_s->connect_without_scan = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800824 wpa_s->prev_scan_wildcard = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700825 wpa_supplicant_assoc_try(wpa_s, ssid);
826 return;
827 } else if (wpa_s->conf->ap_scan == 2) {
828 /*
829 * User-initiated scan request in ap_scan == 2; scan with
830 * wildcard SSID.
831 */
832 ssid = NULL;
Dmitry Shmidt98660862014-03-11 17:26:21 -0700833 } else if (wpa_s->reattach && wpa_s->current_ssid != NULL) {
834 /*
835 * Perform single-channel single-SSID scan for
836 * reassociate-to-same-BSS operation.
837 */
838 /* Setup SSID */
839 ssid = wpa_s->current_ssid;
840 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
841 ssid->ssid, ssid->ssid_len);
842 params.ssids[0].ssid = ssid->ssid;
843 params.ssids[0].ssid_len = ssid->ssid_len;
844 params.num_ssids = 1;
845
846 /*
847 * Allocate memory for frequency array, allocate one extra
848 * slot for the zero-terminator.
849 */
850 params.freqs = os_malloc(sizeof(int) * 2);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700851 if (params.freqs) {
852 params.freqs[0] = wpa_s->assoc_freq;
853 params.freqs[1] = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -0700854 }
Dmitry Shmidt98660862014-03-11 17:26:21 -0700855
856 /*
857 * Reset the reattach flag so that we fall back to full scan if
858 * this scan fails.
859 */
860 wpa_s->reattach = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700861 } else {
862 struct wpa_ssid *start = ssid, *tssid;
863 int freqs_set = 0;
864 if (ssid == NULL && max_ssids > 1)
865 ssid = wpa_s->conf->ssid;
866 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700867 if (!wpas_network_disabled(wpa_s, ssid) &&
868 ssid->scan_ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700869 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
870 ssid->ssid, ssid->ssid_len);
871 params.ssids[params.num_ssids].ssid =
872 ssid->ssid;
873 params.ssids[params.num_ssids].ssid_len =
874 ssid->ssid_len;
875 params.num_ssids++;
876 if (params.num_ssids + 1 >= max_ssids)
877 break;
878 }
879 ssid = ssid->next;
880 if (ssid == start)
881 break;
882 if (ssid == NULL && max_ssids > 1 &&
883 start != wpa_s->conf->ssid)
884 ssid = wpa_s->conf->ssid;
885 }
886
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700887 if (wpa_s->scan_id_count &&
888 wpa_s->last_scan_req == MANUAL_SCAN_REQ)
889 wpa_set_scan_ssids(wpa_s, &params, max_ssids);
890
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800891 for (tssid = wpa_s->conf->ssid;
892 wpa_s->last_scan_req != MANUAL_SCAN_REQ && tssid;
893 tssid = tssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700894 if (wpas_network_disabled(wpa_s, tssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700895 continue;
896 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
897 int_array_concat(&params.freqs,
898 tssid->scan_freq);
899 } else {
900 os_free(params.freqs);
901 params.freqs = NULL;
902 }
903 freqs_set = 1;
904 }
905 int_array_sort_unique(params.freqs);
906 }
907
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800908 if (ssid && max_ssids == 1) {
909 /*
910 * If the driver is limited to 1 SSID at a time interleave
911 * wildcard SSID scans with specific SSID scans to avoid
912 * waiting a long time for a wildcard scan.
913 */
914 if (!wpa_s->prev_scan_wildcard) {
915 params.ssids[0].ssid = NULL;
916 params.ssids[0].ssid_len = 0;
917 wpa_s->prev_scan_wildcard = 1;
918 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
919 "wildcard SSID (Interleave with specific)");
920 } else {
921 wpa_s->prev_scan_ssid = ssid;
922 wpa_s->prev_scan_wildcard = 0;
923 wpa_dbg(wpa_s, MSG_DEBUG,
924 "Starting AP scan for specific SSID: %s",
925 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700926 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800927 } else if (ssid) {
928 /* max_ssids > 1 */
929
930 wpa_s->prev_scan_ssid = ssid;
931 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
932 "the scan request");
933 params.num_ssids++;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800934 } else if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
935 wpa_s->manual_scan_passive && params.num_ssids == 0) {
936 wpa_dbg(wpa_s, MSG_DEBUG, "Use passive scan based on manual request");
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800937 } else if (wpa_s->conf->passive_scan) {
938 wpa_dbg(wpa_s, MSG_DEBUG,
939 "Use passive scan based on configuration");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700940 } else {
941 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
942 params.num_ssids++;
943 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
944 "SSID");
945 }
946
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700947ssid_list_set:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800948 wpa_supplicant_optimize_freqs(wpa_s, &params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700949 extra_ie = wpa_supplicant_extra_ies(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700950
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800951 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800952 wpa_s->manual_scan_only_new) {
953 wpa_printf(MSG_DEBUG,
954 "Request driver to clear scan cache due to manual only_new=1 scan");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800955 params.only_new_results = 1;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800956 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800957
958 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs == NULL &&
959 wpa_s->manual_scan_freqs) {
960 wpa_dbg(wpa_s, MSG_DEBUG, "Limit manual scan to specified channels");
961 params.freqs = wpa_s->manual_scan_freqs;
962 wpa_s->manual_scan_freqs = NULL;
963 }
964
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700965 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
966 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
967 "generated frequency list");
968 params.freqs = wpa_s->next_scan_freqs;
969 } else
970 os_free(wpa_s->next_scan_freqs);
971 wpa_s->next_scan_freqs = NULL;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700972 wpa_setband_scan_freqs(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700973
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700974 /* See if user specified frequencies. If so, scan only those. */
975 if (wpa_s->conf->freq_list && !params.freqs) {
976 wpa_dbg(wpa_s, MSG_DEBUG,
977 "Optimize scan based on conf->freq_list");
978 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
979 }
980
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700981 /* Use current associated channel? */
982 if (wpa_s->conf->scan_cur_freq && !params.freqs) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700983 unsigned int num = wpa_s->num_multichan_concurrent;
984
985 params.freqs = os_calloc(num + 1, sizeof(int));
986 if (params.freqs) {
987 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
988 if (num > 0) {
989 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
990 "current operating channels since "
991 "scan_cur_freq is enabled");
992 } else {
993 os_free(params.freqs);
994 params.freqs = NULL;
995 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700996 }
997 }
998
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700999 params.filter_ssids = wpa_supplicant_build_filter_ssids(
1000 wpa_s->conf, &params.num_filter_ssids);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001001 if (extra_ie) {
1002 params.extra_ies = wpabuf_head(extra_ie);
1003 params.extra_ies_len = wpabuf_len(extra_ie);
1004 }
1005
1006#ifdef CONFIG_P2P
Dmitry Shmidt413dde72014-04-11 10:23:22 -07001007 if (wpa_s->p2p_in_provisioning || wpa_s->p2p_in_invitation ||
Dmitry Shmidt04949592012-07-19 12:16:46 -07001008 (wpa_s->show_group_started && wpa_s->go_params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001009 /*
1010 * The interface may not yet be in P2P mode, so we have to
1011 * explicitly request P2P probe to disable CCK rates.
1012 */
1013 params.p2p_probe = 1;
1014 }
1015#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001016
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001017 if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) {
1018 params.mac_addr_rand = 1;
1019 if (wpa_s->mac_addr_scan) {
1020 params.mac_addr = wpa_s->mac_addr_scan;
1021 params.mac_addr_mask = wpa_s->mac_addr_scan + ETH_ALEN;
1022 }
1023 }
1024
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001025 if (!is_zero_ether_addr(wpa_s->next_scan_bssid)) {
1026 struct wpa_bss *bss;
1027
1028 params.bssid = wpa_s->next_scan_bssid;
1029 bss = wpa_bss_get_bssid_latest(wpa_s, params.bssid);
1030 if (bss && bss->ssid_len && params.num_ssids == 1 &&
1031 params.ssids[0].ssid_len == 0) {
1032 params.ssids[0].ssid = bss->ssid;
1033 params.ssids[0].ssid_len = bss->ssid_len;
1034 wpa_dbg(wpa_s, MSG_DEBUG,
1035 "Scan a previously specified BSSID " MACSTR
1036 " and SSID %s",
1037 MAC2STR(params.bssid),
1038 wpa_ssid_txt(bss->ssid, bss->ssid_len));
1039 } else {
1040 wpa_dbg(wpa_s, MSG_DEBUG,
1041 "Scan a previously specified BSSID " MACSTR,
1042 MAC2STR(params.bssid));
1043 }
1044 }
1045
Dmitry Shmidt04949592012-07-19 12:16:46 -07001046 scan_params = &params;
1047
1048scan:
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001049#ifdef CONFIG_P2P
1050 /*
1051 * If the driver does not support multi-channel concurrency and a
1052 * virtual interface that shares the same radio with the wpa_s interface
1053 * is operating there may not be need to scan other channels apart from
1054 * the current operating channel on the other virtual interface. Filter
1055 * out other channels in case we are trying to find a connection for a
1056 * station interface when we are not configured to prefer station
1057 * connection and a concurrent operation is already in process.
1058 */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001059 if (wpa_s->scan_for_connection &&
1060 wpa_s->last_scan_req == NORMAL_SCAN_REQ &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001061 !scan_params->freqs && !params.freqs &&
1062 wpas_is_p2p_prioritized(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001063 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
1064 non_p2p_network_enabled(wpa_s)) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001065 unsigned int num = wpa_s->num_multichan_concurrent;
1066
1067 params.freqs = os_calloc(num + 1, sizeof(int));
1068 if (params.freqs) {
1069 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
1070 if (num > 0 && num == wpa_s->num_multichan_concurrent) {
1071 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
1072 } else {
1073 os_free(params.freqs);
1074 params.freqs = NULL;
1075 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001076 }
1077 }
1078#endif /* CONFIG_P2P */
1079
Dmitry Shmidt04949592012-07-19 12:16:46 -07001080 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001081
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001082 if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs &&
1083 !wpa_s->manual_scan_freqs) {
1084 /* Restore manual_scan_freqs for the next attempt */
1085 wpa_s->manual_scan_freqs = params.freqs;
1086 params.freqs = NULL;
1087 }
1088
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001089 wpabuf_free(extra_ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001090 os_free(params.freqs);
1091 os_free(params.filter_ssids);
1092
1093 if (ret) {
1094 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001095 if (wpa_s->scan_prev_wpa_state != wpa_s->wpa_state)
1096 wpa_supplicant_set_state(wpa_s,
1097 wpa_s->scan_prev_wpa_state);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001098 /* Restore scan_req since we will try to scan again */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001099 wpa_s->scan_req = wpa_s->last_scan_req;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001100 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001101 } else {
1102 wpa_s->scan_for_connection = 0;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001103#ifdef CONFIG_INTERWORKING
1104 wpa_s->interworking_fast_assoc_tried = 0;
1105#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001106 if (params.bssid)
1107 os_memset(wpa_s->next_scan_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001108 }
1109}
1110
1111
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001112void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
1113{
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001114 struct os_reltime remaining, new_int;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001115 int cancelled;
1116
1117 cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
1118 &remaining);
1119
1120 new_int.sec = sec;
1121 new_int.usec = 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001122 if (cancelled && os_reltime_before(&remaining, &new_int)) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001123 new_int.sec = remaining.sec;
1124 new_int.usec = remaining.usec;
1125 }
1126
Dmitry Shmidt051af732013-10-22 13:52:46 -07001127 if (cancelled) {
1128 eloop_register_timeout(new_int.sec, new_int.usec,
1129 wpa_supplicant_scan, wpa_s, NULL);
1130 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001131 wpa_s->scan_interval = sec;
1132}
1133
1134
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001135/**
1136 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
1137 * @wpa_s: Pointer to wpa_supplicant data
1138 * @sec: Number of seconds after which to scan
1139 * @usec: Number of microseconds after which to scan
1140 *
1141 * This function is used to schedule a scan for neighboring access points after
1142 * the specified time.
1143 */
1144void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
1145{
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001146 int res;
1147
1148 if (wpa_s->p2p_mgmt) {
1149 wpa_dbg(wpa_s, MSG_DEBUG,
1150 "Ignore scan request (%d.%06d sec) on p2p_mgmt interface",
1151 sec, usec);
1152 return;
1153 }
1154
1155 res = eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s,
1156 NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001157 if (res == 1) {
1158 wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d.%06d sec",
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001159 sec, usec);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001160 } else if (res == 0) {
1161 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore new scan request for %d.%06d sec since an earlier request is scheduled to trigger sooner",
1162 sec, usec);
1163 } else {
1164 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d.%06d sec",
1165 sec, usec);
1166 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001167 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001168}
1169
1170
1171/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001172 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
1173 * @wpa_s: Pointer to wpa_supplicant data
1174 * @sec: Number of seconds after which to scan
1175 * @usec: Number of microseconds after which to scan
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001176 * Returns: 0 on success or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001177 *
1178 * This function is used to schedule periodic scans for neighboring
1179 * access points after the specified time.
1180 */
1181int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
1182 int sec, int usec)
1183{
1184 if (!wpa_s->sched_scan_supported)
1185 return -1;
1186
1187 eloop_register_timeout(sec, usec,
1188 wpa_supplicant_delayed_sched_scan_timeout,
1189 wpa_s, NULL);
1190
1191 return 0;
1192}
1193
1194
1195/**
1196 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
1197 * @wpa_s: Pointer to wpa_supplicant data
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001198 * Returns: 0 is sched_scan was started or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001199 *
1200 * This function is used to schedule periodic scans for neighboring
1201 * access points repeating the scan continuously.
1202 */
1203int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
1204{
1205 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001206 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001207 enum wpa_states prev_state;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001208 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001209 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001210 int ret;
1211 unsigned int max_sched_scan_ssids;
1212 int wildcard = 0;
1213 int need_ssids;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001214 struct sched_scan_plan scan_plan;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001215
1216 if (!wpa_s->sched_scan_supported)
1217 return -1;
1218
1219 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
1220 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
1221 else
1222 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001223 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001224 return -1;
1225
1226 if (wpa_s->sched_scanning) {
1227 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
1228 return 0;
1229 }
1230
1231 need_ssids = 0;
1232 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001233 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001234 /* Use wildcard SSID to find this network */
1235 wildcard = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001236 } else if (!wpas_network_disabled(wpa_s, ssid) &&
1237 ssid->ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001238 need_ssids++;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001239
1240#ifdef CONFIG_WPS
1241 if (!wpas_network_disabled(wpa_s, ssid) &&
1242 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1243 /*
1244 * Normal scan is more reliable and faster for WPS
1245 * operations and since these are for short periods of
1246 * time, the benefit of trying to use sched_scan would
1247 * be limited.
1248 */
1249 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1250 "sched_scan for WPS");
1251 return -1;
1252 }
1253#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001254 }
1255 if (wildcard)
1256 need_ssids++;
1257
1258 if (wpa_s->normal_scans < 3 &&
1259 (need_ssids <= wpa_s->max_scan_ssids ||
1260 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
1261 /*
1262 * When normal scan can speed up operations, use that for the
1263 * first operations before starting the sched_scan to allow
1264 * user space sleep more. We do this only if the normal scan
1265 * has functionality that is suitable for this or if the
1266 * sched_scan does not have better support for multiple SSIDs.
1267 */
1268 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1269 "sched_scan for initial scans (normal_scans=%d)",
1270 wpa_s->normal_scans);
1271 return -1;
1272 }
1273
1274 os_memset(&params, 0, sizeof(params));
1275
1276 /* If we can't allocate space for the filters, we just don't filter */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001277 params.filter_ssids = os_calloc(wpa_s->max_match_sets,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001278 sizeof(struct wpa_driver_scan_filter));
1279
1280 prev_state = wpa_s->wpa_state;
1281 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1282 wpa_s->wpa_state == WPA_INACTIVE)
1283 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1284
Dmitry Shmidt04949592012-07-19 12:16:46 -07001285 if (wpa_s->autoscan_params != NULL) {
1286 scan_params = wpa_s->autoscan_params;
1287 goto scan;
1288 }
1289
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001290 /* Find the starting point from which to continue scanning */
1291 ssid = wpa_s->conf->ssid;
1292 if (wpa_s->prev_sched_ssid) {
1293 while (ssid) {
1294 if (ssid == wpa_s->prev_sched_ssid) {
1295 ssid = ssid->next;
1296 break;
1297 }
1298 ssid = ssid->next;
1299 }
1300 }
1301
1302 if (!ssid || !wpa_s->prev_sched_ssid) {
1303 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001304 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1305 wpa_s->first_sched_scan = 1;
1306 ssid = wpa_s->conf->ssid;
1307 wpa_s->prev_sched_ssid = ssid;
1308 }
1309
1310 if (wildcard) {
1311 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1312 params.num_ssids++;
1313 }
1314
1315 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001316 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001317 goto next;
1318
1319 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1320 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1321 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1322 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1323 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1324 ssid->ssid, ssid->ssid_len);
1325 params.filter_ssids[params.num_filter_ssids].ssid_len =
1326 ssid->ssid_len;
1327 params.num_filter_ssids++;
1328 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1329 {
1330 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1331 "filter for sched_scan - drop filter");
1332 os_free(params.filter_ssids);
1333 params.filter_ssids = NULL;
1334 params.num_filter_ssids = 0;
1335 }
1336
1337 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1338 if (params.num_ssids == max_sched_scan_ssids)
1339 break; /* only room for broadcast SSID */
1340 wpa_dbg(wpa_s, MSG_DEBUG,
1341 "add to active scan ssid: %s",
1342 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1343 params.ssids[params.num_ssids].ssid =
1344 ssid->ssid;
1345 params.ssids[params.num_ssids].ssid_len =
1346 ssid->ssid_len;
1347 params.num_ssids++;
1348 if (params.num_ssids >= max_sched_scan_ssids) {
1349 wpa_s->prev_sched_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001350 do {
1351 ssid = ssid->next;
1352 } while (ssid &&
1353 (wpas_network_disabled(wpa_s, ssid) ||
1354 !ssid->scan_ssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001355 break;
1356 }
1357 }
1358
1359 next:
1360 wpa_s->prev_sched_ssid = ssid;
1361 ssid = ssid->next;
1362 }
1363
1364 if (params.num_filter_ssids == 0) {
1365 os_free(params.filter_ssids);
1366 params.filter_ssids = NULL;
1367 }
1368
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001369 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1370 if (extra_ie) {
1371 params.extra_ies = wpabuf_head(extra_ie);
1372 params.extra_ies_len = wpabuf_len(extra_ie);
1373 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001374
Dmitry Shmidt18463232014-01-24 12:29:41 -08001375 if (wpa_s->conf->filter_rssi)
1376 params.filter_rssi = wpa_s->conf->filter_rssi;
1377
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001378 /* See if user specified frequencies. If so, scan only those. */
1379 if (wpa_s->conf->freq_list && !params.freqs) {
1380 wpa_dbg(wpa_s, MSG_DEBUG,
1381 "Optimize scan based on conf->freq_list");
1382 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1383 }
1384
Dmitry Shmidt04949592012-07-19 12:16:46 -07001385 scan_params = &params;
1386
1387scan:
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001388 wpa_s->sched_scan_timed_out = 0;
1389
1390 /*
1391 * We cannot support multiple scan plans if the scan request includes
1392 * too many SSID's, so in this case use only the last scan plan and make
1393 * it run infinitely. It will be stopped by the timeout.
1394 */
1395 if (wpa_s->sched_scan_plans_num == 1 ||
1396 (wpa_s->sched_scan_plans_num && !ssid && wpa_s->first_sched_scan)) {
1397 params.sched_scan_plans = wpa_s->sched_scan_plans;
1398 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
1399 } else if (wpa_s->sched_scan_plans_num > 1) {
1400 wpa_dbg(wpa_s, MSG_DEBUG,
1401 "Too many SSIDs. Default to using single scheduled_scan plan");
1402 params.sched_scan_plans =
1403 &wpa_s->sched_scan_plans[wpa_s->sched_scan_plans_num -
1404 1];
1405 params.sched_scan_plans_num = 1;
1406 } else {
1407 if (wpa_s->conf->sched_scan_interval)
1408 scan_plan.interval = wpa_s->conf->sched_scan_interval;
1409 else
1410 scan_plan.interval = 10;
1411
1412 if (scan_plan.interval > wpa_s->max_sched_scan_plan_interval) {
1413 wpa_printf(MSG_WARNING,
1414 "Scan interval too long(%u), use the maximum allowed(%u)",
1415 scan_plan.interval,
1416 wpa_s->max_sched_scan_plan_interval);
1417 scan_plan.interval =
1418 wpa_s->max_sched_scan_plan_interval;
1419 }
1420
1421 scan_plan.iterations = 0;
1422 params.sched_scan_plans = &scan_plan;
1423 params.sched_scan_plans_num = 1;
1424 }
1425
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001426 if (ssid || !wpa_s->first_sched_scan) {
1427 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001428 "Starting sched scan: interval %u timeout %d",
1429 params.sched_scan_plans[0].interval,
1430 wpa_s->sched_scan_timeout);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001431 } else {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001432 wpa_dbg(wpa_s, MSG_DEBUG, "Starting sched scan (no timeout)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001433 }
1434
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001435 wpa_setband_scan_freqs(wpa_s, scan_params);
1436
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001437 if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCHED_SCAN) {
1438 params.mac_addr_rand = 1;
1439 if (wpa_s->mac_addr_sched_scan) {
1440 params.mac_addr = wpa_s->mac_addr_sched_scan;
1441 params.mac_addr_mask = wpa_s->mac_addr_sched_scan +
1442 ETH_ALEN;
1443 }
1444 }
1445
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001446 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001447 wpabuf_free(extra_ie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001448 os_free(params.filter_ssids);
1449 if (ret) {
1450 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1451 if (prev_state != wpa_s->wpa_state)
1452 wpa_supplicant_set_state(wpa_s, prev_state);
1453 return ret;
1454 }
1455
1456 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1457 if (ssid || !wpa_s->first_sched_scan) {
1458 wpa_s->sched_scan_timed_out = 0;
1459 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1460 wpa_supplicant_sched_scan_timeout,
1461 wpa_s, NULL);
1462 wpa_s->first_sched_scan = 0;
1463 wpa_s->sched_scan_timeout /= 2;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001464 params.sched_scan_plans[0].interval *= 2;
1465 if ((unsigned int) wpa_s->sched_scan_timeout <
1466 params.sched_scan_plans[0].interval ||
1467 params.sched_scan_plans[0].interval >
1468 wpa_s->max_sched_scan_plan_interval) {
1469 params.sched_scan_plans[0].interval = 10;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001470 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1471 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001472 }
1473
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001474 /* If there is no more ssids, start next time from the beginning */
1475 if (!ssid)
1476 wpa_s->prev_sched_ssid = NULL;
1477
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001478 return 0;
1479}
1480
1481
1482/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001483 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1484 * @wpa_s: Pointer to wpa_supplicant data
1485 *
1486 * This function is used to cancel a scan request scheduled with
1487 * wpa_supplicant_req_scan().
1488 */
1489void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1490{
1491 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1492 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001493}
1494
1495
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001496/**
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001497 * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
1498 * @wpa_s: Pointer to wpa_supplicant data
1499 *
1500 * This function is used to stop a delayed scheduled scan.
1501 */
1502void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
1503{
1504 if (!wpa_s->sched_scan_supported)
1505 return;
1506
1507 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
1508 eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
1509 wpa_s, NULL);
1510}
1511
1512
1513/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001514 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1515 * @wpa_s: Pointer to wpa_supplicant data
1516 *
1517 * This function is used to stop a periodic scheduled scan.
1518 */
1519void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1520{
1521 if (!wpa_s->sched_scanning)
1522 return;
1523
1524 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1525 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1526 wpa_supplicant_stop_sched_scan(wpa_s);
1527}
1528
1529
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001530/**
1531 * wpa_supplicant_notify_scanning - Indicate possible scan state change
1532 * @wpa_s: Pointer to wpa_supplicant data
1533 * @scanning: Whether scanning is currently in progress
1534 *
1535 * This function is to generate scanning notifycations. It is called whenever
1536 * there may have been a change in scanning (scan started, completed, stopped).
1537 * wpas_notify_scanning() is called whenever the scanning state changed from the
1538 * previously notified state.
1539 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001540void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1541 int scanning)
1542{
1543 if (wpa_s->scanning != scanning) {
1544 wpa_s->scanning = scanning;
1545 wpas_notify_scanning(wpa_s);
1546 }
1547}
1548
1549
1550static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1551{
1552 int rate = 0;
1553 const u8 *ie;
1554 int i;
1555
1556 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1557 for (i = 0; ie && i < ie[1]; i++) {
1558 if ((ie[i + 2] & 0x7f) > rate)
1559 rate = ie[i + 2] & 0x7f;
1560 }
1561
1562 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1563 for (i = 0; ie && i < ie[1]; i++) {
1564 if ((ie[i + 2] & 0x7f) > rate)
1565 rate = ie[i + 2] & 0x7f;
1566 }
1567
1568 return rate;
1569}
1570
1571
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001572/**
1573 * wpa_scan_get_ie - Fetch a specified information element from a scan result
1574 * @res: Scan result entry
1575 * @ie: Information element identitifier (WLAN_EID_*)
1576 * Returns: Pointer to the information element (id field) or %NULL if not found
1577 *
1578 * This function returns the first matching information element in the scan
1579 * result.
1580 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001581const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1582{
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001583 return get_ie((const u8 *) (res + 1), res->ie_len, ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001584}
1585
1586
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001587/**
1588 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1589 * @res: Scan result entry
1590 * @vendor_type: Vendor type (four octets starting the IE payload)
1591 * Returns: Pointer to the information element (id field) or %NULL if not found
1592 *
1593 * This function returns the first matching information element in the scan
1594 * result.
1595 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001596const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1597 u32 vendor_type)
1598{
1599 const u8 *end, *pos;
1600
1601 pos = (const u8 *) (res + 1);
1602 end = pos + res->ie_len;
1603
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001604 while (end - pos > 1) {
1605 if (2 + pos[1] > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001606 break;
1607 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1608 vendor_type == WPA_GET_BE32(&pos[2]))
1609 return pos;
1610 pos += 2 + pos[1];
1611 }
1612
1613 return NULL;
1614}
1615
1616
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001617/**
Dmitry Shmidt96571392013-10-14 12:54:46 -07001618 * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
1619 * @res: Scan result entry
1620 * @vendor_type: Vendor type (four octets starting the IE payload)
1621 * Returns: Pointer to the information element (id field) or %NULL if not found
1622 *
1623 * This function returns the first matching information element in the scan
1624 * result.
1625 *
1626 * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
1627 * from Beacon frames instead of either Beacon or Probe Response frames.
1628 */
1629const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
1630 u32 vendor_type)
1631{
1632 const u8 *end, *pos;
1633
1634 if (res->beacon_ie_len == 0)
1635 return NULL;
1636
1637 pos = (const u8 *) (res + 1);
1638 pos += res->ie_len;
1639 end = pos + res->beacon_ie_len;
1640
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001641 while (end - pos > 1) {
1642 if (2 + pos[1] > end - pos)
Dmitry Shmidt96571392013-10-14 12:54:46 -07001643 break;
1644 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1645 vendor_type == WPA_GET_BE32(&pos[2]))
1646 return pos;
1647 pos += 2 + pos[1];
1648 }
1649
1650 return NULL;
1651}
1652
1653
1654/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001655 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
1656 * @res: Scan result entry
1657 * @vendor_type: Vendor type (four octets starting the IE payload)
1658 * Returns: Pointer to the information element payload or %NULL if not found
1659 *
1660 * This function returns concatenated payload of possibly fragmented vendor
1661 * specific information elements in the scan result. The caller is responsible
1662 * for freeing the returned buffer.
1663 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001664struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1665 u32 vendor_type)
1666{
1667 struct wpabuf *buf;
1668 const u8 *end, *pos;
1669
1670 buf = wpabuf_alloc(res->ie_len);
1671 if (buf == NULL)
1672 return NULL;
1673
1674 pos = (const u8 *) (res + 1);
1675 end = pos + res->ie_len;
1676
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001677 while (end - pos > 1) {
1678 if (2 + pos[1] > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001679 break;
1680 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1681 vendor_type == WPA_GET_BE32(&pos[2]))
1682 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1683 pos += 2 + pos[1];
1684 }
1685
1686 if (wpabuf_len(buf) == 0) {
1687 wpabuf_free(buf);
1688 buf = NULL;
1689 }
1690
1691 return buf;
1692}
1693
1694
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001695/*
1696 * Channels with a great SNR can operate at full rate. What is a great SNR?
1697 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1698 * rule of thumb is that any SNR above 20 is good." This one
1699 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1700 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1701 * conservative value.
1702 */
1703#define GREAT_SNR 30
1704
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001705#define IS_5GHZ(n) (n > 4000)
1706
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001707/* Compare function for sorting scan results. Return >0 if @b is considered
1708 * better. */
1709static int wpa_scan_result_compar(const void *a, const void *b)
1710{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001711#define MIN(a,b) a < b ? a : b
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001712 struct wpa_scan_res **_wa = (void *) a;
1713 struct wpa_scan_res **_wb = (void *) b;
1714 struct wpa_scan_res *wa = *_wa;
1715 struct wpa_scan_res *wb = *_wb;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001716 int wpa_a, wpa_b;
1717 int snr_a, snr_b, snr_a_full, snr_b_full;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001718
1719 /* WPA/WPA2 support preferred */
1720 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1721 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1722 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1723 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1724
1725 if (wpa_b && !wpa_a)
1726 return 1;
1727 if (!wpa_b && wpa_a)
1728 return -1;
1729
1730 /* privacy support preferred */
1731 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1732 (wb->caps & IEEE80211_CAP_PRIVACY))
1733 return 1;
1734 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1735 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1736 return -1;
1737
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001738 if (wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001739 snr_a_full = wa->snr;
1740 snr_a = MIN(wa->snr, GREAT_SNR);
1741 snr_b_full = wb->snr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001742 snr_b = MIN(wb->snr, GREAT_SNR);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001743 } else {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001744 /* Level is not in dBm, so we can't calculate
1745 * SNR. Just use raw level (units unknown). */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001746 snr_a = snr_a_full = wa->level;
1747 snr_b = snr_b_full = wb->level;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001748 }
1749
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001750 /* if SNR is close, decide by max rate or frequency band */
1751 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001752 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001753 if (wa->est_throughput != wb->est_throughput)
1754 return wb->est_throughput - wa->est_throughput;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001755 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1756 return IS_5GHZ(wa->freq) ? -1 : 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001757 }
1758
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001759 /* all things being equal, use SNR; if SNRs are
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001760 * identical, use quality values since some drivers may only report
1761 * that value and leave the signal level zero */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001762 if (snr_b_full == snr_a_full)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001763 return wb->qual - wa->qual;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001764 return snr_b_full - snr_a_full;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001765#undef MIN
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001766}
1767
1768
1769#ifdef CONFIG_WPS
1770/* Compare function for sorting scan results when searching a WPS AP for
1771 * provisioning. Return >0 if @b is considered better. */
1772static int wpa_scan_result_wps_compar(const void *a, const void *b)
1773{
1774 struct wpa_scan_res **_wa = (void *) a;
1775 struct wpa_scan_res **_wb = (void *) b;
1776 struct wpa_scan_res *wa = *_wa;
1777 struct wpa_scan_res *wb = *_wb;
1778 int uses_wps_a, uses_wps_b;
1779 struct wpabuf *wps_a, *wps_b;
1780 int res;
1781
1782 /* Optimization - check WPS IE existence before allocated memory and
1783 * doing full reassembly. */
1784 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1785 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1786 if (uses_wps_a && !uses_wps_b)
1787 return -1;
1788 if (!uses_wps_a && uses_wps_b)
1789 return 1;
1790
1791 if (uses_wps_a && uses_wps_b) {
1792 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1793 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1794 res = wps_ap_priority_compar(wps_a, wps_b);
1795 wpabuf_free(wps_a);
1796 wpabuf_free(wps_b);
1797 if (res)
1798 return res;
1799 }
1800
1801 /*
1802 * Do not use current AP security policy as a sorting criteria during
1803 * WPS provisioning step since the AP may get reconfigured at the
1804 * completion of provisioning.
1805 */
1806
1807 /* all things being equal, use signal level; if signal levels are
1808 * identical, use quality values since some drivers may only report
1809 * that value and leave the signal level zero */
1810 if (wb->level == wa->level)
1811 return wb->qual - wa->qual;
1812 return wb->level - wa->level;
1813}
1814#endif /* CONFIG_WPS */
1815
1816
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001817static void dump_scan_res(struct wpa_scan_results *scan_res)
1818{
1819#ifndef CONFIG_NO_STDOUT_DEBUG
1820 size_t i;
1821
1822 if (scan_res->res == NULL || scan_res->num == 0)
1823 return;
1824
1825 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1826
1827 for (i = 0; i < scan_res->num; i++) {
1828 struct wpa_scan_res *r = scan_res->res[i];
Dmitry Shmidt04949592012-07-19 12:16:46 -07001829 u8 *pos;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001830 if (r->flags & WPA_SCAN_LEVEL_DBM) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001831 int noise_valid = !(r->flags & WPA_SCAN_NOISE_INVALID);
1832
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001833 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001834 "noise=%d%s level=%d snr=%d%s flags=0x%x age=%u est=%u",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001835 MAC2STR(r->bssid), r->freq, r->qual,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001836 r->noise, noise_valid ? "" : "~", r->level,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001837 r->snr, r->snr >= GREAT_SNR ? "*" : "",
1838 r->flags,
1839 r->age, r->est_throughput);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001840 } else {
1841 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001842 "noise=%d level=%d flags=0x%x age=%u est=%u",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001843 MAC2STR(r->bssid), r->freq, r->qual,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001844 r->noise, r->level, r->flags, r->age,
1845 r->est_throughput);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001846 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001847 pos = (u8 *) (r + 1);
1848 if (r->ie_len)
1849 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
1850 pos += r->ie_len;
1851 if (r->beacon_ie_len)
1852 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
1853 pos, r->beacon_ie_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001854 }
1855#endif /* CONFIG_NO_STDOUT_DEBUG */
1856}
1857
1858
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001859/**
1860 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
1861 * @wpa_s: Pointer to wpa_supplicant data
1862 * @bssid: BSSID to check
1863 * Returns: 0 if the BSSID is filtered or 1 if not
1864 *
1865 * This function is used to filter out specific BSSIDs from scan reslts mainly
1866 * for testing purposes (SET bssid_filter ctrl_iface command).
1867 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001868int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
1869 const u8 *bssid)
1870{
1871 size_t i;
1872
1873 if (wpa_s->bssid_filter == NULL)
1874 return 1;
1875
1876 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
1877 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
1878 ETH_ALEN) == 0)
1879 return 1;
1880 }
1881
1882 return 0;
1883}
1884
1885
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001886void filter_scan_res(struct wpa_supplicant *wpa_s,
1887 struct wpa_scan_results *res)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001888{
1889 size_t i, j;
1890
1891 if (wpa_s->bssid_filter == NULL)
1892 return;
1893
1894 for (i = 0, j = 0; i < res->num; i++) {
1895 if (wpa_supplicant_filter_bssid_match(wpa_s,
1896 res->res[i]->bssid)) {
1897 res->res[j++] = res->res[i];
1898 } else {
1899 os_free(res->res[i]);
1900 res->res[i] = NULL;
1901 }
1902 }
1903
1904 if (res->num != j) {
1905 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
1906 (int) (res->num - j));
1907 res->num = j;
1908 }
1909}
1910
1911
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001912/*
1913 * Noise floor values to use when we have signal strength
Dmitry Shmidte4663042016-04-04 10:07:49 -07001914 * measurements, but no noise floor measurements. These values were
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001915 * measured in an office environment with many APs.
1916 */
1917#define DEFAULT_NOISE_FLOOR_2GHZ (-89)
1918#define DEFAULT_NOISE_FLOOR_5GHZ (-92)
1919
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001920void scan_snr(struct wpa_scan_res *res)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001921{
1922 if (res->flags & WPA_SCAN_NOISE_INVALID) {
1923 res->noise = IS_5GHZ(res->freq) ?
1924 DEFAULT_NOISE_FLOOR_5GHZ :
1925 DEFAULT_NOISE_FLOOR_2GHZ;
1926 }
1927
1928 if (res->flags & WPA_SCAN_LEVEL_DBM) {
1929 res->snr = res->level - res->noise;
1930 } else {
1931 /* Level is not in dBm, so we can't calculate
1932 * SNR. Just use raw level (units unknown). */
1933 res->snr = res->level;
1934 }
1935}
1936
1937
1938static unsigned int max_ht20_rate(int snr)
1939{
1940 if (snr < 6)
1941 return 6500; /* HT20 MCS0 */
1942 if (snr < 8)
1943 return 13000; /* HT20 MCS1 */
1944 if (snr < 13)
1945 return 19500; /* HT20 MCS2 */
1946 if (snr < 17)
1947 return 26000; /* HT20 MCS3 */
1948 if (snr < 20)
1949 return 39000; /* HT20 MCS4 */
1950 if (snr < 23)
1951 return 52000; /* HT20 MCS5 */
1952 if (snr < 24)
1953 return 58500; /* HT20 MCS6 */
1954 return 65000; /* HT20 MCS7 */
1955}
1956
1957
1958static unsigned int max_ht40_rate(int snr)
1959{
1960 if (snr < 3)
1961 return 13500; /* HT40 MCS0 */
1962 if (snr < 6)
1963 return 27000; /* HT40 MCS1 */
1964 if (snr < 10)
1965 return 40500; /* HT40 MCS2 */
1966 if (snr < 15)
1967 return 54000; /* HT40 MCS3 */
1968 if (snr < 17)
1969 return 81000; /* HT40 MCS4 */
1970 if (snr < 22)
1971 return 108000; /* HT40 MCS5 */
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001972 if (snr < 24)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001973 return 121500; /* HT40 MCS6 */
1974 return 135000; /* HT40 MCS7 */
1975}
1976
1977
1978static unsigned int max_vht80_rate(int snr)
1979{
1980 if (snr < 1)
1981 return 0;
1982 if (snr < 2)
1983 return 29300; /* VHT80 MCS0 */
1984 if (snr < 5)
1985 return 58500; /* VHT80 MCS1 */
1986 if (snr < 9)
1987 return 87800; /* VHT80 MCS2 */
1988 if (snr < 11)
1989 return 117000; /* VHT80 MCS3 */
1990 if (snr < 15)
1991 return 175500; /* VHT80 MCS4 */
1992 if (snr < 16)
1993 return 234000; /* VHT80 MCS5 */
1994 if (snr < 18)
1995 return 263300; /* VHT80 MCS6 */
1996 if (snr < 20)
1997 return 292500; /* VHT80 MCS7 */
1998 if (snr < 22)
1999 return 351000; /* VHT80 MCS8 */
2000 return 390000; /* VHT80 MCS9 */
2001}
2002
2003
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002004void scan_est_throughput(struct wpa_supplicant *wpa_s,
2005 struct wpa_scan_res *res)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002006{
2007 enum local_hw_capab capab = wpa_s->hw_capab;
2008 int rate; /* max legacy rate in 500 kb/s units */
2009 const u8 *ie;
2010 unsigned int est, tmp;
2011 int snr = res->snr;
2012
2013 if (res->est_throughput)
2014 return;
2015
2016 /* Get maximum legacy rate */
2017 rate = wpa_scan_get_max_rate(res);
2018
2019 /* Limit based on estimated SNR */
2020 if (rate > 1 * 2 && snr < 1)
2021 rate = 1 * 2;
2022 else if (rate > 2 * 2 && snr < 4)
2023 rate = 2 * 2;
2024 else if (rate > 6 * 2 && snr < 5)
2025 rate = 6 * 2;
2026 else if (rate > 9 * 2 && snr < 6)
2027 rate = 9 * 2;
2028 else if (rate > 12 * 2 && snr < 7)
2029 rate = 12 * 2;
2030 else if (rate > 18 * 2 && snr < 10)
2031 rate = 18 * 2;
2032 else if (rate > 24 * 2 && snr < 11)
2033 rate = 24 * 2;
2034 else if (rate > 36 * 2 && snr < 15)
2035 rate = 36 * 2;
2036 else if (rate > 48 * 2 && snr < 19)
2037 rate = 48 * 2;
2038 else if (rate > 54 * 2 && snr < 21)
2039 rate = 54 * 2;
2040 est = rate * 500;
2041
2042 if (capab == CAPAB_HT || capab == CAPAB_HT40 || capab == CAPAB_VHT) {
2043 ie = wpa_scan_get_ie(res, WLAN_EID_HT_CAP);
2044 if (ie) {
2045 tmp = max_ht20_rate(snr);
2046 if (tmp > est)
2047 est = tmp;
2048 }
2049 }
2050
2051 if (capab == CAPAB_HT40 || capab == CAPAB_VHT) {
2052 ie = wpa_scan_get_ie(res, WLAN_EID_HT_OPERATION);
2053 if (ie && ie[1] >= 2 &&
2054 (ie[3] & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
2055 tmp = max_ht40_rate(snr);
2056 if (tmp > est)
2057 est = tmp;
2058 }
2059 }
2060
2061 if (capab == CAPAB_VHT) {
2062 /* Use +1 to assume VHT is always faster than HT */
2063 ie = wpa_scan_get_ie(res, WLAN_EID_VHT_CAP);
2064 if (ie) {
2065 tmp = max_ht20_rate(snr) + 1;
2066 if (tmp > est)
2067 est = tmp;
2068
2069 ie = wpa_scan_get_ie(res, WLAN_EID_HT_OPERATION);
2070 if (ie && ie[1] >= 2 &&
2071 (ie[3] &
2072 HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
2073 tmp = max_ht40_rate(snr) + 1;
2074 if (tmp > est)
2075 est = tmp;
2076 }
2077
2078 ie = wpa_scan_get_ie(res, WLAN_EID_VHT_OPERATION);
2079 if (ie && ie[1] >= 1 &&
2080 (ie[2] & VHT_OPMODE_CHANNEL_WIDTH_MASK)) {
2081 tmp = max_vht80_rate(snr) + 1;
2082 if (tmp > est)
2083 est = tmp;
2084 }
2085 }
2086 }
2087
2088 /* TODO: channel utilization and AP load (e.g., from AP Beacon) */
2089
2090 res->est_throughput = est;
2091}
2092
2093
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002094/**
2095 * wpa_supplicant_get_scan_results - Get scan results
2096 * @wpa_s: Pointer to wpa_supplicant data
2097 * @info: Information about what was scanned or %NULL if not available
2098 * @new_scan: Whether a new scan was performed
2099 * Returns: Scan results, %NULL on failure
2100 *
2101 * This function request the current scan results from the driver and updates
2102 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
2103 * results with wpa_scan_results_free().
2104 */
2105struct wpa_scan_results *
2106wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
2107 struct scan_info *info, int new_scan)
2108{
2109 struct wpa_scan_results *scan_res;
2110 size_t i;
2111 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
2112
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002113 scan_res = wpa_drv_get_scan_results2(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002114 if (scan_res == NULL) {
2115 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
2116 return NULL;
2117 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002118 if (scan_res->fetch_time.sec == 0) {
2119 /*
2120 * Make sure we have a valid timestamp if the driver wrapper
2121 * does not set this.
2122 */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002123 os_get_reltime(&scan_res->fetch_time);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002124 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002125 filter_scan_res(wpa_s, scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002126
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002127 for (i = 0; i < scan_res->num; i++) {
2128 struct wpa_scan_res *scan_res_item = scan_res->res[i];
2129
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002130 scan_snr(scan_res_item);
2131 scan_est_throughput(wpa_s, scan_res_item);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002132 }
2133
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002134#ifdef CONFIG_WPS
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002135 if (wpas_wps_searching(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002136 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
2137 "provisioning rules");
2138 compar = wpa_scan_result_wps_compar;
2139 }
2140#endif /* CONFIG_WPS */
2141
2142 qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
2143 compar);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002144 dump_scan_res(scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002145
2146 wpa_bss_update_start(wpa_s);
2147 for (i = 0; i < scan_res->num; i++)
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002148 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
2149 &scan_res->fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002150 wpa_bss_update_end(wpa_s, info, new_scan);
2151
2152 return scan_res;
2153}
2154
2155
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002156/**
2157 * wpa_supplicant_update_scan_results - Update scan results from the driver
2158 * @wpa_s: Pointer to wpa_supplicant data
2159 * Returns: 0 on success, -1 on failure
2160 *
2161 * This function updates the BSS table within wpa_supplicant based on the
2162 * currently available scan results from the driver without requesting a new
2163 * scan. This is used in cases where the driver indicates an association
2164 * (including roaming within ESS) and wpa_supplicant does not yet have the
2165 * needed information to complete the connection (e.g., to perform validation
2166 * steps in 4-way handshake).
2167 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002168int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
2169{
2170 struct wpa_scan_results *scan_res;
2171 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
2172 if (scan_res == NULL)
2173 return -1;
2174 wpa_scan_results_free(scan_res);
2175
2176 return 0;
2177}
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002178
2179
2180/**
2181 * scan_only_handler - Reports scan results
2182 */
2183void scan_only_handler(struct wpa_supplicant *wpa_s,
2184 struct wpa_scan_results *scan_res)
2185{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002186 wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002187 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
2188 wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
2189 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
2190 wpa_s->manual_scan_id);
2191 wpa_s->manual_scan_use_id = 0;
2192 } else {
2193 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
2194 }
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002195 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002196 wpas_notify_scan_done(wpa_s, 1);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002197 if (wpa_s->scan_work) {
2198 struct wpa_radio_work *work = wpa_s->scan_work;
2199 wpa_s->scan_work = NULL;
2200 radio_work_done(work);
2201 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002202
2203 if (wpa_s->wpa_state == WPA_SCANNING)
2204 wpa_supplicant_set_state(wpa_s, wpa_s->scan_prev_wpa_state);
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002205}
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07002206
2207
2208int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
2209{
2210 return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
2211}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002212
2213
2214struct wpa_driver_scan_params *
2215wpa_scan_clone_params(const struct wpa_driver_scan_params *src)
2216{
2217 struct wpa_driver_scan_params *params;
2218 size_t i;
2219 u8 *n;
2220
2221 params = os_zalloc(sizeof(*params));
2222 if (params == NULL)
2223 return NULL;
2224
2225 for (i = 0; i < src->num_ssids; i++) {
2226 if (src->ssids[i].ssid) {
2227 n = os_malloc(src->ssids[i].ssid_len);
2228 if (n == NULL)
2229 goto failed;
2230 os_memcpy(n, src->ssids[i].ssid,
2231 src->ssids[i].ssid_len);
2232 params->ssids[i].ssid = n;
2233 params->ssids[i].ssid_len = src->ssids[i].ssid_len;
2234 }
2235 }
2236 params->num_ssids = src->num_ssids;
2237
2238 if (src->extra_ies) {
2239 n = os_malloc(src->extra_ies_len);
2240 if (n == NULL)
2241 goto failed;
2242 os_memcpy(n, src->extra_ies, src->extra_ies_len);
2243 params->extra_ies = n;
2244 params->extra_ies_len = src->extra_ies_len;
2245 }
2246
2247 if (src->freqs) {
2248 int len = int_array_len(src->freqs);
2249 params->freqs = os_malloc((len + 1) * sizeof(int));
2250 if (params->freqs == NULL)
2251 goto failed;
2252 os_memcpy(params->freqs, src->freqs, (len + 1) * sizeof(int));
2253 }
2254
2255 if (src->filter_ssids) {
Dmitry Shmidt97672262014-02-03 13:02:54 -08002256 params->filter_ssids = os_malloc(sizeof(*params->filter_ssids) *
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002257 src->num_filter_ssids);
2258 if (params->filter_ssids == NULL)
2259 goto failed;
2260 os_memcpy(params->filter_ssids, src->filter_ssids,
Dmitry Shmidt97672262014-02-03 13:02:54 -08002261 sizeof(*params->filter_ssids) *
2262 src->num_filter_ssids);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002263 params->num_filter_ssids = src->num_filter_ssids;
2264 }
2265
2266 params->filter_rssi = src->filter_rssi;
2267 params->p2p_probe = src->p2p_probe;
2268 params->only_new_results = src->only_new_results;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07002269 params->low_priority = src->low_priority;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002270
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002271 if (src->sched_scan_plans_num > 0) {
2272 params->sched_scan_plans =
2273 os_malloc(sizeof(*src->sched_scan_plans) *
2274 src->sched_scan_plans_num);
2275 if (!params->sched_scan_plans)
2276 goto failed;
2277
2278 os_memcpy(params->sched_scan_plans, src->sched_scan_plans,
2279 sizeof(*src->sched_scan_plans) *
2280 src->sched_scan_plans_num);
2281 params->sched_scan_plans_num = src->sched_scan_plans_num;
2282 }
2283
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002284 if (src->mac_addr_rand) {
2285 params->mac_addr_rand = src->mac_addr_rand;
2286
2287 if (src->mac_addr && src->mac_addr_mask) {
2288 u8 *mac_addr;
2289
2290 mac_addr = os_malloc(2 * ETH_ALEN);
2291 if (!mac_addr)
2292 goto failed;
2293
2294 os_memcpy(mac_addr, src->mac_addr, ETH_ALEN);
2295 os_memcpy(mac_addr + ETH_ALEN, src->mac_addr_mask,
2296 ETH_ALEN);
2297 params->mac_addr = mac_addr;
2298 params->mac_addr_mask = mac_addr + ETH_ALEN;
2299 }
2300 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002301
2302 if (src->bssid) {
2303 u8 *bssid;
2304
2305 bssid = os_malloc(ETH_ALEN);
2306 if (!bssid)
2307 goto failed;
2308 os_memcpy(bssid, src->bssid, ETH_ALEN);
2309 params->bssid = bssid;
2310 }
2311
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002312 return params;
2313
2314failed:
2315 wpa_scan_free_params(params);
2316 return NULL;
2317}
2318
2319
2320void wpa_scan_free_params(struct wpa_driver_scan_params *params)
2321{
2322 size_t i;
2323
2324 if (params == NULL)
2325 return;
2326
2327 for (i = 0; i < params->num_ssids; i++)
2328 os_free((u8 *) params->ssids[i].ssid);
2329 os_free((u8 *) params->extra_ies);
2330 os_free(params->freqs);
2331 os_free(params->filter_ssids);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002332 os_free(params->sched_scan_plans);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002333
2334 /*
2335 * Note: params->mac_addr_mask points to same memory allocation and
2336 * must not be freed separately.
2337 */
2338 os_free((u8 *) params->mac_addr);
2339
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002340 os_free((u8 *) params->bssid);
2341
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002342 os_free(params);
2343}
Dmitry Shmidt98660862014-03-11 17:26:21 -07002344
2345
2346int wpas_start_pno(struct wpa_supplicant *wpa_s)
2347{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002348 int ret, prio;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002349 size_t i, num_ssid, num_match_ssid;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002350 struct wpa_ssid *ssid;
2351 struct wpa_driver_scan_params params;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002352 struct sched_scan_plan scan_plan;
Dmitry Shmidte4663042016-04-04 10:07:49 -07002353 unsigned int max_sched_scan_ssids;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002354
2355 if (!wpa_s->sched_scan_supported)
2356 return -1;
2357
Dmitry Shmidte4663042016-04-04 10:07:49 -07002358 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
2359 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
2360 else
2361 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
2362 if (max_sched_scan_ssids < 1)
2363 return -1;
2364
Dmitry Shmidt98660862014-03-11 17:26:21 -07002365 if (wpa_s->pno || wpa_s->pno_sched_pending)
2366 return 0;
2367
2368 if ((wpa_s->wpa_state > WPA_SCANNING) &&
2369 (wpa_s->wpa_state <= WPA_COMPLETED)) {
2370 wpa_printf(MSG_ERROR, "PNO: In assoc process");
2371 return -EAGAIN;
2372 }
2373
2374 if (wpa_s->wpa_state == WPA_SCANNING) {
2375 wpa_supplicant_cancel_scan(wpa_s);
2376 if (wpa_s->sched_scanning) {
2377 wpa_printf(MSG_DEBUG, "Schedule PNO on completion of "
2378 "ongoing sched scan");
2379 wpa_supplicant_cancel_sched_scan(wpa_s);
2380 wpa_s->pno_sched_pending = 1;
2381 return 0;
2382 }
2383 }
2384
2385 os_memset(&params, 0, sizeof(params));
2386
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002387 num_ssid = num_match_ssid = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002388 ssid = wpa_s->conf->ssid;
2389 while (ssid) {
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002390 if (!wpas_network_disabled(wpa_s, ssid)) {
2391 num_match_ssid++;
2392 if (ssid->scan_ssid)
2393 num_ssid++;
2394 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07002395 ssid = ssid->next;
2396 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002397
2398 if (num_match_ssid == 0) {
2399 wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
2400 return -1;
2401 }
2402
2403 if (num_match_ssid > num_ssid) {
2404 params.num_ssids++; /* wildcard */
2405 num_ssid++;
2406 }
2407
Dmitry Shmidte4663042016-04-04 10:07:49 -07002408 if (num_ssid > max_sched_scan_ssids) {
Dmitry Shmidt98660862014-03-11 17:26:21 -07002409 wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
Dmitry Shmidte4663042016-04-04 10:07:49 -07002410 "%u", max_sched_scan_ssids, (unsigned int) num_ssid);
2411 num_ssid = max_sched_scan_ssids;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002412 }
2413
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002414 if (num_match_ssid > wpa_s->max_match_sets) {
2415 num_match_ssid = wpa_s->max_match_sets;
2416 wpa_dbg(wpa_s, MSG_DEBUG, "PNO: Too many SSIDs to match");
Dmitry Shmidt98660862014-03-11 17:26:21 -07002417 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002418 params.filter_ssids = os_calloc(num_match_ssid,
2419 sizeof(struct wpa_driver_scan_filter));
Dmitry Shmidt98660862014-03-11 17:26:21 -07002420 if (params.filter_ssids == NULL)
2421 return -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002422
Dmitry Shmidt98660862014-03-11 17:26:21 -07002423 i = 0;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002424 prio = 0;
2425 ssid = wpa_s->conf->pssid[prio];
Dmitry Shmidt98660862014-03-11 17:26:21 -07002426 while (ssid) {
2427 if (!wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002428 if (ssid->scan_ssid && params.num_ssids < num_ssid) {
2429 params.ssids[params.num_ssids].ssid =
2430 ssid->ssid;
2431 params.ssids[params.num_ssids].ssid_len =
2432 ssid->ssid_len;
2433 params.num_ssids++;
2434 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07002435 os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
2436 ssid->ssid_len);
2437 params.filter_ssids[i].ssid_len = ssid->ssid_len;
2438 params.num_filter_ssids++;
2439 i++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002440 if (i == num_match_ssid)
Dmitry Shmidt98660862014-03-11 17:26:21 -07002441 break;
2442 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002443 if (ssid->pnext)
2444 ssid = ssid->pnext;
2445 else if (prio + 1 == wpa_s->conf->num_prio)
2446 break;
2447 else
2448 ssid = wpa_s->conf->pssid[++prio];
Dmitry Shmidt98660862014-03-11 17:26:21 -07002449 }
2450
2451 if (wpa_s->conf->filter_rssi)
2452 params.filter_rssi = wpa_s->conf->filter_rssi;
2453
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002454 if (wpa_s->sched_scan_plans_num) {
2455 params.sched_scan_plans = wpa_s->sched_scan_plans;
2456 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
2457 } else {
2458 /* Set one scan plan that will run infinitely */
2459 if (wpa_s->conf->sched_scan_interval)
2460 scan_plan.interval = wpa_s->conf->sched_scan_interval;
2461 else
2462 scan_plan.interval = 10;
2463
2464 scan_plan.iterations = 0;
2465 params.sched_scan_plans = &scan_plan;
2466 params.sched_scan_plans_num = 1;
2467 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07002468
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002469 if (params.freqs == NULL && wpa_s->manual_sched_scan_freqs) {
2470 wpa_dbg(wpa_s, MSG_DEBUG, "Limit sched scan to specified channels");
2471 params.freqs = wpa_s->manual_sched_scan_freqs;
2472 }
2473
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002474 if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_PNO) {
2475 params.mac_addr_rand = 1;
2476 if (wpa_s->mac_addr_pno) {
2477 params.mac_addr = wpa_s->mac_addr_pno;
2478 params.mac_addr_mask = wpa_s->mac_addr_pno + ETH_ALEN;
2479 }
2480 }
2481
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002482 ret = wpa_supplicant_start_sched_scan(wpa_s, &params);
Dmitry Shmidt98660862014-03-11 17:26:21 -07002483 os_free(params.filter_ssids);
2484 if (ret == 0)
2485 wpa_s->pno = 1;
2486 else
2487 wpa_msg(wpa_s, MSG_ERROR, "Failed to schedule PNO");
2488 return ret;
2489}
2490
2491
2492int wpas_stop_pno(struct wpa_supplicant *wpa_s)
2493{
2494 int ret = 0;
2495
2496 if (!wpa_s->pno)
2497 return 0;
2498
2499 ret = wpa_supplicant_stop_sched_scan(wpa_s);
2500
2501 wpa_s->pno = 0;
2502 wpa_s->pno_sched_pending = 0;
2503
2504 if (wpa_s->wpa_state == WPA_SCANNING)
2505 wpa_supplicant_req_scan(wpa_s, 0, 0);
2506
2507 return ret;
2508}
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002509
2510
2511void wpas_mac_addr_rand_scan_clear(struct wpa_supplicant *wpa_s,
2512 unsigned int type)
2513{
2514 type &= MAC_ADDR_RAND_ALL;
2515 wpa_s->mac_addr_rand_enable &= ~type;
2516
2517 if (type & MAC_ADDR_RAND_SCAN) {
2518 os_free(wpa_s->mac_addr_scan);
2519 wpa_s->mac_addr_scan = NULL;
2520 }
2521
2522 if (type & MAC_ADDR_RAND_SCHED_SCAN) {
2523 os_free(wpa_s->mac_addr_sched_scan);
2524 wpa_s->mac_addr_sched_scan = NULL;
2525 }
2526
2527 if (type & MAC_ADDR_RAND_PNO) {
2528 os_free(wpa_s->mac_addr_pno);
2529 wpa_s->mac_addr_pno = NULL;
2530 }
2531}
2532
2533
2534int wpas_mac_addr_rand_scan_set(struct wpa_supplicant *wpa_s,
2535 unsigned int type, const u8 *addr,
2536 const u8 *mask)
2537{
2538 u8 *tmp = NULL;
2539
2540 wpas_mac_addr_rand_scan_clear(wpa_s, type);
2541
2542 if (addr) {
2543 tmp = os_malloc(2 * ETH_ALEN);
2544 if (!tmp)
2545 return -1;
2546 os_memcpy(tmp, addr, ETH_ALEN);
2547 os_memcpy(tmp + ETH_ALEN, mask, ETH_ALEN);
2548 }
2549
2550 if (type == MAC_ADDR_RAND_SCAN) {
2551 wpa_s->mac_addr_scan = tmp;
2552 } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
2553 wpa_s->mac_addr_sched_scan = tmp;
2554 } else if (type == MAC_ADDR_RAND_PNO) {
2555 wpa_s->mac_addr_pno = tmp;
2556 } else {
2557 wpa_printf(MSG_INFO,
2558 "scan: Invalid MAC randomization type=0x%x",
2559 type);
2560 os_free(tmp);
2561 return -1;
2562 }
2563
2564 wpa_s->mac_addr_rand_enable |= type;
2565 return 0;
2566}
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002567
2568
2569int wpas_abort_ongoing_scan(struct wpa_supplicant *wpa_s)
2570{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002571 int scan_work = !!wpa_s->scan_work;
2572
2573#ifdef CONFIG_P2P
2574 scan_work |= !!wpa_s->p2p_scan_work;
2575#endif /* CONFIG_P2P */
2576
2577 if (scan_work && wpa_s->own_scan_running) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002578 wpa_dbg(wpa_s, MSG_DEBUG, "Abort an ongoing scan");
2579 return wpa_drv_abort_scan(wpa_s);
2580 }
2581
2582 return 0;
2583}
2584
2585
2586int wpas_sched_scan_plans_set(struct wpa_supplicant *wpa_s, const char *cmd)
2587{
2588 struct sched_scan_plan *scan_plans = NULL;
2589 const char *token, *context = NULL;
2590 unsigned int num = 0;
2591
2592 if (!cmd)
2593 return -1;
2594
2595 if (!cmd[0]) {
2596 wpa_printf(MSG_DEBUG, "Clear sched scan plans");
2597 os_free(wpa_s->sched_scan_plans);
2598 wpa_s->sched_scan_plans = NULL;
2599 wpa_s->sched_scan_plans_num = 0;
2600 return 0;
2601 }
2602
2603 while ((token = cstr_token(cmd, " ", &context))) {
2604 int ret;
2605 struct sched_scan_plan *scan_plan, *n;
2606
2607 n = os_realloc_array(scan_plans, num + 1, sizeof(*scan_plans));
2608 if (!n)
2609 goto fail;
2610
2611 scan_plans = n;
2612 scan_plan = &scan_plans[num];
2613 num++;
2614
2615 ret = sscanf(token, "%u:%u", &scan_plan->interval,
2616 &scan_plan->iterations);
2617 if (ret <= 0 || ret > 2 || !scan_plan->interval) {
2618 wpa_printf(MSG_ERROR,
2619 "Invalid sched scan plan input: %s", token);
2620 goto fail;
2621 }
2622
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002623 if (scan_plan->interval > wpa_s->max_sched_scan_plan_interval) {
2624 wpa_printf(MSG_WARNING,
2625 "scan plan %u: Scan interval too long(%u), use the maximum allowed(%u)",
2626 num, scan_plan->interval,
2627 wpa_s->max_sched_scan_plan_interval);
2628 scan_plan->interval =
2629 wpa_s->max_sched_scan_plan_interval;
2630 }
2631
2632 if (ret == 1) {
2633 scan_plan->iterations = 0;
2634 break;
2635 }
2636
2637 if (!scan_plan->iterations) {
2638 wpa_printf(MSG_ERROR,
2639 "scan plan %u: Number of iterations cannot be zero",
2640 num);
2641 goto fail;
2642 }
2643
2644 if (scan_plan->iterations >
2645 wpa_s->max_sched_scan_plan_iterations) {
2646 wpa_printf(MSG_WARNING,
2647 "scan plan %u: Too many iterations(%u), use the maximum allowed(%u)",
2648 num, scan_plan->iterations,
2649 wpa_s->max_sched_scan_plan_iterations);
2650 scan_plan->iterations =
2651 wpa_s->max_sched_scan_plan_iterations;
2652 }
2653
2654 wpa_printf(MSG_DEBUG,
2655 "scan plan %u: interval=%u iterations=%u",
2656 num, scan_plan->interval, scan_plan->iterations);
2657 }
2658
2659 if (!scan_plans) {
2660 wpa_printf(MSG_ERROR, "Invalid scan plans entry");
2661 goto fail;
2662 }
2663
2664 if (cstr_token(cmd, " ", &context) || scan_plans[num - 1].iterations) {
2665 wpa_printf(MSG_ERROR,
2666 "All scan plans but the last must specify a number of iterations");
2667 goto fail;
2668 }
2669
2670 wpa_printf(MSG_DEBUG, "scan plan %u (last plan): interval=%u",
2671 num, scan_plans[num - 1].interval);
2672
2673 if (num > wpa_s->max_sched_scan_plans) {
2674 wpa_printf(MSG_WARNING,
2675 "Too many scheduled scan plans (only %u supported)",
2676 wpa_s->max_sched_scan_plans);
2677 wpa_printf(MSG_WARNING,
2678 "Use only the first %u scan plans, and the last one (in infinite loop)",
2679 wpa_s->max_sched_scan_plans - 1);
2680 os_memcpy(&scan_plans[wpa_s->max_sched_scan_plans - 1],
2681 &scan_plans[num - 1], sizeof(*scan_plans));
2682 num = wpa_s->max_sched_scan_plans;
2683 }
2684
2685 os_free(wpa_s->sched_scan_plans);
2686 wpa_s->sched_scan_plans = scan_plans;
2687 wpa_s->sched_scan_plans_num = num;
2688
2689 return 0;
2690
2691fail:
2692 os_free(scan_plans);
2693 wpa_printf(MSG_ERROR, "invalid scan plans list");
2694 return -1;
2695}