blob: 172772de6f4e370cd3650584c416ff351634ae47 [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 Shmidt7f2c7532016-08-15 09:48:12 -0700429void wpa_supplicant_set_default_scan_ies(struct wpa_supplicant *wpa_s)
430{
431 struct wpabuf *default_ies = NULL;
432 u8 ext_capab[18];
433 int ext_capab_len;
434 enum wpa_driver_if_type type = WPA_IF_STATION;
435
436#ifdef CONFIG_P2P
437 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT)
438 type = WPA_IF_P2P_CLIENT;
439#endif /* CONFIG_P2P */
440
441 wpa_drv_get_ext_capa(wpa_s, type);
442
443 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
444 sizeof(ext_capab));
445 if (ext_capab_len > 0 &&
446 wpabuf_resize(&default_ies, ext_capab_len) == 0)
447 wpabuf_put_data(default_ies, ext_capab, ext_capab_len);
448
449#ifdef CONFIG_MBO
450 /* Send cellular capabilities for potential MBO STAs */
451 if (wpabuf_resize(&default_ies, 9) == 0)
452 wpas_mbo_scan_ie(wpa_s, default_ies);
453#endif /* CONFIG_MBO */
454
455 if (default_ies)
456 wpa_drv_set_default_scan_ies(wpa_s, wpabuf_head(default_ies),
457 wpabuf_len(default_ies));
458 wpabuf_free(default_ies);
459}
460
461
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700462static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800463{
464 struct wpabuf *extra_ie = NULL;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700465 u8 ext_capab[18];
466 int ext_capab_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800467#ifdef CONFIG_WPS
468 int wps = 0;
469 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
470#endif /* CONFIG_WPS */
471
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700472#ifdef CONFIG_P2P
473 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT)
474 wpa_drv_get_ext_capa(wpa_s, WPA_IF_P2P_CLIENT);
475 else
476#endif /* CONFIG_P2P */
477 wpa_drv_get_ext_capa(wpa_s, WPA_IF_STATION);
478
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700479 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
480 sizeof(ext_capab));
481 if (ext_capab_len > 0 &&
482 wpabuf_resize(&extra_ie, ext_capab_len) == 0)
483 wpabuf_put_data(extra_ie, ext_capab, ext_capab_len);
484
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800485#ifdef CONFIG_INTERWORKING
486 if (wpa_s->conf->interworking &&
487 wpabuf_resize(&extra_ie, 100) == 0)
488 wpas_add_interworking_elements(wpa_s, extra_ie);
489#endif /* CONFIG_INTERWORKING */
490
491#ifdef CONFIG_WPS
492 wps = wpas_wps_in_use(wpa_s, &req_type);
493
494 if (wps) {
495 struct wpabuf *wps_ie;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700496 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
497 DEV_PW_DEFAULT,
498 &wpa_s->wps->dev,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800499 wpa_s->wps->uuid, req_type,
500 0, NULL);
501 if (wps_ie) {
502 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
503 wpabuf_put_buf(extra_ie, wps_ie);
504 wpabuf_free(wps_ie);
505 }
506 }
507
508#ifdef CONFIG_P2P
509 if (wps) {
510 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
511 if (wpabuf_resize(&extra_ie, ielen) == 0)
512 wpas_p2p_scan_ie(wpa_s, extra_ie);
513 }
514#endif /* CONFIG_P2P */
515
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800516 wpa_supplicant_mesh_add_scan_ie(wpa_s, &extra_ie);
517
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800518#endif /* CONFIG_WPS */
519
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700520#ifdef CONFIG_HS20
521 if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 7) == 0)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800522 wpas_hs20_add_indication(extra_ie, -1);
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700523#endif /* CONFIG_HS20 */
524
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800525#ifdef CONFIG_FST
526 if (wpa_s->fst_ies &&
527 wpabuf_resize(&extra_ie, wpabuf_len(wpa_s->fst_ies)) == 0)
528 wpabuf_put_buf(extra_ie, wpa_s->fst_ies);
529#endif /* CONFIG_FST */
530
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800531#ifdef CONFIG_MBO
532 /* Send cellular capabilities for potential MBO STAs */
533 if (wpabuf_resize(&extra_ie, 9) == 0)
534 wpas_mbo_scan_ie(wpa_s, extra_ie);
535#endif /* CONFIG_MBO */
536
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700537 if (wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ]) {
538 struct wpabuf *buf = wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ];
539
540 if (wpabuf_resize(&extra_ie, wpabuf_len(buf)) == 0)
541 wpabuf_put_buf(extra_ie, buf);
542 }
543
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800544 return extra_ie;
545}
546
547
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800548#ifdef CONFIG_P2P
549
550/*
551 * Check whether there are any enabled networks or credentials that could be
552 * used for a non-P2P connection.
553 */
554static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
555{
556 struct wpa_ssid *ssid;
557
558 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
559 if (wpas_network_disabled(wpa_s, ssid))
560 continue;
561 if (!ssid->p2p_group)
562 return 1;
563 }
564
565 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
566 wpa_s->conf->auto_interworking)
567 return 1;
568
569 return 0;
570}
571
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700572#endif /* CONFIG_P2P */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800573
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800574
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700575static void wpa_setband_scan_freqs_list(struct wpa_supplicant *wpa_s,
576 enum hostapd_hw_mode band,
577 struct wpa_driver_scan_params *params)
578{
579 /* Include only supported channels for the specified band */
580 struct hostapd_hw_modes *mode;
581 int count, i;
582
583 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band);
584 if (mode == NULL) {
585 /* No channels supported in this band - use empty list */
586 params->freqs = os_zalloc(sizeof(int));
587 return;
588 }
589
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800590 params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700591 if (params->freqs == NULL)
592 return;
593 for (count = 0, i = 0; i < mode->num_channels; i++) {
594 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
595 continue;
596 params->freqs[count++] = mode->channels[i].freq;
597 }
598}
599
600
601static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
602 struct wpa_driver_scan_params *params)
603{
604 if (wpa_s->hw.modes == NULL)
605 return; /* unknown what channels the driver supports */
606 if (params->freqs)
607 return; /* already using a limited channel set */
608 if (wpa_s->setband == WPA_SETBAND_5G)
609 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
610 params);
611 else if (wpa_s->setband == WPA_SETBAND_2G)
612 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G,
613 params);
614}
615
616
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700617static void wpa_set_scan_ssids(struct wpa_supplicant *wpa_s,
618 struct wpa_driver_scan_params *params,
619 size_t max_ssids)
620{
621 unsigned int i;
622 struct wpa_ssid *ssid;
623
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700624 /*
625 * For devices with max_ssids greater than 1, leave the last slot empty
626 * for adding the wildcard scan entry.
627 */
628 max_ssids = max_ssids > 1 ? max_ssids - 1 : max_ssids;
629
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700630 for (i = 0; i < wpa_s->scan_id_count; i++) {
631 unsigned int j;
632
633 ssid = wpa_config_get_network(wpa_s->conf, wpa_s->scan_id[i]);
634 if (!ssid || !ssid->scan_ssid)
635 continue;
636
637 for (j = 0; j < params->num_ssids; j++) {
638 if (params->ssids[j].ssid_len == ssid->ssid_len &&
639 params->ssids[j].ssid &&
640 os_memcmp(params->ssids[j].ssid, ssid->ssid,
641 ssid->ssid_len) == 0)
642 break;
643 }
644 if (j < params->num_ssids)
645 continue; /* already in the list */
646
647 if (params->num_ssids + 1 > max_ssids) {
648 wpa_printf(MSG_DEBUG,
649 "Over max scan SSIDs for manual request");
650 break;
651 }
652
653 wpa_printf(MSG_DEBUG, "Scan SSID (manual request): %s",
654 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
655 params->ssids[params->num_ssids].ssid = ssid->ssid;
656 params->ssids[params->num_ssids].ssid_len = ssid->ssid_len;
657 params->num_ssids++;
658 }
659
660 wpa_s->scan_id_count = 0;
661}
662
663
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700664static int wpa_set_ssids_from_scan_req(struct wpa_supplicant *wpa_s,
665 struct wpa_driver_scan_params *params,
666 size_t max_ssids)
667{
668 unsigned int i;
669
670 if (wpa_s->ssids_from_scan_req == NULL ||
671 wpa_s->num_ssids_from_scan_req == 0)
672 return 0;
673
674 if (wpa_s->num_ssids_from_scan_req > max_ssids) {
675 wpa_s->num_ssids_from_scan_req = max_ssids;
676 wpa_printf(MSG_DEBUG, "Over max scan SSIDs from scan req: %u",
677 (unsigned int) max_ssids);
678 }
679
680 for (i = 0; i < wpa_s->num_ssids_from_scan_req; i++) {
681 params->ssids[i].ssid = wpa_s->ssids_from_scan_req[i].ssid;
682 params->ssids[i].ssid_len =
683 wpa_s->ssids_from_scan_req[i].ssid_len;
684 wpa_hexdump_ascii(MSG_DEBUG, "specific SSID",
685 params->ssids[i].ssid,
686 params->ssids[i].ssid_len);
687 }
688
689 params->num_ssids = wpa_s->num_ssids_from_scan_req;
690 wpa_s->num_ssids_from_scan_req = 0;
691 return 1;
692}
693
694
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700695static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
696{
697 struct wpa_supplicant *wpa_s = eloop_ctx;
698 struct wpa_ssid *ssid;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800699 int ret, p2p_in_prog;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700700 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700701 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700702 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700703 size_t max_ssids;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800704 int connect_without_scan = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700705
Dmitry Shmidt18463232014-01-24 12:29:41 -0800706 if (wpa_s->pno || wpa_s->pno_sched_pending) {
707 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - PNO is in progress");
708 return;
709 }
710
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700711 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
712 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
713 return;
714 }
715
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800716 if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700717 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700718 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
719 return;
720 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800721
Dmitry Shmidt5887a9d2012-09-14 10:47:43 -0700722 if (wpa_s->scanning) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800723 /*
724 * If we are already in scanning state, we shall reschedule the
725 * the incoming scan request.
726 */
727 wpa_dbg(wpa_s, MSG_DEBUG, "Already scanning - Reschedule the incoming scan req");
728 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidt5887a9d2012-09-14 10:47:43 -0700729 return;
730 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800731
Dmitry Shmidt04949592012-07-19 12:16:46 -0700732 if (!wpa_supplicant_enabled_networks(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800733 wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700734 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
735 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
736 return;
737 }
738
739 if (wpa_s->conf->ap_scan != 0 &&
740 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
741 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
742 "overriding ap_scan configuration");
743 wpa_s->conf->ap_scan = 0;
744 wpas_notify_ap_scan_changed(wpa_s);
745 }
746
747 if (wpa_s->conf->ap_scan == 0) {
748 wpa_supplicant_gen_assoc_event(wpa_s);
749 return;
750 }
751
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800752 ssid = NULL;
753 if (wpa_s->scan_req != MANUAL_SCAN_REQ &&
754 wpa_s->connect_without_scan) {
755 connect_without_scan = 1;
756 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
757 if (ssid == wpa_s->connect_without_scan)
758 break;
759 }
760 }
761
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800762 p2p_in_prog = wpas_p2p_in_progress(wpa_s);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800763 if (p2p_in_prog && p2p_in_prog != 2 &&
764 (!ssid ||
765 (ssid->mode != WPAS_MODE_AP && ssid->mode != WPAS_MODE_P2P_GO))) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800766 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan while P2P operation is in progress");
767 wpa_supplicant_req_scan(wpa_s, 5, 0);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700768 return;
769 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700770
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800771 if (wpa_s->conf->ap_scan == 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700772 max_ssids = 1;
773 else {
774 max_ssids = wpa_s->max_scan_ssids;
775 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
776 max_ssids = WPAS_MAX_SCAN_SSIDS;
777 }
778
Dmitry Shmidt01904cf2013-12-05 11:08:35 -0800779 wpa_s->last_scan_req = wpa_s->scan_req;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800780 wpa_s->scan_req = NORMAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700781
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800782 if (connect_without_scan) {
783 wpa_s->connect_without_scan = NULL;
784 if (ssid) {
785 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
786 "without scan step");
787 wpa_supplicant_associate(wpa_s, NULL, ssid);
788 return;
789 }
790 }
791
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700792 os_memset(&params, 0, sizeof(params));
793
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800794 wpa_s->scan_prev_wpa_state = wpa_s->wpa_state;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700795 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
796 wpa_s->wpa_state == WPA_INACTIVE)
797 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
798
Dmitry Shmidt04949592012-07-19 12:16:46 -0700799 /*
800 * If autoscan has set its own scanning parameters
801 */
802 if (wpa_s->autoscan_params != NULL) {
803 scan_params = wpa_s->autoscan_params;
804 goto scan;
805 }
806
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700807 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
808 wpa_set_ssids_from_scan_req(wpa_s, &params, max_ssids)) {
809 wpa_printf(MSG_DEBUG, "Use specific SSIDs from SCAN command");
810 goto ssid_list_set;
811 }
812
Dmitry Shmidt04949592012-07-19 12:16:46 -0700813#ifdef CONFIG_P2P
814 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800815 wpa_s->go_params && !wpa_s->conf->passive_scan) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800816 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
817 wpa_s->p2p_in_provisioning,
818 wpa_s->show_group_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700819 params.ssids[0].ssid = wpa_s->go_params->ssid;
820 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
821 params.num_ssids = 1;
822 goto ssid_list_set;
823 }
Dmitry Shmidt15907092014-03-25 10:42:57 -0700824
825 if (wpa_s->p2p_in_invitation) {
826 if (wpa_s->current_ssid) {
827 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during invitation");
828 params.ssids[0].ssid = wpa_s->current_ssid->ssid;
829 params.ssids[0].ssid_len =
830 wpa_s->current_ssid->ssid_len;
831 params.num_ssids = 1;
832 } else {
833 wpa_printf(MSG_DEBUG, "P2P: No specific SSID known for scan during invitation");
834 }
835 goto ssid_list_set;
836 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700837#endif /* CONFIG_P2P */
838
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700839 /* Find the starting point from which to continue scanning */
840 ssid = wpa_s->conf->ssid;
841 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
842 while (ssid) {
843 if (ssid == wpa_s->prev_scan_ssid) {
844 ssid = ssid->next;
845 break;
846 }
847 ssid = ssid->next;
848 }
849 }
850
Dmitry Shmidt01904cf2013-12-05 11:08:35 -0800851 if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800852#ifdef CONFIG_AP
853 !wpa_s->ap_iface &&
854#endif /* CONFIG_AP */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -0800855 wpa_s->conf->ap_scan == 2) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700856 wpa_s->connect_without_scan = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800857 wpa_s->prev_scan_wildcard = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700858 wpa_supplicant_assoc_try(wpa_s, ssid);
859 return;
860 } else if (wpa_s->conf->ap_scan == 2) {
861 /*
862 * User-initiated scan request in ap_scan == 2; scan with
863 * wildcard SSID.
864 */
865 ssid = NULL;
Dmitry Shmidt98660862014-03-11 17:26:21 -0700866 } else if (wpa_s->reattach && wpa_s->current_ssid != NULL) {
867 /*
868 * Perform single-channel single-SSID scan for
869 * reassociate-to-same-BSS operation.
870 */
871 /* Setup SSID */
872 ssid = wpa_s->current_ssid;
873 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
874 ssid->ssid, ssid->ssid_len);
875 params.ssids[0].ssid = ssid->ssid;
876 params.ssids[0].ssid_len = ssid->ssid_len;
877 params.num_ssids = 1;
878
879 /*
880 * Allocate memory for frequency array, allocate one extra
881 * slot for the zero-terminator.
882 */
883 params.freqs = os_malloc(sizeof(int) * 2);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700884 if (params.freqs) {
885 params.freqs[0] = wpa_s->assoc_freq;
886 params.freqs[1] = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -0700887 }
Dmitry Shmidt98660862014-03-11 17:26:21 -0700888
889 /*
890 * Reset the reattach flag so that we fall back to full scan if
891 * this scan fails.
892 */
893 wpa_s->reattach = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700894 } else {
895 struct wpa_ssid *start = ssid, *tssid;
896 int freqs_set = 0;
897 if (ssid == NULL && max_ssids > 1)
898 ssid = wpa_s->conf->ssid;
899 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700900 if (!wpas_network_disabled(wpa_s, ssid) &&
901 ssid->scan_ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700902 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
903 ssid->ssid, ssid->ssid_len);
904 params.ssids[params.num_ssids].ssid =
905 ssid->ssid;
906 params.ssids[params.num_ssids].ssid_len =
907 ssid->ssid_len;
908 params.num_ssids++;
909 if (params.num_ssids + 1 >= max_ssids)
910 break;
911 }
912 ssid = ssid->next;
913 if (ssid == start)
914 break;
915 if (ssid == NULL && max_ssids > 1 &&
916 start != wpa_s->conf->ssid)
917 ssid = wpa_s->conf->ssid;
918 }
919
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700920 if (wpa_s->scan_id_count &&
921 wpa_s->last_scan_req == MANUAL_SCAN_REQ)
922 wpa_set_scan_ssids(wpa_s, &params, max_ssids);
923
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800924 for (tssid = wpa_s->conf->ssid;
925 wpa_s->last_scan_req != MANUAL_SCAN_REQ && tssid;
926 tssid = tssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700927 if (wpas_network_disabled(wpa_s, tssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700928 continue;
929 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
930 int_array_concat(&params.freqs,
931 tssid->scan_freq);
932 } else {
933 os_free(params.freqs);
934 params.freqs = NULL;
935 }
936 freqs_set = 1;
937 }
938 int_array_sort_unique(params.freqs);
939 }
940
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800941 if (ssid && max_ssids == 1) {
942 /*
943 * If the driver is limited to 1 SSID at a time interleave
944 * wildcard SSID scans with specific SSID scans to avoid
945 * waiting a long time for a wildcard scan.
946 */
947 if (!wpa_s->prev_scan_wildcard) {
948 params.ssids[0].ssid = NULL;
949 params.ssids[0].ssid_len = 0;
950 wpa_s->prev_scan_wildcard = 1;
951 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
952 "wildcard SSID (Interleave with specific)");
953 } else {
954 wpa_s->prev_scan_ssid = ssid;
955 wpa_s->prev_scan_wildcard = 0;
956 wpa_dbg(wpa_s, MSG_DEBUG,
957 "Starting AP scan for specific SSID: %s",
958 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700959 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800960 } else if (ssid) {
961 /* max_ssids > 1 */
962
963 wpa_s->prev_scan_ssid = ssid;
964 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
965 "the scan request");
966 params.num_ssids++;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800967 } else if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
968 wpa_s->manual_scan_passive && params.num_ssids == 0) {
969 wpa_dbg(wpa_s, MSG_DEBUG, "Use passive scan based on manual request");
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800970 } else if (wpa_s->conf->passive_scan) {
971 wpa_dbg(wpa_s, MSG_DEBUG,
972 "Use passive scan based on configuration");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700973 } else {
974 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
975 params.num_ssids++;
976 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
977 "SSID");
978 }
979
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700980ssid_list_set:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800981 wpa_supplicant_optimize_freqs(wpa_s, &params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700982 extra_ie = wpa_supplicant_extra_ies(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700983
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800984 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800985 wpa_s->manual_scan_only_new) {
986 wpa_printf(MSG_DEBUG,
987 "Request driver to clear scan cache due to manual only_new=1 scan");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800988 params.only_new_results = 1;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800989 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800990
991 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs == NULL &&
992 wpa_s->manual_scan_freqs) {
993 wpa_dbg(wpa_s, MSG_DEBUG, "Limit manual scan to specified channels");
994 params.freqs = wpa_s->manual_scan_freqs;
995 wpa_s->manual_scan_freqs = NULL;
996 }
997
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700998 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
999 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
1000 "generated frequency list");
1001 params.freqs = wpa_s->next_scan_freqs;
1002 } else
1003 os_free(wpa_s->next_scan_freqs);
1004 wpa_s->next_scan_freqs = NULL;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001005 wpa_setband_scan_freqs(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001006
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001007 /* See if user specified frequencies. If so, scan only those. */
1008 if (wpa_s->conf->freq_list && !params.freqs) {
1009 wpa_dbg(wpa_s, MSG_DEBUG,
1010 "Optimize scan based on conf->freq_list");
1011 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1012 }
1013
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001014 /* Use current associated channel? */
1015 if (wpa_s->conf->scan_cur_freq && !params.freqs) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001016 unsigned int num = wpa_s->num_multichan_concurrent;
1017
1018 params.freqs = os_calloc(num + 1, sizeof(int));
1019 if (params.freqs) {
1020 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
1021 if (num > 0) {
1022 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
1023 "current operating channels since "
1024 "scan_cur_freq is enabled");
1025 } else {
1026 os_free(params.freqs);
1027 params.freqs = NULL;
1028 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001029 }
1030 }
1031
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001032 params.filter_ssids = wpa_supplicant_build_filter_ssids(
1033 wpa_s->conf, &params.num_filter_ssids);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001034 if (extra_ie) {
1035 params.extra_ies = wpabuf_head(extra_ie);
1036 params.extra_ies_len = wpabuf_len(extra_ie);
1037 }
1038
1039#ifdef CONFIG_P2P
Dmitry Shmidt413dde72014-04-11 10:23:22 -07001040 if (wpa_s->p2p_in_provisioning || wpa_s->p2p_in_invitation ||
Dmitry Shmidt04949592012-07-19 12:16:46 -07001041 (wpa_s->show_group_started && wpa_s->go_params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001042 /*
1043 * The interface may not yet be in P2P mode, so we have to
1044 * explicitly request P2P probe to disable CCK rates.
1045 */
1046 params.p2p_probe = 1;
1047 }
1048#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001049
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001050 if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) {
1051 params.mac_addr_rand = 1;
1052 if (wpa_s->mac_addr_scan) {
1053 params.mac_addr = wpa_s->mac_addr_scan;
1054 params.mac_addr_mask = wpa_s->mac_addr_scan + ETH_ALEN;
1055 }
1056 }
1057
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001058 if (!is_zero_ether_addr(wpa_s->next_scan_bssid)) {
1059 struct wpa_bss *bss;
1060
1061 params.bssid = wpa_s->next_scan_bssid;
1062 bss = wpa_bss_get_bssid_latest(wpa_s, params.bssid);
1063 if (bss && bss->ssid_len && params.num_ssids == 1 &&
1064 params.ssids[0].ssid_len == 0) {
1065 params.ssids[0].ssid = bss->ssid;
1066 params.ssids[0].ssid_len = bss->ssid_len;
1067 wpa_dbg(wpa_s, MSG_DEBUG,
1068 "Scan a previously specified BSSID " MACSTR
1069 " and SSID %s",
1070 MAC2STR(params.bssid),
1071 wpa_ssid_txt(bss->ssid, bss->ssid_len));
1072 } else {
1073 wpa_dbg(wpa_s, MSG_DEBUG,
1074 "Scan a previously specified BSSID " MACSTR,
1075 MAC2STR(params.bssid));
1076 }
1077 }
1078
Dmitry Shmidt04949592012-07-19 12:16:46 -07001079 scan_params = &params;
1080
1081scan:
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001082#ifdef CONFIG_P2P
1083 /*
1084 * If the driver does not support multi-channel concurrency and a
1085 * virtual interface that shares the same radio with the wpa_s interface
1086 * is operating there may not be need to scan other channels apart from
1087 * the current operating channel on the other virtual interface. Filter
1088 * out other channels in case we are trying to find a connection for a
1089 * station interface when we are not configured to prefer station
1090 * connection and a concurrent operation is already in process.
1091 */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001092 if (wpa_s->scan_for_connection &&
1093 wpa_s->last_scan_req == NORMAL_SCAN_REQ &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001094 !scan_params->freqs && !params.freqs &&
1095 wpas_is_p2p_prioritized(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001096 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
1097 non_p2p_network_enabled(wpa_s)) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001098 unsigned int num = wpa_s->num_multichan_concurrent;
1099
1100 params.freqs = os_calloc(num + 1, sizeof(int));
1101 if (params.freqs) {
1102 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
1103 if (num > 0 && num == wpa_s->num_multichan_concurrent) {
1104 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
1105 } else {
1106 os_free(params.freqs);
1107 params.freqs = NULL;
1108 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001109 }
1110 }
1111#endif /* CONFIG_P2P */
1112
Dmitry Shmidt04949592012-07-19 12:16:46 -07001113 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001114
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001115 if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs &&
1116 !wpa_s->manual_scan_freqs) {
1117 /* Restore manual_scan_freqs for the next attempt */
1118 wpa_s->manual_scan_freqs = params.freqs;
1119 params.freqs = NULL;
1120 }
1121
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001122 wpabuf_free(extra_ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001123 os_free(params.freqs);
1124 os_free(params.filter_ssids);
1125
1126 if (ret) {
1127 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001128 if (wpa_s->scan_prev_wpa_state != wpa_s->wpa_state)
1129 wpa_supplicant_set_state(wpa_s,
1130 wpa_s->scan_prev_wpa_state);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001131 /* Restore scan_req since we will try to scan again */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001132 wpa_s->scan_req = wpa_s->last_scan_req;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001133 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001134 } else {
1135 wpa_s->scan_for_connection = 0;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001136#ifdef CONFIG_INTERWORKING
1137 wpa_s->interworking_fast_assoc_tried = 0;
1138#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001139 if (params.bssid)
1140 os_memset(wpa_s->next_scan_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001141 }
1142}
1143
1144
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001145void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
1146{
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001147 struct os_reltime remaining, new_int;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001148 int cancelled;
1149
1150 cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
1151 &remaining);
1152
1153 new_int.sec = sec;
1154 new_int.usec = 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001155 if (cancelled && os_reltime_before(&remaining, &new_int)) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001156 new_int.sec = remaining.sec;
1157 new_int.usec = remaining.usec;
1158 }
1159
Dmitry Shmidt051af732013-10-22 13:52:46 -07001160 if (cancelled) {
1161 eloop_register_timeout(new_int.sec, new_int.usec,
1162 wpa_supplicant_scan, wpa_s, NULL);
1163 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001164 wpa_s->scan_interval = sec;
1165}
1166
1167
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001168/**
1169 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
1170 * @wpa_s: Pointer to wpa_supplicant data
1171 * @sec: Number of seconds after which to scan
1172 * @usec: Number of microseconds after which to scan
1173 *
1174 * This function is used to schedule a scan for neighboring access points after
1175 * the specified time.
1176 */
1177void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
1178{
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001179 int res;
1180
1181 if (wpa_s->p2p_mgmt) {
1182 wpa_dbg(wpa_s, MSG_DEBUG,
1183 "Ignore scan request (%d.%06d sec) on p2p_mgmt interface",
1184 sec, usec);
1185 return;
1186 }
1187
1188 res = eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s,
1189 NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001190 if (res == 1) {
1191 wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d.%06d sec",
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001192 sec, usec);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001193 } else if (res == 0) {
1194 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore new scan request for %d.%06d sec since an earlier request is scheduled to trigger sooner",
1195 sec, usec);
1196 } else {
1197 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d.%06d sec",
1198 sec, usec);
1199 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001200 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001201}
1202
1203
1204/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001205 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
1206 * @wpa_s: Pointer to wpa_supplicant data
1207 * @sec: Number of seconds after which to scan
1208 * @usec: Number of microseconds after which to scan
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001209 * Returns: 0 on success or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001210 *
1211 * This function is used to schedule periodic scans for neighboring
1212 * access points after the specified time.
1213 */
1214int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
1215 int sec, int usec)
1216{
1217 if (!wpa_s->sched_scan_supported)
1218 return -1;
1219
1220 eloop_register_timeout(sec, usec,
1221 wpa_supplicant_delayed_sched_scan_timeout,
1222 wpa_s, NULL);
1223
1224 return 0;
1225}
1226
1227
1228/**
1229 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
1230 * @wpa_s: Pointer to wpa_supplicant data
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001231 * Returns: 0 is sched_scan was started or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001232 *
1233 * This function is used to schedule periodic scans for neighboring
1234 * access points repeating the scan continuously.
1235 */
1236int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
1237{
1238 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001239 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001240 enum wpa_states prev_state;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001241 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001242 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001243 int ret;
1244 unsigned int max_sched_scan_ssids;
1245 int wildcard = 0;
1246 int need_ssids;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001247 struct sched_scan_plan scan_plan;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001248
1249 if (!wpa_s->sched_scan_supported)
1250 return -1;
1251
1252 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
1253 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
1254 else
1255 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001256 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001257 return -1;
1258
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001259 wpa_s->sched_scan_stop_req = 0;
1260
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001261 if (wpa_s->sched_scanning) {
1262 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
1263 return 0;
1264 }
1265
1266 need_ssids = 0;
1267 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001268 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001269 /* Use wildcard SSID to find this network */
1270 wildcard = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001271 } else if (!wpas_network_disabled(wpa_s, ssid) &&
1272 ssid->ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001273 need_ssids++;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001274
1275#ifdef CONFIG_WPS
1276 if (!wpas_network_disabled(wpa_s, ssid) &&
1277 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1278 /*
1279 * Normal scan is more reliable and faster for WPS
1280 * operations and since these are for short periods of
1281 * time, the benefit of trying to use sched_scan would
1282 * be limited.
1283 */
1284 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1285 "sched_scan for WPS");
1286 return -1;
1287 }
1288#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001289 }
1290 if (wildcard)
1291 need_ssids++;
1292
1293 if (wpa_s->normal_scans < 3 &&
1294 (need_ssids <= wpa_s->max_scan_ssids ||
1295 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
1296 /*
1297 * When normal scan can speed up operations, use that for the
1298 * first operations before starting the sched_scan to allow
1299 * user space sleep more. We do this only if the normal scan
1300 * has functionality that is suitable for this or if the
1301 * sched_scan does not have better support for multiple SSIDs.
1302 */
1303 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1304 "sched_scan for initial scans (normal_scans=%d)",
1305 wpa_s->normal_scans);
1306 return -1;
1307 }
1308
1309 os_memset(&params, 0, sizeof(params));
1310
1311 /* If we can't allocate space for the filters, we just don't filter */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001312 params.filter_ssids = os_calloc(wpa_s->max_match_sets,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001313 sizeof(struct wpa_driver_scan_filter));
1314
1315 prev_state = wpa_s->wpa_state;
1316 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1317 wpa_s->wpa_state == WPA_INACTIVE)
1318 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1319
Dmitry Shmidt04949592012-07-19 12:16:46 -07001320 if (wpa_s->autoscan_params != NULL) {
1321 scan_params = wpa_s->autoscan_params;
1322 goto scan;
1323 }
1324
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001325 /* Find the starting point from which to continue scanning */
1326 ssid = wpa_s->conf->ssid;
1327 if (wpa_s->prev_sched_ssid) {
1328 while (ssid) {
1329 if (ssid == wpa_s->prev_sched_ssid) {
1330 ssid = ssid->next;
1331 break;
1332 }
1333 ssid = ssid->next;
1334 }
1335 }
1336
1337 if (!ssid || !wpa_s->prev_sched_ssid) {
1338 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001339 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1340 wpa_s->first_sched_scan = 1;
1341 ssid = wpa_s->conf->ssid;
1342 wpa_s->prev_sched_ssid = ssid;
1343 }
1344
1345 if (wildcard) {
1346 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1347 params.num_ssids++;
1348 }
1349
1350 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001351 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001352 goto next;
1353
1354 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1355 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1356 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1357 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1358 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1359 ssid->ssid, ssid->ssid_len);
1360 params.filter_ssids[params.num_filter_ssids].ssid_len =
1361 ssid->ssid_len;
1362 params.num_filter_ssids++;
1363 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1364 {
1365 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1366 "filter for sched_scan - drop filter");
1367 os_free(params.filter_ssids);
1368 params.filter_ssids = NULL;
1369 params.num_filter_ssids = 0;
1370 }
1371
1372 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1373 if (params.num_ssids == max_sched_scan_ssids)
1374 break; /* only room for broadcast SSID */
1375 wpa_dbg(wpa_s, MSG_DEBUG,
1376 "add to active scan ssid: %s",
1377 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1378 params.ssids[params.num_ssids].ssid =
1379 ssid->ssid;
1380 params.ssids[params.num_ssids].ssid_len =
1381 ssid->ssid_len;
1382 params.num_ssids++;
1383 if (params.num_ssids >= max_sched_scan_ssids) {
1384 wpa_s->prev_sched_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001385 do {
1386 ssid = ssid->next;
1387 } while (ssid &&
1388 (wpas_network_disabled(wpa_s, ssid) ||
1389 !ssid->scan_ssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001390 break;
1391 }
1392 }
1393
1394 next:
1395 wpa_s->prev_sched_ssid = ssid;
1396 ssid = ssid->next;
1397 }
1398
1399 if (params.num_filter_ssids == 0) {
1400 os_free(params.filter_ssids);
1401 params.filter_ssids = NULL;
1402 }
1403
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001404 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1405 if (extra_ie) {
1406 params.extra_ies = wpabuf_head(extra_ie);
1407 params.extra_ies_len = wpabuf_len(extra_ie);
1408 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001409
Dmitry Shmidt18463232014-01-24 12:29:41 -08001410 if (wpa_s->conf->filter_rssi)
1411 params.filter_rssi = wpa_s->conf->filter_rssi;
1412
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001413 /* See if user specified frequencies. If so, scan only those. */
1414 if (wpa_s->conf->freq_list && !params.freqs) {
1415 wpa_dbg(wpa_s, MSG_DEBUG,
1416 "Optimize scan based on conf->freq_list");
1417 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1418 }
1419
Dmitry Shmidt04949592012-07-19 12:16:46 -07001420 scan_params = &params;
1421
1422scan:
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001423 wpa_s->sched_scan_timed_out = 0;
1424
1425 /*
1426 * We cannot support multiple scan plans if the scan request includes
1427 * too many SSID's, so in this case use only the last scan plan and make
1428 * it run infinitely. It will be stopped by the timeout.
1429 */
1430 if (wpa_s->sched_scan_plans_num == 1 ||
1431 (wpa_s->sched_scan_plans_num && !ssid && wpa_s->first_sched_scan)) {
1432 params.sched_scan_plans = wpa_s->sched_scan_plans;
1433 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
1434 } else if (wpa_s->sched_scan_plans_num > 1) {
1435 wpa_dbg(wpa_s, MSG_DEBUG,
1436 "Too many SSIDs. Default to using single scheduled_scan plan");
1437 params.sched_scan_plans =
1438 &wpa_s->sched_scan_plans[wpa_s->sched_scan_plans_num -
1439 1];
1440 params.sched_scan_plans_num = 1;
1441 } else {
1442 if (wpa_s->conf->sched_scan_interval)
1443 scan_plan.interval = wpa_s->conf->sched_scan_interval;
1444 else
1445 scan_plan.interval = 10;
1446
1447 if (scan_plan.interval > wpa_s->max_sched_scan_plan_interval) {
1448 wpa_printf(MSG_WARNING,
1449 "Scan interval too long(%u), use the maximum allowed(%u)",
1450 scan_plan.interval,
1451 wpa_s->max_sched_scan_plan_interval);
1452 scan_plan.interval =
1453 wpa_s->max_sched_scan_plan_interval;
1454 }
1455
1456 scan_plan.iterations = 0;
1457 params.sched_scan_plans = &scan_plan;
1458 params.sched_scan_plans_num = 1;
1459 }
1460
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001461 if (ssid || !wpa_s->first_sched_scan) {
1462 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001463 "Starting sched scan: interval %u timeout %d",
1464 params.sched_scan_plans[0].interval,
1465 wpa_s->sched_scan_timeout);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001466 } else {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001467 wpa_dbg(wpa_s, MSG_DEBUG, "Starting sched scan (no timeout)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001468 }
1469
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001470 wpa_setband_scan_freqs(wpa_s, scan_params);
1471
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001472 if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCHED_SCAN) {
1473 params.mac_addr_rand = 1;
1474 if (wpa_s->mac_addr_sched_scan) {
1475 params.mac_addr = wpa_s->mac_addr_sched_scan;
1476 params.mac_addr_mask = wpa_s->mac_addr_sched_scan +
1477 ETH_ALEN;
1478 }
1479 }
1480
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001481 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001482 wpabuf_free(extra_ie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001483 os_free(params.filter_ssids);
1484 if (ret) {
1485 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1486 if (prev_state != wpa_s->wpa_state)
1487 wpa_supplicant_set_state(wpa_s, prev_state);
1488 return ret;
1489 }
1490
1491 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1492 if (ssid || !wpa_s->first_sched_scan) {
1493 wpa_s->sched_scan_timed_out = 0;
1494 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1495 wpa_supplicant_sched_scan_timeout,
1496 wpa_s, NULL);
1497 wpa_s->first_sched_scan = 0;
1498 wpa_s->sched_scan_timeout /= 2;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001499 params.sched_scan_plans[0].interval *= 2;
1500 if ((unsigned int) wpa_s->sched_scan_timeout <
1501 params.sched_scan_plans[0].interval ||
1502 params.sched_scan_plans[0].interval >
1503 wpa_s->max_sched_scan_plan_interval) {
1504 params.sched_scan_plans[0].interval = 10;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001505 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1506 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001507 }
1508
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001509 /* If there is no more ssids, start next time from the beginning */
1510 if (!ssid)
1511 wpa_s->prev_sched_ssid = NULL;
1512
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001513 return 0;
1514}
1515
1516
1517/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001518 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1519 * @wpa_s: Pointer to wpa_supplicant data
1520 *
1521 * This function is used to cancel a scan request scheduled with
1522 * wpa_supplicant_req_scan().
1523 */
1524void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1525{
1526 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1527 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001528}
1529
1530
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001531/**
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001532 * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
1533 * @wpa_s: Pointer to wpa_supplicant data
1534 *
1535 * This function is used to stop a delayed scheduled scan.
1536 */
1537void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
1538{
1539 if (!wpa_s->sched_scan_supported)
1540 return;
1541
1542 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
1543 eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
1544 wpa_s, NULL);
1545}
1546
1547
1548/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001549 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1550 * @wpa_s: Pointer to wpa_supplicant data
1551 *
1552 * This function is used to stop a periodic scheduled scan.
1553 */
1554void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1555{
1556 if (!wpa_s->sched_scanning)
1557 return;
1558
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001559 if (wpa_s->sched_scanning)
1560 wpa_s->sched_scan_stop_req = 1;
1561
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001562 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1563 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1564 wpa_supplicant_stop_sched_scan(wpa_s);
1565}
1566
1567
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001568/**
1569 * wpa_supplicant_notify_scanning - Indicate possible scan state change
1570 * @wpa_s: Pointer to wpa_supplicant data
1571 * @scanning: Whether scanning is currently in progress
1572 *
1573 * This function is to generate scanning notifycations. It is called whenever
1574 * there may have been a change in scanning (scan started, completed, stopped).
1575 * wpas_notify_scanning() is called whenever the scanning state changed from the
1576 * previously notified state.
1577 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001578void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1579 int scanning)
1580{
1581 if (wpa_s->scanning != scanning) {
1582 wpa_s->scanning = scanning;
1583 wpas_notify_scanning(wpa_s);
1584 }
1585}
1586
1587
1588static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1589{
1590 int rate = 0;
1591 const u8 *ie;
1592 int i;
1593
1594 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1595 for (i = 0; ie && i < ie[1]; i++) {
1596 if ((ie[i + 2] & 0x7f) > rate)
1597 rate = ie[i + 2] & 0x7f;
1598 }
1599
1600 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1601 for (i = 0; ie && i < ie[1]; i++) {
1602 if ((ie[i + 2] & 0x7f) > rate)
1603 rate = ie[i + 2] & 0x7f;
1604 }
1605
1606 return rate;
1607}
1608
1609
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001610/**
1611 * wpa_scan_get_ie - Fetch a specified information element from a scan result
1612 * @res: Scan result entry
1613 * @ie: Information element identitifier (WLAN_EID_*)
1614 * Returns: Pointer to the information element (id field) or %NULL if not found
1615 *
1616 * This function returns the first matching information element in the scan
1617 * result.
1618 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001619const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1620{
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001621 size_t ie_len = res->ie_len;
1622
1623 /* Use the Beacon frame IEs if res->ie_len is not available */
1624 if (!ie_len)
1625 ie_len = res->beacon_ie_len;
1626
1627 return get_ie((const u8 *) (res + 1), ie_len, ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001628}
1629
1630
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001631/**
1632 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1633 * @res: Scan result entry
1634 * @vendor_type: Vendor type (four octets starting the IE payload)
1635 * Returns: Pointer to the information element (id field) or %NULL if not found
1636 *
1637 * This function returns the first matching information element in the scan
1638 * result.
1639 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001640const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1641 u32 vendor_type)
1642{
1643 const u8 *end, *pos;
1644
1645 pos = (const u8 *) (res + 1);
1646 end = pos + res->ie_len;
1647
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001648 while (end - pos > 1) {
1649 if (2 + pos[1] > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001650 break;
1651 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1652 vendor_type == WPA_GET_BE32(&pos[2]))
1653 return pos;
1654 pos += 2 + pos[1];
1655 }
1656
1657 return NULL;
1658}
1659
1660
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001661/**
Dmitry Shmidt96571392013-10-14 12:54:46 -07001662 * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
1663 * @res: Scan result entry
1664 * @vendor_type: Vendor type (four octets starting the IE payload)
1665 * Returns: Pointer to the information element (id field) or %NULL if not found
1666 *
1667 * This function returns the first matching information element in the scan
1668 * result.
1669 *
1670 * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
1671 * from Beacon frames instead of either Beacon or Probe Response frames.
1672 */
1673const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
1674 u32 vendor_type)
1675{
1676 const u8 *end, *pos;
1677
1678 if (res->beacon_ie_len == 0)
1679 return NULL;
1680
1681 pos = (const u8 *) (res + 1);
1682 pos += res->ie_len;
1683 end = pos + res->beacon_ie_len;
1684
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001685 while (end - pos > 1) {
1686 if (2 + pos[1] > end - pos)
Dmitry Shmidt96571392013-10-14 12:54:46 -07001687 break;
1688 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1689 vendor_type == WPA_GET_BE32(&pos[2]))
1690 return pos;
1691 pos += 2 + pos[1];
1692 }
1693
1694 return NULL;
1695}
1696
1697
1698/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001699 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
1700 * @res: Scan result entry
1701 * @vendor_type: Vendor type (four octets starting the IE payload)
1702 * Returns: Pointer to the information element payload or %NULL if not found
1703 *
1704 * This function returns concatenated payload of possibly fragmented vendor
1705 * specific information elements in the scan result. The caller is responsible
1706 * for freeing the returned buffer.
1707 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001708struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1709 u32 vendor_type)
1710{
1711 struct wpabuf *buf;
1712 const u8 *end, *pos;
1713
1714 buf = wpabuf_alloc(res->ie_len);
1715 if (buf == NULL)
1716 return NULL;
1717
1718 pos = (const u8 *) (res + 1);
1719 end = pos + res->ie_len;
1720
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001721 while (end - pos > 1) {
1722 if (2 + pos[1] > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001723 break;
1724 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1725 vendor_type == WPA_GET_BE32(&pos[2]))
1726 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1727 pos += 2 + pos[1];
1728 }
1729
1730 if (wpabuf_len(buf) == 0) {
1731 wpabuf_free(buf);
1732 buf = NULL;
1733 }
1734
1735 return buf;
1736}
1737
1738
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001739/*
1740 * Channels with a great SNR can operate at full rate. What is a great SNR?
1741 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1742 * rule of thumb is that any SNR above 20 is good." This one
1743 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1744 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1745 * conservative value.
1746 */
1747#define GREAT_SNR 30
1748
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001749#define IS_5GHZ(n) (n > 4000)
1750
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001751/* Compare function for sorting scan results. Return >0 if @b is considered
1752 * better. */
1753static int wpa_scan_result_compar(const void *a, const void *b)
1754{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001755#define MIN(a,b) a < b ? a : b
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001756 struct wpa_scan_res **_wa = (void *) a;
1757 struct wpa_scan_res **_wb = (void *) b;
1758 struct wpa_scan_res *wa = *_wa;
1759 struct wpa_scan_res *wb = *_wb;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001760 int wpa_a, wpa_b;
1761 int snr_a, snr_b, snr_a_full, snr_b_full;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001762
1763 /* WPA/WPA2 support preferred */
1764 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1765 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1766 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1767 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1768
1769 if (wpa_b && !wpa_a)
1770 return 1;
1771 if (!wpa_b && wpa_a)
1772 return -1;
1773
1774 /* privacy support preferred */
1775 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1776 (wb->caps & IEEE80211_CAP_PRIVACY))
1777 return 1;
1778 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1779 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1780 return -1;
1781
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001782 if (wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001783 snr_a_full = wa->snr;
1784 snr_a = MIN(wa->snr, GREAT_SNR);
1785 snr_b_full = wb->snr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001786 snr_b = MIN(wb->snr, GREAT_SNR);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001787 } else {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001788 /* Level is not in dBm, so we can't calculate
1789 * SNR. Just use raw level (units unknown). */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001790 snr_a = snr_a_full = wa->level;
1791 snr_b = snr_b_full = wb->level;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001792 }
1793
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001794 /* if SNR is close, decide by max rate or frequency band */
1795 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001796 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001797 if (wa->est_throughput != wb->est_throughput)
1798 return wb->est_throughput - wa->est_throughput;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001799 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1800 return IS_5GHZ(wa->freq) ? -1 : 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001801 }
1802
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001803 /* all things being equal, use SNR; if SNRs are
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001804 * identical, use quality values since some drivers may only report
1805 * that value and leave the signal level zero */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001806 if (snr_b_full == snr_a_full)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001807 return wb->qual - wa->qual;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001808 return snr_b_full - snr_a_full;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001809#undef MIN
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001810}
1811
1812
1813#ifdef CONFIG_WPS
1814/* Compare function for sorting scan results when searching a WPS AP for
1815 * provisioning. Return >0 if @b is considered better. */
1816static int wpa_scan_result_wps_compar(const void *a, const void *b)
1817{
1818 struct wpa_scan_res **_wa = (void *) a;
1819 struct wpa_scan_res **_wb = (void *) b;
1820 struct wpa_scan_res *wa = *_wa;
1821 struct wpa_scan_res *wb = *_wb;
1822 int uses_wps_a, uses_wps_b;
1823 struct wpabuf *wps_a, *wps_b;
1824 int res;
1825
1826 /* Optimization - check WPS IE existence before allocated memory and
1827 * doing full reassembly. */
1828 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1829 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1830 if (uses_wps_a && !uses_wps_b)
1831 return -1;
1832 if (!uses_wps_a && uses_wps_b)
1833 return 1;
1834
1835 if (uses_wps_a && uses_wps_b) {
1836 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1837 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1838 res = wps_ap_priority_compar(wps_a, wps_b);
1839 wpabuf_free(wps_a);
1840 wpabuf_free(wps_b);
1841 if (res)
1842 return res;
1843 }
1844
1845 /*
1846 * Do not use current AP security policy as a sorting criteria during
1847 * WPS provisioning step since the AP may get reconfigured at the
1848 * completion of provisioning.
1849 */
1850
1851 /* all things being equal, use signal level; if signal levels are
1852 * identical, use quality values since some drivers may only report
1853 * that value and leave the signal level zero */
1854 if (wb->level == wa->level)
1855 return wb->qual - wa->qual;
1856 return wb->level - wa->level;
1857}
1858#endif /* CONFIG_WPS */
1859
1860
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001861static void dump_scan_res(struct wpa_scan_results *scan_res)
1862{
1863#ifndef CONFIG_NO_STDOUT_DEBUG
1864 size_t i;
1865
1866 if (scan_res->res == NULL || scan_res->num == 0)
1867 return;
1868
1869 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1870
1871 for (i = 0; i < scan_res->num; i++) {
1872 struct wpa_scan_res *r = scan_res->res[i];
Dmitry Shmidt04949592012-07-19 12:16:46 -07001873 u8 *pos;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001874 if (r->flags & WPA_SCAN_LEVEL_DBM) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001875 int noise_valid = !(r->flags & WPA_SCAN_NOISE_INVALID);
1876
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001877 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001878 "noise=%d%s level=%d snr=%d%s flags=0x%x age=%u est=%u",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001879 MAC2STR(r->bssid), r->freq, r->qual,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001880 r->noise, noise_valid ? "" : "~", r->level,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001881 r->snr, r->snr >= GREAT_SNR ? "*" : "",
1882 r->flags,
1883 r->age, r->est_throughput);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001884 } else {
1885 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001886 "noise=%d level=%d flags=0x%x age=%u est=%u",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001887 MAC2STR(r->bssid), r->freq, r->qual,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001888 r->noise, r->level, r->flags, r->age,
1889 r->est_throughput);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001890 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001891 pos = (u8 *) (r + 1);
1892 if (r->ie_len)
1893 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
1894 pos += r->ie_len;
1895 if (r->beacon_ie_len)
1896 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
1897 pos, r->beacon_ie_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001898 }
1899#endif /* CONFIG_NO_STDOUT_DEBUG */
1900}
1901
1902
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001903/**
1904 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
1905 * @wpa_s: Pointer to wpa_supplicant data
1906 * @bssid: BSSID to check
1907 * Returns: 0 if the BSSID is filtered or 1 if not
1908 *
1909 * This function is used to filter out specific BSSIDs from scan reslts mainly
1910 * for testing purposes (SET bssid_filter ctrl_iface command).
1911 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001912int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
1913 const u8 *bssid)
1914{
1915 size_t i;
1916
1917 if (wpa_s->bssid_filter == NULL)
1918 return 1;
1919
1920 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
1921 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
1922 ETH_ALEN) == 0)
1923 return 1;
1924 }
1925
1926 return 0;
1927}
1928
1929
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001930void filter_scan_res(struct wpa_supplicant *wpa_s,
1931 struct wpa_scan_results *res)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001932{
1933 size_t i, j;
1934
1935 if (wpa_s->bssid_filter == NULL)
1936 return;
1937
1938 for (i = 0, j = 0; i < res->num; i++) {
1939 if (wpa_supplicant_filter_bssid_match(wpa_s,
1940 res->res[i]->bssid)) {
1941 res->res[j++] = res->res[i];
1942 } else {
1943 os_free(res->res[i]);
1944 res->res[i] = NULL;
1945 }
1946 }
1947
1948 if (res->num != j) {
1949 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
1950 (int) (res->num - j));
1951 res->num = j;
1952 }
1953}
1954
1955
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001956/*
1957 * Noise floor values to use when we have signal strength
Dmitry Shmidte4663042016-04-04 10:07:49 -07001958 * measurements, but no noise floor measurements. These values were
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001959 * measured in an office environment with many APs.
1960 */
1961#define DEFAULT_NOISE_FLOOR_2GHZ (-89)
1962#define DEFAULT_NOISE_FLOOR_5GHZ (-92)
1963
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001964void scan_snr(struct wpa_scan_res *res)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001965{
1966 if (res->flags & WPA_SCAN_NOISE_INVALID) {
1967 res->noise = IS_5GHZ(res->freq) ?
1968 DEFAULT_NOISE_FLOOR_5GHZ :
1969 DEFAULT_NOISE_FLOOR_2GHZ;
1970 }
1971
1972 if (res->flags & WPA_SCAN_LEVEL_DBM) {
1973 res->snr = res->level - res->noise;
1974 } else {
1975 /* Level is not in dBm, so we can't calculate
1976 * SNR. Just use raw level (units unknown). */
1977 res->snr = res->level;
1978 }
1979}
1980
1981
1982static unsigned int max_ht20_rate(int snr)
1983{
1984 if (snr < 6)
1985 return 6500; /* HT20 MCS0 */
1986 if (snr < 8)
1987 return 13000; /* HT20 MCS1 */
1988 if (snr < 13)
1989 return 19500; /* HT20 MCS2 */
1990 if (snr < 17)
1991 return 26000; /* HT20 MCS3 */
1992 if (snr < 20)
1993 return 39000; /* HT20 MCS4 */
1994 if (snr < 23)
1995 return 52000; /* HT20 MCS5 */
1996 if (snr < 24)
1997 return 58500; /* HT20 MCS6 */
1998 return 65000; /* HT20 MCS7 */
1999}
2000
2001
2002static unsigned int max_ht40_rate(int snr)
2003{
2004 if (snr < 3)
2005 return 13500; /* HT40 MCS0 */
2006 if (snr < 6)
2007 return 27000; /* HT40 MCS1 */
2008 if (snr < 10)
2009 return 40500; /* HT40 MCS2 */
2010 if (snr < 15)
2011 return 54000; /* HT40 MCS3 */
2012 if (snr < 17)
2013 return 81000; /* HT40 MCS4 */
2014 if (snr < 22)
2015 return 108000; /* HT40 MCS5 */
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002016 if (snr < 24)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002017 return 121500; /* HT40 MCS6 */
2018 return 135000; /* HT40 MCS7 */
2019}
2020
2021
2022static unsigned int max_vht80_rate(int snr)
2023{
2024 if (snr < 1)
2025 return 0;
2026 if (snr < 2)
2027 return 29300; /* VHT80 MCS0 */
2028 if (snr < 5)
2029 return 58500; /* VHT80 MCS1 */
2030 if (snr < 9)
2031 return 87800; /* VHT80 MCS2 */
2032 if (snr < 11)
2033 return 117000; /* VHT80 MCS3 */
2034 if (snr < 15)
2035 return 175500; /* VHT80 MCS4 */
2036 if (snr < 16)
2037 return 234000; /* VHT80 MCS5 */
2038 if (snr < 18)
2039 return 263300; /* VHT80 MCS6 */
2040 if (snr < 20)
2041 return 292500; /* VHT80 MCS7 */
2042 if (snr < 22)
2043 return 351000; /* VHT80 MCS8 */
2044 return 390000; /* VHT80 MCS9 */
2045}
2046
2047
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002048void scan_est_throughput(struct wpa_supplicant *wpa_s,
2049 struct wpa_scan_res *res)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002050{
2051 enum local_hw_capab capab = wpa_s->hw_capab;
2052 int rate; /* max legacy rate in 500 kb/s units */
2053 const u8 *ie;
2054 unsigned int est, tmp;
2055 int snr = res->snr;
2056
2057 if (res->est_throughput)
2058 return;
2059
2060 /* Get maximum legacy rate */
2061 rate = wpa_scan_get_max_rate(res);
2062
2063 /* Limit based on estimated SNR */
2064 if (rate > 1 * 2 && snr < 1)
2065 rate = 1 * 2;
2066 else if (rate > 2 * 2 && snr < 4)
2067 rate = 2 * 2;
2068 else if (rate > 6 * 2 && snr < 5)
2069 rate = 6 * 2;
2070 else if (rate > 9 * 2 && snr < 6)
2071 rate = 9 * 2;
2072 else if (rate > 12 * 2 && snr < 7)
2073 rate = 12 * 2;
2074 else if (rate > 18 * 2 && snr < 10)
2075 rate = 18 * 2;
2076 else if (rate > 24 * 2 && snr < 11)
2077 rate = 24 * 2;
2078 else if (rate > 36 * 2 && snr < 15)
2079 rate = 36 * 2;
2080 else if (rate > 48 * 2 && snr < 19)
2081 rate = 48 * 2;
2082 else if (rate > 54 * 2 && snr < 21)
2083 rate = 54 * 2;
2084 est = rate * 500;
2085
2086 if (capab == CAPAB_HT || capab == CAPAB_HT40 || capab == CAPAB_VHT) {
2087 ie = wpa_scan_get_ie(res, WLAN_EID_HT_CAP);
2088 if (ie) {
2089 tmp = max_ht20_rate(snr);
2090 if (tmp > est)
2091 est = tmp;
2092 }
2093 }
2094
2095 if (capab == CAPAB_HT40 || capab == CAPAB_VHT) {
2096 ie = wpa_scan_get_ie(res, WLAN_EID_HT_OPERATION);
2097 if (ie && ie[1] >= 2 &&
2098 (ie[3] & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
2099 tmp = max_ht40_rate(snr);
2100 if (tmp > est)
2101 est = tmp;
2102 }
2103 }
2104
2105 if (capab == CAPAB_VHT) {
2106 /* Use +1 to assume VHT is always faster than HT */
2107 ie = wpa_scan_get_ie(res, WLAN_EID_VHT_CAP);
2108 if (ie) {
2109 tmp = max_ht20_rate(snr) + 1;
2110 if (tmp > est)
2111 est = tmp;
2112
2113 ie = wpa_scan_get_ie(res, WLAN_EID_HT_OPERATION);
2114 if (ie && ie[1] >= 2 &&
2115 (ie[3] &
2116 HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
2117 tmp = max_ht40_rate(snr) + 1;
2118 if (tmp > est)
2119 est = tmp;
2120 }
2121
2122 ie = wpa_scan_get_ie(res, WLAN_EID_VHT_OPERATION);
2123 if (ie && ie[1] >= 1 &&
2124 (ie[2] & VHT_OPMODE_CHANNEL_WIDTH_MASK)) {
2125 tmp = max_vht80_rate(snr) + 1;
2126 if (tmp > est)
2127 est = tmp;
2128 }
2129 }
2130 }
2131
2132 /* TODO: channel utilization and AP load (e.g., from AP Beacon) */
2133
2134 res->est_throughput = est;
2135}
2136
2137
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002138/**
2139 * wpa_supplicant_get_scan_results - Get scan results
2140 * @wpa_s: Pointer to wpa_supplicant data
2141 * @info: Information about what was scanned or %NULL if not available
2142 * @new_scan: Whether a new scan was performed
2143 * Returns: Scan results, %NULL on failure
2144 *
2145 * This function request the current scan results from the driver and updates
2146 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
2147 * results with wpa_scan_results_free().
2148 */
2149struct wpa_scan_results *
2150wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
2151 struct scan_info *info, int new_scan)
2152{
2153 struct wpa_scan_results *scan_res;
2154 size_t i;
2155 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
2156
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002157 scan_res = wpa_drv_get_scan_results2(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002158 if (scan_res == NULL) {
2159 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
2160 return NULL;
2161 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002162 if (scan_res->fetch_time.sec == 0) {
2163 /*
2164 * Make sure we have a valid timestamp if the driver wrapper
2165 * does not set this.
2166 */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002167 os_get_reltime(&scan_res->fetch_time);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002168 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002169 filter_scan_res(wpa_s, scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002170
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002171 for (i = 0; i < scan_res->num; i++) {
2172 struct wpa_scan_res *scan_res_item = scan_res->res[i];
2173
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002174 scan_snr(scan_res_item);
2175 scan_est_throughput(wpa_s, scan_res_item);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002176 }
2177
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002178#ifdef CONFIG_WPS
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002179 if (wpas_wps_searching(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002180 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
2181 "provisioning rules");
2182 compar = wpa_scan_result_wps_compar;
2183 }
2184#endif /* CONFIG_WPS */
2185
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002186 if (scan_res->res) {
2187 qsort(scan_res->res, scan_res->num,
2188 sizeof(struct wpa_scan_res *), compar);
2189 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002190 dump_scan_res(scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002191
2192 wpa_bss_update_start(wpa_s);
2193 for (i = 0; i < scan_res->num; i++)
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002194 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
2195 &scan_res->fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002196 wpa_bss_update_end(wpa_s, info, new_scan);
2197
2198 return scan_res;
2199}
2200
2201
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002202/**
2203 * wpa_supplicant_update_scan_results - Update scan results from the driver
2204 * @wpa_s: Pointer to wpa_supplicant data
2205 * Returns: 0 on success, -1 on failure
2206 *
2207 * This function updates the BSS table within wpa_supplicant based on the
2208 * currently available scan results from the driver without requesting a new
2209 * scan. This is used in cases where the driver indicates an association
2210 * (including roaming within ESS) and wpa_supplicant does not yet have the
2211 * needed information to complete the connection (e.g., to perform validation
2212 * steps in 4-way handshake).
2213 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002214int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
2215{
2216 struct wpa_scan_results *scan_res;
2217 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
2218 if (scan_res == NULL)
2219 return -1;
2220 wpa_scan_results_free(scan_res);
2221
2222 return 0;
2223}
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002224
2225
2226/**
2227 * scan_only_handler - Reports scan results
2228 */
2229void scan_only_handler(struct wpa_supplicant *wpa_s,
2230 struct wpa_scan_results *scan_res)
2231{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002232 wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002233 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
2234 wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
2235 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
2236 wpa_s->manual_scan_id);
2237 wpa_s->manual_scan_use_id = 0;
2238 } else {
2239 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
2240 }
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002241 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002242 wpas_notify_scan_done(wpa_s, 1);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002243 if (wpa_s->scan_work) {
2244 struct wpa_radio_work *work = wpa_s->scan_work;
2245 wpa_s->scan_work = NULL;
2246 radio_work_done(work);
2247 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002248
2249 if (wpa_s->wpa_state == WPA_SCANNING)
2250 wpa_supplicant_set_state(wpa_s, wpa_s->scan_prev_wpa_state);
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002251}
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07002252
2253
2254int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
2255{
2256 return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
2257}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002258
2259
2260struct wpa_driver_scan_params *
2261wpa_scan_clone_params(const struct wpa_driver_scan_params *src)
2262{
2263 struct wpa_driver_scan_params *params;
2264 size_t i;
2265 u8 *n;
2266
2267 params = os_zalloc(sizeof(*params));
2268 if (params == NULL)
2269 return NULL;
2270
2271 for (i = 0; i < src->num_ssids; i++) {
2272 if (src->ssids[i].ssid) {
2273 n = os_malloc(src->ssids[i].ssid_len);
2274 if (n == NULL)
2275 goto failed;
2276 os_memcpy(n, src->ssids[i].ssid,
2277 src->ssids[i].ssid_len);
2278 params->ssids[i].ssid = n;
2279 params->ssids[i].ssid_len = src->ssids[i].ssid_len;
2280 }
2281 }
2282 params->num_ssids = src->num_ssids;
2283
2284 if (src->extra_ies) {
2285 n = os_malloc(src->extra_ies_len);
2286 if (n == NULL)
2287 goto failed;
2288 os_memcpy(n, src->extra_ies, src->extra_ies_len);
2289 params->extra_ies = n;
2290 params->extra_ies_len = src->extra_ies_len;
2291 }
2292
2293 if (src->freqs) {
2294 int len = int_array_len(src->freqs);
2295 params->freqs = os_malloc((len + 1) * sizeof(int));
2296 if (params->freqs == NULL)
2297 goto failed;
2298 os_memcpy(params->freqs, src->freqs, (len + 1) * sizeof(int));
2299 }
2300
2301 if (src->filter_ssids) {
Dmitry Shmidt97672262014-02-03 13:02:54 -08002302 params->filter_ssids = os_malloc(sizeof(*params->filter_ssids) *
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002303 src->num_filter_ssids);
2304 if (params->filter_ssids == NULL)
2305 goto failed;
2306 os_memcpy(params->filter_ssids, src->filter_ssids,
Dmitry Shmidt97672262014-02-03 13:02:54 -08002307 sizeof(*params->filter_ssids) *
2308 src->num_filter_ssids);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002309 params->num_filter_ssids = src->num_filter_ssids;
2310 }
2311
2312 params->filter_rssi = src->filter_rssi;
2313 params->p2p_probe = src->p2p_probe;
2314 params->only_new_results = src->only_new_results;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07002315 params->low_priority = src->low_priority;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002316
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002317 if (src->sched_scan_plans_num > 0) {
2318 params->sched_scan_plans =
2319 os_malloc(sizeof(*src->sched_scan_plans) *
2320 src->sched_scan_plans_num);
2321 if (!params->sched_scan_plans)
2322 goto failed;
2323
2324 os_memcpy(params->sched_scan_plans, src->sched_scan_plans,
2325 sizeof(*src->sched_scan_plans) *
2326 src->sched_scan_plans_num);
2327 params->sched_scan_plans_num = src->sched_scan_plans_num;
2328 }
2329
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002330 if (src->mac_addr_rand) {
2331 params->mac_addr_rand = src->mac_addr_rand;
2332
2333 if (src->mac_addr && src->mac_addr_mask) {
2334 u8 *mac_addr;
2335
2336 mac_addr = os_malloc(2 * ETH_ALEN);
2337 if (!mac_addr)
2338 goto failed;
2339
2340 os_memcpy(mac_addr, src->mac_addr, ETH_ALEN);
2341 os_memcpy(mac_addr + ETH_ALEN, src->mac_addr_mask,
2342 ETH_ALEN);
2343 params->mac_addr = mac_addr;
2344 params->mac_addr_mask = mac_addr + ETH_ALEN;
2345 }
2346 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002347
2348 if (src->bssid) {
2349 u8 *bssid;
2350
2351 bssid = os_malloc(ETH_ALEN);
2352 if (!bssid)
2353 goto failed;
2354 os_memcpy(bssid, src->bssid, ETH_ALEN);
2355 params->bssid = bssid;
2356 }
2357
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002358 return params;
2359
2360failed:
2361 wpa_scan_free_params(params);
2362 return NULL;
2363}
2364
2365
2366void wpa_scan_free_params(struct wpa_driver_scan_params *params)
2367{
2368 size_t i;
2369
2370 if (params == NULL)
2371 return;
2372
2373 for (i = 0; i < params->num_ssids; i++)
2374 os_free((u8 *) params->ssids[i].ssid);
2375 os_free((u8 *) params->extra_ies);
2376 os_free(params->freqs);
2377 os_free(params->filter_ssids);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002378 os_free(params->sched_scan_plans);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002379
2380 /*
2381 * Note: params->mac_addr_mask points to same memory allocation and
2382 * must not be freed separately.
2383 */
2384 os_free((u8 *) params->mac_addr);
2385
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002386 os_free((u8 *) params->bssid);
2387
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002388 os_free(params);
2389}
Dmitry Shmidt98660862014-03-11 17:26:21 -07002390
2391
2392int wpas_start_pno(struct wpa_supplicant *wpa_s)
2393{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002394 int ret, prio;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002395 size_t i, num_ssid, num_match_ssid;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002396 struct wpa_ssid *ssid;
2397 struct wpa_driver_scan_params params;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002398 struct sched_scan_plan scan_plan;
Dmitry Shmidte4663042016-04-04 10:07:49 -07002399 unsigned int max_sched_scan_ssids;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002400
2401 if (!wpa_s->sched_scan_supported)
2402 return -1;
2403
Dmitry Shmidte4663042016-04-04 10:07:49 -07002404 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
2405 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
2406 else
2407 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
2408 if (max_sched_scan_ssids < 1)
2409 return -1;
2410
Dmitry Shmidt98660862014-03-11 17:26:21 -07002411 if (wpa_s->pno || wpa_s->pno_sched_pending)
2412 return 0;
2413
2414 if ((wpa_s->wpa_state > WPA_SCANNING) &&
2415 (wpa_s->wpa_state <= WPA_COMPLETED)) {
2416 wpa_printf(MSG_ERROR, "PNO: In assoc process");
2417 return -EAGAIN;
2418 }
2419
2420 if (wpa_s->wpa_state == WPA_SCANNING) {
2421 wpa_supplicant_cancel_scan(wpa_s);
2422 if (wpa_s->sched_scanning) {
2423 wpa_printf(MSG_DEBUG, "Schedule PNO on completion of "
2424 "ongoing sched scan");
2425 wpa_supplicant_cancel_sched_scan(wpa_s);
2426 wpa_s->pno_sched_pending = 1;
2427 return 0;
2428 }
2429 }
2430
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002431 if (wpa_s->sched_scan_stop_req) {
2432 wpa_printf(MSG_DEBUG,
2433 "Schedule PNO after previous sched scan has stopped");
2434 wpa_s->pno_sched_pending = 1;
2435 return 0;
2436 }
2437
Dmitry Shmidt98660862014-03-11 17:26:21 -07002438 os_memset(&params, 0, sizeof(params));
2439
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002440 num_ssid = num_match_ssid = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002441 ssid = wpa_s->conf->ssid;
2442 while (ssid) {
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002443 if (!wpas_network_disabled(wpa_s, ssid)) {
2444 num_match_ssid++;
2445 if (ssid->scan_ssid)
2446 num_ssid++;
2447 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07002448 ssid = ssid->next;
2449 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002450
2451 if (num_match_ssid == 0) {
2452 wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
2453 return -1;
2454 }
2455
2456 if (num_match_ssid > num_ssid) {
2457 params.num_ssids++; /* wildcard */
2458 num_ssid++;
2459 }
2460
Dmitry Shmidte4663042016-04-04 10:07:49 -07002461 if (num_ssid > max_sched_scan_ssids) {
Dmitry Shmidt98660862014-03-11 17:26:21 -07002462 wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
Dmitry Shmidte4663042016-04-04 10:07:49 -07002463 "%u", max_sched_scan_ssids, (unsigned int) num_ssid);
2464 num_ssid = max_sched_scan_ssids;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002465 }
2466
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002467 if (num_match_ssid > wpa_s->max_match_sets) {
2468 num_match_ssid = wpa_s->max_match_sets;
2469 wpa_dbg(wpa_s, MSG_DEBUG, "PNO: Too many SSIDs to match");
Dmitry Shmidt98660862014-03-11 17:26:21 -07002470 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002471 params.filter_ssids = os_calloc(num_match_ssid,
2472 sizeof(struct wpa_driver_scan_filter));
Dmitry Shmidt98660862014-03-11 17:26:21 -07002473 if (params.filter_ssids == NULL)
2474 return -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002475
Dmitry Shmidt98660862014-03-11 17:26:21 -07002476 i = 0;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002477 prio = 0;
2478 ssid = wpa_s->conf->pssid[prio];
Dmitry Shmidt98660862014-03-11 17:26:21 -07002479 while (ssid) {
2480 if (!wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002481 if (ssid->scan_ssid && params.num_ssids < num_ssid) {
2482 params.ssids[params.num_ssids].ssid =
2483 ssid->ssid;
2484 params.ssids[params.num_ssids].ssid_len =
2485 ssid->ssid_len;
2486 params.num_ssids++;
2487 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07002488 os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
2489 ssid->ssid_len);
2490 params.filter_ssids[i].ssid_len = ssid->ssid_len;
2491 params.num_filter_ssids++;
2492 i++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002493 if (i == num_match_ssid)
Dmitry Shmidt98660862014-03-11 17:26:21 -07002494 break;
2495 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002496 if (ssid->pnext)
2497 ssid = ssid->pnext;
2498 else if (prio + 1 == wpa_s->conf->num_prio)
2499 break;
2500 else
2501 ssid = wpa_s->conf->pssid[++prio];
Dmitry Shmidt98660862014-03-11 17:26:21 -07002502 }
2503
2504 if (wpa_s->conf->filter_rssi)
2505 params.filter_rssi = wpa_s->conf->filter_rssi;
2506
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002507 if (wpa_s->sched_scan_plans_num) {
2508 params.sched_scan_plans = wpa_s->sched_scan_plans;
2509 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
2510 } else {
2511 /* Set one scan plan that will run infinitely */
2512 if (wpa_s->conf->sched_scan_interval)
2513 scan_plan.interval = wpa_s->conf->sched_scan_interval;
2514 else
2515 scan_plan.interval = 10;
2516
2517 scan_plan.iterations = 0;
2518 params.sched_scan_plans = &scan_plan;
2519 params.sched_scan_plans_num = 1;
2520 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07002521
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002522 if (params.freqs == NULL && wpa_s->manual_sched_scan_freqs) {
2523 wpa_dbg(wpa_s, MSG_DEBUG, "Limit sched scan to specified channels");
2524 params.freqs = wpa_s->manual_sched_scan_freqs;
2525 }
2526
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002527 if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_PNO) {
2528 params.mac_addr_rand = 1;
2529 if (wpa_s->mac_addr_pno) {
2530 params.mac_addr = wpa_s->mac_addr_pno;
2531 params.mac_addr_mask = wpa_s->mac_addr_pno + ETH_ALEN;
2532 }
2533 }
2534
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002535 ret = wpa_supplicant_start_sched_scan(wpa_s, &params);
Dmitry Shmidt98660862014-03-11 17:26:21 -07002536 os_free(params.filter_ssids);
2537 if (ret == 0)
2538 wpa_s->pno = 1;
2539 else
2540 wpa_msg(wpa_s, MSG_ERROR, "Failed to schedule PNO");
2541 return ret;
2542}
2543
2544
2545int wpas_stop_pno(struct wpa_supplicant *wpa_s)
2546{
2547 int ret = 0;
2548
2549 if (!wpa_s->pno)
2550 return 0;
2551
2552 ret = wpa_supplicant_stop_sched_scan(wpa_s);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002553 wpa_s->sched_scan_stop_req = 1;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002554
2555 wpa_s->pno = 0;
2556 wpa_s->pno_sched_pending = 0;
2557
2558 if (wpa_s->wpa_state == WPA_SCANNING)
2559 wpa_supplicant_req_scan(wpa_s, 0, 0);
2560
2561 return ret;
2562}
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002563
2564
2565void wpas_mac_addr_rand_scan_clear(struct wpa_supplicant *wpa_s,
2566 unsigned int type)
2567{
2568 type &= MAC_ADDR_RAND_ALL;
2569 wpa_s->mac_addr_rand_enable &= ~type;
2570
2571 if (type & MAC_ADDR_RAND_SCAN) {
2572 os_free(wpa_s->mac_addr_scan);
2573 wpa_s->mac_addr_scan = NULL;
2574 }
2575
2576 if (type & MAC_ADDR_RAND_SCHED_SCAN) {
2577 os_free(wpa_s->mac_addr_sched_scan);
2578 wpa_s->mac_addr_sched_scan = NULL;
2579 }
2580
2581 if (type & MAC_ADDR_RAND_PNO) {
2582 os_free(wpa_s->mac_addr_pno);
2583 wpa_s->mac_addr_pno = NULL;
2584 }
2585}
2586
2587
2588int wpas_mac_addr_rand_scan_set(struct wpa_supplicant *wpa_s,
2589 unsigned int type, const u8 *addr,
2590 const u8 *mask)
2591{
2592 u8 *tmp = NULL;
2593
2594 wpas_mac_addr_rand_scan_clear(wpa_s, type);
2595
2596 if (addr) {
2597 tmp = os_malloc(2 * ETH_ALEN);
2598 if (!tmp)
2599 return -1;
2600 os_memcpy(tmp, addr, ETH_ALEN);
2601 os_memcpy(tmp + ETH_ALEN, mask, ETH_ALEN);
2602 }
2603
2604 if (type == MAC_ADDR_RAND_SCAN) {
2605 wpa_s->mac_addr_scan = tmp;
2606 } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
2607 wpa_s->mac_addr_sched_scan = tmp;
2608 } else if (type == MAC_ADDR_RAND_PNO) {
2609 wpa_s->mac_addr_pno = tmp;
2610 } else {
2611 wpa_printf(MSG_INFO,
2612 "scan: Invalid MAC randomization type=0x%x",
2613 type);
2614 os_free(tmp);
2615 return -1;
2616 }
2617
2618 wpa_s->mac_addr_rand_enable |= type;
2619 return 0;
2620}
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002621
2622
2623int wpas_abort_ongoing_scan(struct wpa_supplicant *wpa_s)
2624{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002625 int scan_work = !!wpa_s->scan_work;
2626
2627#ifdef CONFIG_P2P
2628 scan_work |= !!wpa_s->p2p_scan_work;
2629#endif /* CONFIG_P2P */
2630
2631 if (scan_work && wpa_s->own_scan_running) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002632 wpa_dbg(wpa_s, MSG_DEBUG, "Abort an ongoing scan");
2633 return wpa_drv_abort_scan(wpa_s);
2634 }
2635
2636 return 0;
2637}
2638
2639
2640int wpas_sched_scan_plans_set(struct wpa_supplicant *wpa_s, const char *cmd)
2641{
2642 struct sched_scan_plan *scan_plans = NULL;
2643 const char *token, *context = NULL;
2644 unsigned int num = 0;
2645
2646 if (!cmd)
2647 return -1;
2648
2649 if (!cmd[0]) {
2650 wpa_printf(MSG_DEBUG, "Clear sched scan plans");
2651 os_free(wpa_s->sched_scan_plans);
2652 wpa_s->sched_scan_plans = NULL;
2653 wpa_s->sched_scan_plans_num = 0;
2654 return 0;
2655 }
2656
2657 while ((token = cstr_token(cmd, " ", &context))) {
2658 int ret;
2659 struct sched_scan_plan *scan_plan, *n;
2660
2661 n = os_realloc_array(scan_plans, num + 1, sizeof(*scan_plans));
2662 if (!n)
2663 goto fail;
2664
2665 scan_plans = n;
2666 scan_plan = &scan_plans[num];
2667 num++;
2668
2669 ret = sscanf(token, "%u:%u", &scan_plan->interval,
2670 &scan_plan->iterations);
2671 if (ret <= 0 || ret > 2 || !scan_plan->interval) {
2672 wpa_printf(MSG_ERROR,
2673 "Invalid sched scan plan input: %s", token);
2674 goto fail;
2675 }
2676
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002677 if (scan_plan->interval > wpa_s->max_sched_scan_plan_interval) {
2678 wpa_printf(MSG_WARNING,
2679 "scan plan %u: Scan interval too long(%u), use the maximum allowed(%u)",
2680 num, scan_plan->interval,
2681 wpa_s->max_sched_scan_plan_interval);
2682 scan_plan->interval =
2683 wpa_s->max_sched_scan_plan_interval;
2684 }
2685
2686 if (ret == 1) {
2687 scan_plan->iterations = 0;
2688 break;
2689 }
2690
2691 if (!scan_plan->iterations) {
2692 wpa_printf(MSG_ERROR,
2693 "scan plan %u: Number of iterations cannot be zero",
2694 num);
2695 goto fail;
2696 }
2697
2698 if (scan_plan->iterations >
2699 wpa_s->max_sched_scan_plan_iterations) {
2700 wpa_printf(MSG_WARNING,
2701 "scan plan %u: Too many iterations(%u), use the maximum allowed(%u)",
2702 num, scan_plan->iterations,
2703 wpa_s->max_sched_scan_plan_iterations);
2704 scan_plan->iterations =
2705 wpa_s->max_sched_scan_plan_iterations;
2706 }
2707
2708 wpa_printf(MSG_DEBUG,
2709 "scan plan %u: interval=%u iterations=%u",
2710 num, scan_plan->interval, scan_plan->iterations);
2711 }
2712
2713 if (!scan_plans) {
2714 wpa_printf(MSG_ERROR, "Invalid scan plans entry");
2715 goto fail;
2716 }
2717
2718 if (cstr_token(cmd, " ", &context) || scan_plans[num - 1].iterations) {
2719 wpa_printf(MSG_ERROR,
2720 "All scan plans but the last must specify a number of iterations");
2721 goto fail;
2722 }
2723
2724 wpa_printf(MSG_DEBUG, "scan plan %u (last plan): interval=%u",
2725 num, scan_plans[num - 1].interval);
2726
2727 if (num > wpa_s->max_sched_scan_plans) {
2728 wpa_printf(MSG_WARNING,
2729 "Too many scheduled scan plans (only %u supported)",
2730 wpa_s->max_sched_scan_plans);
2731 wpa_printf(MSG_WARNING,
2732 "Use only the first %u scan plans, and the last one (in infinite loop)",
2733 wpa_s->max_sched_scan_plans - 1);
2734 os_memcpy(&scan_plans[wpa_s->max_sched_scan_plans - 1],
2735 &scan_plans[num - 1], sizeof(*scan_plans));
2736 num = wpa_s->max_sched_scan_plans;
2737 }
2738
2739 os_free(wpa_s->sched_scan_plans);
2740 wpa_s->sched_scan_plans = scan_plans;
2741 wpa_s->sched_scan_plans_num = num;
2742
2743 return 0;
2744
2745fail:
2746 os_free(scan_plans);
2747 wpa_printf(MSG_ERROR, "invalid scan plans list");
2748 return -1;
2749}
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002750
2751
2752/**
2753 * wpas_scan_reset_sched_scan - Reset sched_scan state
2754 * @wpa_s: Pointer to wpa_supplicant data
2755 *
2756 * This function is used to cancel a running scheduled scan and to reset an
2757 * internal scan state to continue with a regular scan on the following
2758 * wpa_supplicant_req_scan() calls.
2759 */
2760void wpas_scan_reset_sched_scan(struct wpa_supplicant *wpa_s)
2761{
2762 wpa_s->normal_scans = 0;
2763 if (wpa_s->sched_scanning) {
2764 wpa_s->sched_scan_timed_out = 0;
2765 wpa_s->prev_sched_ssid = NULL;
2766 wpa_supplicant_cancel_sched_scan(wpa_s);
2767 }
2768}
2769
2770
2771void wpas_scan_restart_sched_scan(struct wpa_supplicant *wpa_s)
2772{
2773 /* simulate timeout to restart the sched scan */
2774 wpa_s->sched_scan_timed_out = 1;
2775 wpa_s->prev_sched_ssid = NULL;
2776 wpa_supplicant_cancel_sched_scan(wpa_s);
2777}